?? ballworld4.java
字號:
// BallWorld - version 4
import java.awt.*;
public class BallWorld extends Frame
{
////////////////////////////////
////////// variables ///////////
////////////////////////////////
// application variables
// NEW NEW NEW - variables for width and height of frame
public static final int frameWidth = 600;
public static final int frameHeight = 400;
private Ball myBall;
// 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
// NEW NEW NEW - now set using variables "frameWidth", "frameHeight"
setSize(frameWidth, frameHeight);
setTitle("Ball World");
// make "myBall" a reference to a new instance of ball
// NEW NEW NEW - arguments for X, Y motion
myBall = new Ball(100, 100, 50, 4, 5);
// set out counter to zero
counter = 0;
}
////////// method paint ///////////
public void paint(Graphics g)
{
myBall.paint(g);
myBall.move();
pause( 20 );
// NEW NEW NEW - 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 - bounce a lot more times !
if (counter < 500)
{
counter = counter + 1;
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 + -