资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
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 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;this.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.getWidth()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.orientation = 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() return 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 = speed;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.Graphics;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(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+)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这个说法。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号