java弹跳小球程序_JAVA框内小球弹跳加重力

/**BouncePanel.java*/

import java.awt.*;import java.awt.event.*;import java.awt.geom.*;importjava.util.Random;import javax.swing.*;public class BouncePanel extendsJComponentimplementsComponentListener, Runnable {final static long serialVersionUID = 0; //kill warning//constants

final static BasicStroke stroke = new BasicStroke(2.0f);//fields

intRed,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

floattemp;intt;int delay; //delay between frames in miliseconds

Thread animThread; //animation thread

/*************************************************

* Draws the ball on the screen.

*************************************************/

public voidpaintComponent(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(newColor(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 voidcomponentHidden (ComponentEvent e) { }public voidcomponentMoved (ComponentEvent e) { }public voidcomponentResized(ComponentEvent e) { }public voidcomponentShown (ComponentEvent e) { }/****************************************************

* Checks to see if the ball has 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 is what actually causes the ball to bounce.

***********************************************************/

public voidrun() {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.

****************************************************/

publicBouncePanel() {//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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值