ballworld2.java

来自「Java变的几个小程序」· Java 代码 · 共 110 行

JAVA
110
字号
// BallWorld.java - version 2

          import java.awt.*;

          public class BallWorld extends Frame

          {

          ////////////////////////////////

          ////////// variables ///////////

          ////////////////////////////////

          private Ball myBall;

          // NEW NEW NEW

          // 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(600, 400);

          setTitle("Ball World");

          // make "myBall" a reference to a new instance of ball

          myBall = new Ball(100, 100, 50, 4, 5 );

          // NEW NEW NEW - set out counter to zero

          counter = 0;

          }

          ////////// method paint ///////////

          public void paint(Graphics g)

          {

          myBall.paint( g );

          pause( 2000 );

          // NEW NEW NEW - change ball position

          myBall.move();

          // NEW NEW NEW - test counter, so will repaint again with new ball position

          if( counter < 1 )

          {

          repaint();

          counter = counter + 1;

          }

          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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?