博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一个测试Swing多线程的 小球.

Posted on 2012-05-15 23:38  紫冰龙  阅读(234)  评论(0编辑  收藏  举报
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;
public class ThreadTestBall extends JFrame {
    private ArrayList<Ellipse2D> balls;
    private BallPanel ballPanel;
    public ThreadTestBall(){
        balls = new ArrayList<Ellipse2D>();
        ballPanel = new BallPanel(balls);
        add(ballPanel,BorderLayout.CENTER);
        JPanel ButtonPanel = new JPanel();
        JButton startBt = new JButton("Start");
        JButton stopBt = new JButton("Exit");
        
        add(ButtonPanel,BorderLayout.SOUTH);
        ButtonPanel.add(startBt);
        ButtonPanel.add(stopBt);
        
        startBt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Thread t = new Thread(new Ball());
                t.start();
            }
        });
        stopBt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        
        
        
        setTitle("ThreadTestBall");
        setSize(450,400);
        setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth()-300)/2),
                (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight()-400)/2));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args){
        new ThreadTestBall();
    }
    public class Ball implements Runnable{
        private int dx=1;
        private int dy=1;
        
        private Ellipse2D ball;
        public Ball(){
            double x = new Random().nextInt((int)ballPanel.getBounds().getMaxX()-20);
            double y = new Random().nextInt((int)ballPanel.getBounds().getMaxY()-20);
            ball = new Ellipse2D.Double(x,y,15,15);
            balls.add(ball);
            
        }
        public void run(){
            for(int i=0;i<2000;i++){
                move();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //System.out.println(Thread.currentThread().getName()+":"+i);
                ballPanel.repaint();
            }
        }
        public void move(){
            double x=ball.getX();
            double y=ball.getY();
            double maxx = ballPanel.getBounds().getMaxX();
            double maxy = ballPanel.getBounds().getMaxY();
            double minx = ballPanel.getBounds().getMinX();
            double miny = ballPanel.getBounds().getMinY();
            x+=dx;
            y+=dy;
            if (x>(maxx-15)) {x-=dx;dx=-dx;}
            if (x<(minx+15)) {x-=dx;dx=-dx;}
            if (y>(maxy-15)) {y-=dy;dy=-dy;}
            if (y<(miny+15)) {y-=dy;dy=-dy;}
            ball.setFrame(x, y, 15, 15);
        }
    }
    
}
class BallPanel extends JPanel {
    private ArrayList<Ellipse2D> balls;
    public BallPanel(ArrayList<Ellipse2D> balls){
        this.balls = balls;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Color[] color=new Color[]{Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GREEN
                ,Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.RED};
        
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(color[new Random().nextInt(color.length)]);
        for(Ellipse2D ball:balls){
            g2.fill(ball);
        }
            
        
    }
}