zoukankan      html  css  js  c++  java
  • 一个测试Swing多线程的 小球.

    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);
            }
                
            
        }
    }
  • 相关阅读:
    做题经验
    4906 删数问题
    1225 八数码难题
    1005 生日礼物
    1004 四子连棋 未完成
    1008 选数 2002年NOIP全国联赛普及组
    1068 乌龟棋 2010年NOIP全国联赛提高组
    2292 图灵机游戏
    实战数据结构(9)_单链表实现多项式的相乘
    最近招两个兼职的活(PHP和JSP)
  • 原文地址:https://www.cnblogs.com/qqjue/p/2502614.html
Copyright © 2011-2022 走看看