小球碰撞

采用java awt以及swing写的小球碰撞

线程是一个非常重要的知识块!!!

//球类
public class Ball {
    
    //1.属性:所有球的共同的字段
    int x;
    int y;
    int d;//球的半径
    int speed;//速度
    int dir; //方向
    Color c;//颜色
    
    //定义四个方向
    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;//右下
    
    //构造器 随机构造
    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方法:
            使用当前颜色填充外接指定矩形框的椭圆。
        参数:
        x - 要填充椭圆的左上角的 x 坐标。
        y - 要填充椭圆的左上角的 y 坐标。
        width - 要填充椭圆的宽度。
        height - 要填充椭圆的高度。 
     */
    public void draw(Graphics g) {
        g.setColor(c);//设置颜色
        g.fillOval(x, y, d, d);
    }
    
    //根据方向更改坐标
    //碰撞检测
    public void change() {
        switch(dir) {
        
        case RIGHT_UP://向右上方向移动
            x += speed;//以speed的速度移动
            y -= speed;
            if(y < 0) {
                dir = RIDHT_DOWN;
            }
            if(x > 800-d) {
                dir = LEFT_UP;
            }
            break;
        case RIDHT_DOWN://向右下方向移动
            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://向左上方向移动
            x -= speed;
            y -= speed;
            if(x < 0) {
                dir = RIGHT_UP;
            }
            if(y < 0) {
                dir = LEFT_DOWN;
            }
            break;
        case LEFT_DOWN://向左下方向移动
            x -= speed;
            y += speed;
            if(x < 0) {//当碰到左边界,让小球方向改为右下
                dir = RIDHT_DOWN;
            }
            if(y > 600-d) {//当碰到下边界,让小球方向改为左上
                dir = LEFT_UP;
            }
            
            break;
        default:
            System.out.println("error");
            System.exit(0);
        }
    }
}
//窗体
public
class BallFrame extends JFrame{ //构造器 public BallFrame() { //创建窗体 最底层 // JFrame frame = new JFrame("窗体"); //让BallFrame继承JFrame 则直接用this调用 //设置窗体的位置和大小 // frame.setBounds(500,200,800,600);//x y 宽 高 this.setBounds(500,200,800,600); //容器类 //JPanel panel = new JPanel(); BallPanel panel = new BallPanel(); //设置背景颜色 panel.setBackground(Color.black); //将容器添加到窗体上 容器在窗体上层 // frame.add(panel); this.add(panel); panel.move();//球体移动方法 //设置窗体显示 // frame.setVisible(true); this.setVisible(true); //设置窗体关闭 // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new BallFrame(); } }
/*
 * 1 创建数组,存放多个球
 * 2 在构造方法中,对数组进行初始化
 * 3 在paint方法中,用for循环遍历每个球
 * 4 在move中 通过遍历到的球 
 * 
 */
//自定义容器类
public class BallPanel extends JPanel{
    
    //创建一个球的对象
    Ball b1 = new Ball();
    
    Ball[] balls = new Ball[20];
    
    //构造
    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();
                    }
                    //重构
                    repaint();
                    try {
                        Thread.sleep(50);
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        
    }
}

猜你喜欢

转载自www.cnblogs.com/jiang0123/p/11285986.html
今日推荐