⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ballworld6.java

📁 Java变的几个小程序
💻 JAVA
字号:
// BallWorld - version 6

          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;

          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;

          }

          /// NEW NEW NEW - make the Ball object referred to bounce

          private void bounce(Ball ball)

          {

          // check for left of frame collision (X value too small)

          if ( ball.getX() < 0 )

          ball.setMotion( -ball.getXMotion(), ball.getYMotion() );

          // check for right of frame collision (X value too large)

          if ( ball.getX() > frameWidth )

          ball.setMotion(-ball.getXMotion(), ball.getYMotion());

          // check for top of frame collision (Y value too small)

          if ( ball.getY() < 0 )

          ball.setMotion(ball.getXMotion(), -ball.getYMotion());

          // check for bottom of frame collision (Y value too large)

          if ( ball.getY() > frameHeight )

          ball.setMotion(ball.getXMotion(), -ball.getYMotion());

          }

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

          public void paint(Graphics g)

          {

          myBall.paint(g);

          myBall.move();

          anotherBall.paint(g);

          anotherBall.move();

          pause( 20 );

          // NEW NEW NEW - call method "bounce" with each ball reference

          bounce( myBall );

          bounce( anotherBall );

          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 + -