JAVA框內小球彈跳加重力

來源:互聯網
上載者:User

標籤:refresh   isp   struct   upd   oat   port   xtend   animation   span   

/**BouncePanel.java*/import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.util.Random;import javax.swing.*;public class BouncePanel extends JComponent                          implements ComponentListener, Runnable {        final static long serialVersionUID = 0;  // kill warning        // constants    final static BasicStroke stroke = new BasicStroke(2.0f);          // fields    int Red,Green,Blue;    float xsize,ysize;  // size of window    float xpos,ypos,yypos;    // position of ball     float xlen,ylen;    // size of ball    float speed;        // distance the ball moves in each frame    float dx,dy,ddy,da;        // current speed + direction of ball     float temp;    int  t;    int    delay;       // delay between frames in miliseconds    Thread animThread;  // animation thread                                           /*************************************************     * Draws the ball on the screen.     *************************************************/                                    public void paintComponent(Graphics g) {        Graphics2D g2 = (Graphics2D) g.create();        // get the current window size             Dimension dim = getSize();        xsize = dim.width;        ysize = dim.height;                 // clear background to white        g2.setPaint(Color.white);        g2.fill(new Rectangle2D.Double(0,0,xsize,ysize));               // draw ball                         g2.setPaint(new Color(Red,Green,Blue));        g2.fill(new Ellipse2D.Double(xpos, ysize-ypos-ylen, xlen, ylen));        g2.setColor(Color.black);        g2.draw(new Ellipse2D.Double(xpos, ysize-ypos-ylen, xlen, ylen));         g2.dispose();    }        // empty methods that are required by the GUI event loop    public void componentHidden (ComponentEvent e)  { }    public void componentMoved  (ComponentEvent e)  { }    public void componentResized(ComponentEvent e)  { }    public void componentShown  (ComponentEvent e)  { }        /****************************************************     * Checks to see if the ball has hit any walls.     * Called from within run().      ****************************************************/    public void checkWalls() {        Random rand = new Random();       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 is what actually causes the ball to bounce.     ***********************************************************/     public void run() {              while (true) {  // loop forever                    // storage history data          yypos=ypos;          ddy = dy;          //new Vy          dy=dy + da;          // update position          xpos += dx ;          temp = (dy+ddy)/2;//*|t=1|          ypos =ypos + temp;                      // check to see if the ball has hit any walls        checkWalls();              // sleep a bit until the next frame        try { Thread.sleep(delay); }         catch (InterruptedException e) { break; }                // refresh the display        repaint();        }    }            /****************************************************     * This is a constructor for the BouncePanel class.     * It initializes all the values that the class needs     * in order to work properly.     ****************************************************/        public BouncePanel() {        // set values for all the variables        xsize = 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 properties        setBackground(Color.white);        setOpaque(true);        setPreferredSize(new Dimension((int) xsize, (int) ysize));        setFocusable(true);        addComponentListener(this);                // start the animation thread        animThread = new Thread(this);        animThread.start();    }}

JAVA框內小球彈跳加重力

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.