小球碰撞
采用java awt以及swing寫的小球碰撞
線程是一個(gè)非常重要的知識(shí)塊!!!
//球類 public class Ball {//1.屬性:所有球的共同的字段int x;int y;int d;//球的半徑int speed;//速度int dir; //方向Color c;//顏色//定義四個(gè)方向public static final int LEFT_UP = 1;//左上public static final int LEFT_DOWN = 2;//左下public static final int RIGHT_UP = 3;//右上public static final int RIDHT_DOWN = 4;//右下//構(gòu)造器 隨機(jī)構(gòu)造public Ball() {x = (int) (Math.random()*800);y = (int)(Math.random()*600);d = (int)(Math.random()*50) + 50;c = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));dir = (int)(Math.random()*4) + 1;speed = (int)(Math.random()*5) + 5;}//行為://畫/* fillOval方法:使用當(dāng)前顏色填充外接指定矩形框的橢圓。參數(shù):x - 要填充橢圓的左上角的 x 坐標(biāo)。y - 要填充橢圓的左上角的 y 坐標(biāo)。width - 要填充橢圓的寬度。height - 要填充橢圓的高度。 */public void draw(Graphics g) {g.setColor(c);//設(shè)置顏色 g.fillOval(x, y, d, d);}//根據(jù)方向更改坐標(biāo)//碰撞檢測(cè)public void change() {switch(dir) {case RIGHT_UP://向右上方向移動(dòng)x += speed;//以speed的速度移動(dòng)y -= speed;if(y < 0) {dir = RIDHT_DOWN;}if(x > 800-d) {dir = LEFT_UP;}break;case RIDHT_DOWN://向右下方向移動(dòng)x += speed;y += speed;if(x > 800-d) {//dir = RIDHT_DOWN;dir = LEFT_DOWN;}if(y > 600-d) {dir = RIGHT_UP;}break;case LEFT_UP://向左上方向移動(dòng)x -= speed;y -= speed;if(x < 0) {dir = RIGHT_UP;}if(y < 0) {dir = LEFT_DOWN;}break;case LEFT_DOWN://向左下方向移動(dòng)x -= speed;y += speed;if(x < 0) {//當(dāng)碰到左邊界,讓小球方向改為右下dir = RIDHT_DOWN;}if(y > 600-d) {//當(dāng)碰到下邊界,讓小球方向改為左上dir = LEFT_UP;}break;default:System.out.println("error");System.exit(0);}} } //窗體public class BallFrame extends JFrame{//構(gòu)造器public BallFrame() {//創(chuàng)建窗體 最底層 // JFrame frame = new JFrame("窗體"); //讓BallFrame繼承JFrame 則直接用this調(diào)用//設(shè)置窗體的位置和大小 // frame.setBounds(500,200,800,600);//x y 寬 高this.setBounds(500,200,800,600);//容器類//JPanel panel = new JPanel();BallPanel panel = new BallPanel();//設(shè)置背景顏色 panel.setBackground(Color.black);//將容器添加到窗體上 容器在窗體上層 // frame.add(panel);this.add(panel);panel.move();//球體移動(dòng)方法//設(shè)置窗體顯示 // frame.setVisible(true);this.setVisible(true);//設(shè)置窗體關(guān)閉 // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {new BallFrame();}} /** 1 創(chuàng)建數(shù)組,存放多個(gè)球* 2 在構(gòu)造方法中,對(duì)數(shù)組進(jìn)行初始化* 3 在paint方法中,用for循環(huán)遍歷每個(gè)球* 4 在move中 通過(guò)遍歷到的球 * */ //自定義容器類 public class BallPanel extends JPanel{//創(chuàng)建一個(gè)球的對(duì)象Ball b1 = new Ball();Ball[] balls = new Ball[20];//構(gòu)造public BallPanel() {for(int i = 0 ; i < balls.length ; i++) {balls[i] = new Ball();}}//重寫父類繪圖方法//Graphics 畫筆類public void paint(Graphics g) {super.paint(g);b1.draw(g);for(int i = 0 ; i < balls.length ; i++) {balls[i].draw(g);}}//move方法public void move() {//開辟新的線程new Thread() {public void run() {while(true) {b1.change();for(int i = 0 ; i < balls.length ; i++) {balls[i].change();}//重構(gòu) repaint();try {Thread.sleep(50);}catch(Exception e) {e.printStackTrace();}}}}.start();} }
?
轉(zhuǎn)載于:https://www.cnblogs.com/jiang0123/p/11285986.html
總結(jié)
- 上一篇: [C++]什么是句柄?为什么会有句柄?
- 下一篇: 群聊工具