Small ball collision

Using java awt and swing the ball collision written

A thread is a very important piece of knowledge! ! !

// ball 
public  class Ball { 
    
    // 1. properties: a common field for all balls 
    int X;
     int Y;
     int D; // ball radius 
    int Speed; // velocity 
    int the dir; // orientation 
    Color C; / / color 
    
    // define four directions 
    public  static  Final  int LEFT_UP =. 1; // upper left 
    public  static  Final  int LEFT_DOWN = 2; // lower left 
    public  static  Final  int RIGHT_UP =. 3; //Upper right 
    public  static  Final  int RIDHT_DOWN =. 4; // lower right 
    
    // constructor randomly constructed 
    public Ball () { 
        
         X = ( int ) (Math.random () 800 * ); 
         
         Y = ( int ) (Math.random () 600 * ); 
         
         D = ( int ) (Math.random () * 50) + 50 ; 
         
         C = new new Color ( 
                 ( int ) (Math.random () * 256 ), 
                 ( int ) (Math.random () * 256 ), 
                 ( int) (Math.random () * 256 )); 
         
        the dir = ( int ) (Math.random () *. 4) +. 1 ; 
        
        Speed = ( int ) (Math.random () *. 5). 5 + ; 
        
        
    } 
    
    // Behavior :
     // Videos 
    / *   fillOval method: 
            the current fill color specified elliptical external rectangle. 
        Parameters: 
        x - x coordinate of the oval to be filled in the upper left corner. 
        y - upper left corner of the oval to be filled y coordinate. 
        width - the width of the ellipse is filled. 
        height - the filling height of the ellipse. 
     * / 
    Public  void Draw (Graphics G) { 
        g.setColor (C); // set the color 
        g.fillOval (X, Y, D, D); 
    } 
    
    //The direction of the coordinate change
     // collision detection 
    public  void Change () {
         Switch (the dir) { 
        
        Case RIGHT_UP: // upper right direction 
            X + = speed; // speed moving speed 
            Y - = speed;
             IF (Y <0 ) { 
                the dir = RIDHT_DOWN; 
            } 
            IF (X> 800- D) { 
                the dir = LEFT_UP; 
            } 
            BREAK ;
         case RIDHT_DOWN: // lower right direction 
            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: // lower left direction 
            X - = Speed; 
            Y + = Speed;
             IF (X <0) { // when it comes to the left border, so the ball to the lower right direction 
                dir = RIDHT_DOWN; 
            } 
            iF (Y> 600-D) { // when it comes to the boundary, so that the direction of the ball to the top left 
                the dir = LEFT_UP; 
            } 
            
            BREAK ;
         default : 
            System.out.println ( "error" 
            System.exit (0);
        }
    }
}
// form 
public
class BallFrame the extends JFrame { // constructor public BallFrame () { // Create a form the bottom // JFrame Frame = new new JFrame ( "Form"); // make BallFrame inherit JFrame directly with this calling // set the position and size of the form // frame.setBounds (500,200,800,600); // XY width Height the this .setBounds (500,200,800,600 ); // containers // the JPanel the JPanel Panel new new = (); BallPanel Panel = new new BallPanel (); // set the background color panel.setBackground (Color.black); // Add the container to form an upper container on a form // frame.add (Panel); the this .add (Panel); panel.move (); // ball moving method // set display form // Frame .setVisible (to true); the this .setVisible ( to true ); // set the form to close // frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); the this .setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); } public static void main (String [] args) { new new BallFrame (); } }
/ * 
 * 1 creates the array, storing a plurality of balls 
 * 2 in the constructor, the array is initialized 
 * 3 in the paint method, for loop through each ball 
 * 4 to move the ball through the traversing 
 * 
 * / 
/ / custom container class 
public  class BallPanel the extends the JPanel { 
    
    // Create a ball object 
    ball B1 = new new ball (); 
    
    ball [] balls = new new ball [20 is ]; 
    
    // configured 
    public BallPanel () {
         for ( int I = 0; I <balls.length; I ++ ) { 
            Balls [I] = new new Ball (); 
        } 
    } 
    //The drawing method override parent
     // Graphics Brushes class 
    public  void Paint (Graphics G) {
         Super .paint (G); 
        
        b1.draw (G); 
        
        for ( int I = 0; I <balls.length; I ++ ) { 
            Balls [I] .draw (G); 
        } 
        
    } 
    
    // Move method 
    public  void Move () { 
        
        // open new thread 
        new new the thread () {
             public  void RUN () {
                 the while ( to 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();
        
    }
}

 

Guess you like

Origin www.cnblogs.com/jiang0123/p/11285986.html