Inside the Java box ball bouncing and gravity

Source: Internet
Author: User
Tags rand

/**Bouncepanel.java*/Importjava.awt.*;Importjava.awt.event.*;Importjava.awt.geom.*;ImportJava.util.Random;Importjavax.swing.*; Public classBouncepanelextendsJComponentImplementsComponentlistener, Runnable {Final Static LongSerialversionuid = 0;//Kill Warning//Constants    Final StaticBasicstroke stroke =NewBasicstroke (2.0f); // Fields    intRed,green,blue; floatXsize,ysize;//Size of window    floatXpos,ypos,yypos;//position of Ball    floatXlen,ylen;//size of ball    floatSpeed//distance the ball moves on each frame    floatDx,dy,ddy,da;//Current speed + direction 's Ball    floattemp; intT; intDelay//delay between frames in milisecondsThread Animthread;//Animation Thread                    /************************************************* * Draws the ball on the screen . *************************************************/                                     Public voidpaintcomponent (Graphics g) {graphics2d g2=(graphics2d) g.create (); //get the current window sizeDimension Dim =GetSize (); Xsize=Dim.width; Ysize=Dim.height; //Clear background to WhiteG2.setpaint (Color.White); G2.fill (NewRectangle2d.double (0,0, xsize,ysize)); //Draw BallG2.setpaint (NewColor (Red,green,blue)); G2.fill (NewEllipse2d.double (xpos, ysize-ypos-Ylen, Xlen, Ylen));        G2.setcolor (Color.Black); G2.draw (NewEllipse2d.double (xpos, ysize-ypos-Ylen, Xlen, Ylen));    G2.dispose (); }        //empty methods that is required by the GUI event loop     Public voidComponenthidden (ComponentEvent e) {} Public voidcomponentmoved (ComponentEvent e) {} Public voidcomponentresized (ComponentEvent e) {} Public voidComponentshown (ComponentEvent e) {}/**************************************************** * Checks to see if the ball have hit any walls.      * Called from within run (). ****************************************************/     Public voidCheckwalls () {Random rand=NewRandom (); if(Xpos + Xlen >=xsize) {xpos= Xsize-Xlen; DX= -DX; Red=rand.nextint (256); Green=rand.nextint (256); Blue=rand.nextint (256); }        if(xpos <= 0) {xpos= 0; DX= -DX; Red=rand.nextint (256); Green=rand.nextint (256); Blue=rand.nextint (256); }        if(Ypos + Ylen >=ysize) {ypos= Ysize-Ylen; Dy= -dy; Red=rand.nextint (256); Green=rand.nextint (256); Blue=rand.nextint (256); }                    if(ypos <= 0) {ypos= 0; Dy= (float) Math.sqrt (2*da*yypos+ddy*Ddy); Red=rand.nextint (256); Green=rand.nextint (256); Blue=rand.nextint (256); }          }   /*Public void Speed () {}*/    /*********************************************************** * This is the animation thread.     * The code here's what actually causes the ball to bounce. ***********************************************************/      Public voidrun () { while(true) {//Loop Forever//Storage History Datayypos=ypos; Ddy=dy; //New VyDy=dy +da; //Update PositionXpos + =DX; Temp= (Dy+ddy)/2;//*|t=1|Ypos =ypos +temp; //check to see if the ball have hit any wallsCheckwalls (); //sleep a bit until the next frame        Try{thread.sleep (delay);} Catch(Interruptedexception e) { Break; } //Refresh the displayrepaint (); }    }            /**************************************************** * This was a constructor for the Bouncepanel class.     * It initializes all the values, the class needs * in order to work properly. ****************************************************/         PublicBouncepanel () {//set values for all the variablesXsize = 480; Ysize= 700; Xpos= 240; Ypos= 350; Xlen= 30; Ylen= 30; speed= 10; DX=Speed ; Dy=Speed ; Da= -1.0f; Delay= 80; //Set up window propertiesSetBackground (Color.White); Setopaque (true); Setpreferredsize (NewDimension ((int) Xsize, (int) (ysize)) ; Setfocusable (true); Addcomponentlistener ( This); //start the animation threadAnimthread =NewThread ( This);    Animthread.start (); }}

Inside the Java box ball bouncing and gravity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.