java代码画樱花_樱花的季节,教大家用CANVAS画出飞舞的樱花树

又到了樱花的季节,教大家使用canvas画出飞舞的樱花树效果。

废话少说,先看效果。

演示效果地址:http://suohb.com/work/tree4.htm

第一步,我们先画出一棵树的主体。

我画树的使用的原理是,定义一个起始点,从这个点开始,向一个角度移动一段距离。得到另一个点。

画出一条线连接两个点。

以新得到的点,依旧向这个角度,移动一段距离。得到第三个点,连写第二第三个点。

以此类推。一定步长之后,就得到一条射线。

我们根据自然界中的真实树的情况,这条线越来越细,直到最细地方结束。

48304ba5e6f9fe08f3fa1abda7d326ab.png

var treeCanvas = document.getElementById("tree");

treeCanvas.width = window.innerWidth;

treeCanvas.height = window.innerHeight ;

var tCxt = treeCanvas.getContext("2d");

var rootTop = 450 ;//树起始位置

var treeColor = "#FFF" ;//树颜色

function drawTree(x,y,deg,step){

var x1 = x + Math.cos(deg) * step ;//越细的枝干越短,所以以步长来做

var y1 = y + Math.sin(deg) * step ;

tCxt.beginPath();

tCxt.lineWidth = step/3;//树干越来越细

tCxt.moveTo(x,y);

tCxt.lineTo(x1,y1);

tCxt.strokeStyle = treeColor ;

tCxt.stroke();

step -- ;

if(step > 0){

drawTree(x1,y1,deg,step);

}

}

drawTree(treeCanvas.width/2,rootTop,-Math.PI/2,30);

48304ba5e6f9fe08f3fa1abda7d326ab.png

树干出来之后,要做分叉

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是 Java 代码飞舞樱花树: ```java import javax.swing.*; import java.awt.*; import java.util.Random; public class SakuraTree extends JFrame { private final int WIDTH = 800; private final int HEIGHT = 600; private final int DELAY = 30; private final int FLOWER_NUM = 50; private Image buffer; private Graphics bufferGraphics; private Random random = new Random(); private Flower[] flowers = new Flower[FLOWER_NUM]; private int[] xPoints = {0, 20, 20, 40, 20, 20, 0}; private int[] yPoints = {0, 20, 40, 20, 20, 0, 20}; public SakuraTree() { setTitle("Sakura Tree"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setResizable(false); buffer = createImage(WIDTH, HEIGHT); bufferGraphics = buffer.getGraphics(); for (int i = 0; i < FLOWER_NUM; i++) { int x = random.nextInt(WIDTH); int y = random.nextInt(HEIGHT / 2); int dx = random.nextInt(5) + 1; int dy = random.nextInt(5) + 1; int size = random.nextInt(20) + 10; flowers[i] = new Flower(x, y, dx, dy, size); } Timer timer = new Timer(DELAY, e -> { bufferGraphics.setColor(Color.WHITE); bufferGraphics.fillRect(0, 0, WIDTH, HEIGHT); for (Flower flower : flowers) { flower.draw(bufferGraphics); flower.update(); } repaint(); }); timer.start(); } @Override public void paint(Graphics g) { g.drawImage(buffer, 0, 0, this); } private class Flower { private int x; private int y; private int dx; private int dy; private int size; public Flower(int x, int y, int dx, int dy, int size) { this.x = x; this.y = y; this.dx = dx; this.dy = dy; this.size = size; } public void draw(Graphics g) { g.setColor(Color.PINK); for (int i = 0; i < 7; i++) { int xPoint = x + xPoints[i] * size / 40; int yPoint = y + yPoints[i] * size / 40; g.fillOval(xPoint, yPoint, size / 4, size / 4); } } public void update() { x += dx; y += dy; if (x < 0 || x > WIDTH - size / 4) { dx = -dx; } if (y < 0 || y > HEIGHT / 2 - size / 4) { dy = -dy; } } } public static void main(String[] args) { new SakuraTree().setVisible(true); } } ``` 这个程序使用了 Java Swing 绘制界面,通过 Timer 类定时更新面。其中,Flower 类代表樱花,包含了绘制和更新的方法。在主程序中创建了 50 朵樱花,并且使用随机数来设置它们的位置、速度和大小。每个樱花使用 7 个点来绘制,颜色为粉色,形状为圆形。它们会在界面中飘动,并且会在到达屏幕边缘时反弹。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值