📄 ballworld5.java
字号:
// BallWorld - version 5
import java.awt.*;
public class BallWorld extends Frame
{
////////////////////////////////
////////// variables ///////////
////////////////////////////////
// application variables
// variables for width and height of frame
public static final int frameWidth = 600;
public static final int frameHeight = 400;
private Ball myBall;
// NEW NEW NEW - a second Ball variable
private Ball anotherBall;
// a counter - so we can paint the ball more than once
private int counter;
////////////////////////////////
///////////// methods //////////
////////////////////////////////
////////// method main ///////////
// sequence of instrucions for main program
static public void main (String [] args)
{
BallWorld world = new BallWorld ();
world.show ();
}
////////// method BallWorld ///////////
private BallWorld ()
{
// set size and title of our application window
setSize(frameWidth, frameHeight);
setTitle("Ball World");
// make "myBall" a reference to a new instance of ball
myBall = new Ball(100, 100, 50, 4, 5);
// NEW NEW NEW - make "anotherBall" a reference to a new instance of ball
anotherBall = new Ball(200, 200, 25, -3, -2);
// NEW NEW NEW
anotherBall.setColor( Color.green );
// set out counter to zero
counter = 0;
}
////////// method paint ///////////
public void paint(Graphics g)
{
myBall.paint(g);
myBall.move();
// NEW NEW NEW
anotherBall.paint(g);
anotherBall.move();
pause( 20 );
// make "myBall" bounce
///////// change X or Y motion if ball touches side of window
// check for left of frame collision (X value too small)
if ( myBall.getX() < 0 )
myBall.setMotion( -myBall.getXMotion(), myBall.getYMotion() );
// check for right of frame collision (X value too large)
if ( myBall.getX() > frameWidth )
myBall.setMotion(-myBall.getXMotion(), myBall.getYMotion());
// check for top of frame collision (Y value too small)
if ( myBall.getY() < 0 )
myBall.setMotion(myBall.getXMotion(), -myBall.getYMotion());
// check for bottom of frame collision (Y value too large)
if ( myBall.getY() > frameHeight )
myBall.setMotion(myBall.getXMotion(), -myBall.getYMotion());
///////////////////////////
// NEW NEW NEW - make "anotherBall" bounce
/////////////////////////////////////
// check for left of frame collision (X value too small)
if ( anotherBall.getX() < 0 )
anotherBall.setMotion( -anotherBall.getXMotion(), anotherBall.getYMotion() );
// check for right of frame collision (X value too large)
if ( anotherBall.getX() > frameWidth )
anotherBall.setMotion(-anotherBall.getXMotion(), anotherBall.getYMotion());
// check for top of frame collision (Y value too small)
if ( anotherBall.getY() < 0 )
anotherBall.setMotion(anotherBall.getXMotion(), -anotherBall.getYMotion());
// check for bottom of frame collision (Y value too large)
if ( anotherBall.getY() > frameHeight )
anotherBall.setMotion(anotherBall.getXMotion(), -anotherBall.getYMotion());
counter = counter + 1;
if (counter < 500)
repaint();
else
System.exit(0);
}
////////// method pause ///////////
private void pause(int numMilliseconds)
{
// use a Java libary routine to pause a little
try{ Thread.sleep( numMilliseconds ); } catch (InterruptedException e){}
}
} // class BallWorld
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -