绘制随机小球来回弹动java代码

上传人:re****.1 文档编号:490108523 上传时间:2022-09-15 格式:DOC 页数:10 大小:21.01KB
返回 下载 相关 举报
绘制随机小球来回弹动java代码_第1页
第1页 / 共10页
绘制随机小球来回弹动java代码_第2页
第2页 / 共10页
绘制随机小球来回弹动java代码_第3页
第3页 / 共10页
绘制随机小球来回弹动java代码_第4页
第4页 / 共10页
绘制随机小球来回弹动java代码_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《绘制随机小球来回弹动java代码》由会员分享,可在线阅读,更多相关《绘制随机小球来回弹动java代码(10页珍藏版)》请在金锄头文库上搜索。

1、Java小球碰撞代码绘制小球随机位置,随机颜色(自定义颜色)来回碰撞。这个代码只有上下左右四个方向,所以反弹也是固定的45角。首先是定义小球(Ball类)package picture;import java.awt.Color;import java.awt.Graphics;public class Ball private int x,y;/分清定义是左上角顶点坐标还是圆心点坐标(圆心坐标)private int rSize;/半径private Color color;private int speed;private int orientation;private BallPanel

2、 panel;/获得小球所属的面板public static final int RIGHT_DOWN=0;public static final int RIGHT_ON=1;public static final int LEFT_DOWN=2;public static final int LEFT_ON=3;public Ball()public Ball(int x, int y, int rSize, Color color, int speed, int orientation) super();this.x = x;this.y = y;this.rSize = rSize;t

3、his.color = color;this.speed = speed;this.orientation = orientation;public void draw(Graphics g)g.setColor(color);g.fillArc(x-rSize, y-rSize/*左上角定点坐标*/, 2*rSize, 2*rSize, 0, 360);/定义小球绘画的逻辑public void move()/小球处在不同方向时移动所改变的坐标值switch(orientation)case RIGHT_DOWN:x+=speed;y+=speed;if(x+rSize =panel.get

4、Width()this.orientation = LEFT_DOWN;if(y+rSize =panel.getHeight()this.orientation = RIGHT_ON;break;case RIGHT_ON:x+=speed;y-=speed;if(x+rSize =panel.getWidth()this.orientation = LEFT_ON;if(y-rSize = 0 )this.orientation = RIGHT_DOWN;break;case LEFT_DOWN:x-=speed;y+=speed;if(x-rSize = 0 )this.orientat

5、ion = RIGHT_DOWN;if(y+rSize =panel.getHeight()this.orientation = LEFT_ON;break;case LEFT_ON:x-=speed;y-=speed;if(x-rSize = 0 )this.orientation = RIGHT_ON;if(y-rSize = 0)this.orientation = LEFT_DOWN;break;/*碰到边界事件处理*/public int getX() return x;public void setX(int x) this.x = x;public int getY() retu

6、rn y;public void setY(int y) this.y = y;public int getrSize() return rSize;public void setrSize(int rSize) this.rSize = rSize;public Color getColor() return color;public void setColor(Color color) this.color = color;public int getSpeed() return speed;public void setSpeed(int speed) this.speed = spee

7、d;public int getOrientation() return orientation;public void setOrientation(int orientation) this.orientation = orientation;public BallPanel getPanel() return panel;public void setPanel(BallPanel panel) this.panel = panel;然后是小球的面板(BallPanel)package picture;import java.awt.Color;import java.awt.Graph

8、ics;import javax.swing.JPanel;public class BallPanel extends JPanelprivate int x = 0,y = 0;/构造函数提前调用startRun不合适private Ballballs;private Colorcs = Color.red,Color.blue,Color.green,Color.cyan,Color.yellow,Color.gray,Color.orange;public BallPanel()this.setBackground(Color.WHITE);balls = new Ball10;for

9、(int i = 0 ;iballs.length;i+)ballsi = new Ball(int)(Math.random()*800),(int)(Math.random()*600),(int)(Math.random()*20)+10,cs(int)(Math.random()*cs.length),(int)(Math.random()*10)+1,(int)(Math.random()*4);ballsi.setPanel(this);public void paint(Graphics g)super.paint(g);for(int i=0;iballs.length;i+)

10、ballsi.draw(g);public void startRun()new Thread()public void run()while(true)for(int i = 0;iballs.length;i+)ballsi.move();repaint();/重新画屏幕try Thread.sleep(2);/画完之后睡一会再循环 catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();.start();这里我需要提醒一下,ball类里面对于小球方向的控制判定,我是可以按照不同的定义。xy的值可以是小球左上角顶点坐标,也可以是圆心坐标。所以才会有x-rsize和y-rsize这个说法。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号