[5151] in java-interest

home help back first fref pref prev next nref lref last post

Bouncing Ball program...

daemon@ATHENA.MIT.EDU (Paul Benati)
Tue Jan 30 10:11:58 1996

Date: Tue, 30 Jan 1996 08:32:15 -0500
To: java-interest@java.sun.com
From: Paul Benati <benatip@pcd.kodak.com>

Anyone out there have any suggestions as to how to make this bouncing 
ball bounce smoothly?

Paul

--------------------------[ code below ]-----------------------------

import java.awt.*;
import java.lang.*;

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
public class Bounce extends Frame
{
  private static    final int TimeInterval = 75;
  private static    Ball ball = new Ball();
  private Panel     mainPanel = new Panel();
  private long      time      = 0;
  private boolean   doQuit    = false;
  private boolean   moveBall  = true;
  private Dimension frameSize;

  // ----------------------------------------------------------------------
  // ----------------------------------------------------------------------
  public
  Bounce(String title)
  {
    setTitle(title);

    setBackground(Color.red);
    add("Center", ball);
  }

  // ----------------------------------------------------------------------
  // ----------------------------------------------------------------------
  public
  void go()
  {
    time = System.currentTimeMillis();

    while(doQuit != true)
    {
      if((moveBall) && ((time + TimeInterval) <= System.currentTimeMillis()))
      {
        frameSize = size();
        ball.moveBall(frameSize.width, frameSize.height);

        time = System.currentTimeMillis();
      }
    }
  }

  // ----------------------------------------------------------------------
  // ----------------------------------------------------------------------
  public
  boolean handleEvent(Event e)
  {
    boolean handled = false;

    switch (e.id)
    {
      case e.MOUSE_DOWN:
      {
        if(moveBall)
          moveBall = false;
        else
          moveBall = true;

        break;
      }

      case e.MOUSE_UP:
      {


        break;
      }

      case e.KEY_PRESS:
      {
        System.exit(0);

        break;
      }

      case e.WINDOW_DESTROY:
      {
        System.exit(0);

        break;
      }
    } // end of switch statement

    return handled;
  }

  // ----------------------------------------------------------------------
  // ----------------------------------------------------------------------
  public
  static void main(String args[])
  {
    Bounce bounce = new Bounce("Bounce");

    bounce.pack();
    bounce.show();
    bounce.go();
  }

  // ----------------------------------------------------------------------
  // ----------------------------------------------------------------------
  public
  Dimension preferredSize()
  {
    return(new Dimension(200, 300));
  }
}


// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
class Ball extends Panel
{
  private static double Gravity  = 1.0; // pid/cron^2
  private static double TimeUnit = 1.0; // cron
  private int radius;
  private int diameter;
  private int xOrigin;
  private int yOrigin;
  private int xVelocity;
  private int yVelocity;

  public
  Ball()
  {
    xOrigin   = 50;
    yOrigin   = 50;
    radius    = 20;
    diameter  = radius*2;
    xVelocity = 5;
    yVelocity = 0;

    setBackground(Color.white);
    resize(200, 300);
  }

  // --------------------------------------------------------------------
  // --------------------------------------------------------------------
  public
  void calculateBallPosition(int windowWidth, int windowHeight)
  {
    xOrigin += (int) (xVelocity + 0.5*Gravity*TimeUnit*TimeUnit);
    yOrigin += (int) (yVelocity + 0.5*Gravity*TimeUnit*TimeUnit);

    // ------------------------------------------------------------------
    // Reflect ball from wall if necessary!
    // ------------------------------------------------------------------
    if((xOrigin + radius) >= windowWidth)
    {
      xOrigin = windowWidth - ((xOrigin + radius) - windowWidth + radius);

      xVelocity = -(Math.abs(xVelocity) - 1);
    }
    else if((xOrigin - radius) <= 0)
    {
      xOrigin = -((xOrigin - radius) - radius);

      xVelocity = (Math.abs(xVelocity) - 1);
    }

    // ------------------------------------------------------------------
    // Reflect ball from wall if necessary!
    // ------------------------------------------------------------------
    if((yOrigin + radius) >= windowHeight)
    {
      yOrigin = windowHeight - ((yOrigin + radius) - windowHeight + radius);

      yVelocity = -(Math.abs(yVelocity) - 1);
    }
    else if((yOrigin - radius) <= 0)
    {
      yOrigin = -((yOrigin - radius) - radius);

      yVelocity = (Math.abs(yVelocity) - 1);
    }
  }

  // --------------------------------------------------------------------
  // --------------------------------------------------------------------
  public
  void calculateBallVelocity()
  {
    yVelocity = yVelocity + (int) (Gravity*TimeUnit);
  }

  // --------------------------------------------------------------------
  // --------------------------------------------------------------------
  public
  void calculateBallVelocity(int xPosition1, int yPosition1,
                             int xPosition2, int yPosition2)
  {
    xVelocity = (xPosition2 - xPosition1)/10;
    yVelocity = (yPosition2 - yPosition1)/10;
  }

  // --------------------------------------------------------------------
  // --------------------------------------------------------------------
  public
  void moveBall(int windowWidth, int windowHeight)
  {
    repaint(1, xOrigin - radius, yOrigin - radius, diameter, diameter);

    calculateBallVelocity();
    calculateBallPosition(windowWidth, windowHeight);

    repaint(1, xOrigin - radius, yOrigin - radius, diameter, diameter);
  }

  // --------------------------------------------------------------------
  // --------------------------------------------------------------------
  public
  void paint(Graphics g)
  {
    g.setXORMode(getBackground());

    g.drawOval(xOrigin - radius, yOrigin - radius, diameter, diameter);
    g.fillOval(xOrigin, yOrigin, 2, 2);
  }

  // --------------------------------------------------------------------
  // --------------------------------------------------------------------
  public
  Dimension preferredSize()
  {
    return(new Dimension(200, 300));
  }
}

Paul Benati
  Digital Imaging Evangelist
  Software Engineer/Imaging Scientist

  Eastman Kodak Company       benatip@kodak.com           
  http://www.kodak.com/          ftp.kodak.com
  901 Elmgrove Road              (716) 253 - 1775 (EP Office)
  Rochester, NY 14653-5555       (716) 726 - 7295 (EP Fax)
                                       233 - 1775 (KMX/Internal)

  "I know that you believe you understand what you think I said, but 
   I am not sure you realize that what you heard is not what I meant." 

    -- by unknown author.                                    

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post