• java学习日记-----------------------------弹弹球


    好玩的小程序,弹弹弹....................

    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    
    import javax.swing.event.*;
    public class BallsCanvas extends Canvas implements ActionListener,FocusListener{
        private Ball balls[];
        private Timer timer;//定时器
        
        private static class Ball{
            int x,y;
            Color color;
            boolean up,left;
            Ball(int x,int y,Color color){
                this.x=x;
                this.y=y;
                this.color=color;
            }
        }
        public BallsCanvas(Color color[],int delay){//指定颜色,延时
            this.balls=new Ball[color.length];
            for(int i=0,x=40;i<color.length;i++,x+=40)
                balls[i]=new Ball(x,x,color[i]);
                this.addFocusListener(this);
                timer=new Timer(delay,this);
                timer.start();//定时器启动
        }
            public void setDelay(int delay1){
                timer.setDelay(delay1);
            }
            public void paint(Graphics g){
                for(int i=0;i<balls.length;i++){
                    g.setColor(balls[i].color);
                    balls[i].x=balls[i].left?balls[i].x-10:balls[i].x+10;
                    if(balls[i].x<=0||balls[i].x>=this.getWidth())//到水平变给变方向
                    balls[i].left=!balls[i].left;
                    balls[i].y=balls[i].up?balls[i].y-10:balls[i].y+10;
                    if(balls[i].y<=0||balls[i].y>=this.getHeight())//到垂直边更改方向
                    balls[i].up=!balls[i].up;
                    g.fillOval(balls[i].x, balls[i].y, 20, 20);
                }
            }
    
        @Override
        public void focusGained(FocusEvent e) {
            // TODO Auto-generated method stub
            timer.stop();
        }
    
        @Override
        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub
            timer.restart();
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            repaint();
        }
    
    }
    class BallsJFrame extends JFrame implements ChangeListener{
         public BallsCanvas ball;
         private JSpinner spinner;
         public BallsJFrame(){
             super("弹弹球");
             this.setBounds(300,200, 480, 360);
             this.setDefaultCloseOperation(EXIT_ON_CLOSE);
             Color colors[]={Color.red,Color.green,Color.blue,Color.magenta,Color.cyan};
             ball=new BallsCanvas(colors,100);
             this.getContentPane().add(ball);
             
             JPanel panel=new JPanel();
             this.getContentPane().add(panel,"South");
             panel.add(new JLabel("Delay"));
             spinner =new JSpinner();
             spinner.setValue(100);
             panel.add(spinner);
             spinner.addChangeListener(this);
             this.setVisible(true);
         }
        @Override
        public void stateChanged(ChangeEvent e) {
            // TODO Auto-generated method stub
            ball.setDelay(Integer.parseInt(""+spinner.getValue()));
        }
        public static void main(String[] args) {
            new BallsJFrame();
        }
    }
  • 相关阅读:
    vue react 路由是否包含id
    vue react初始化脚手架
    多线程编程基础
    模拟实现简化版List迭代器&嵌入List
    斐波那契数与二分法的递归与非递归算法及其复杂度分析
    C语言模拟实现多态
    C++智能指针剖析(下)boost::shared_ptr&其他
    C++智能指针剖析(上)std::auto_ptr与boost::scoped_ptr
    C++模板实现动态顺序表(更深层次的深浅拷贝)与基于顺序表的简单栈的实现
    C++—模板(2)类模板与其特化
  • 原文地址:https://www.cnblogs.com/he-shao/p/4902735.html
Copyright © 2020-2023  润新知