[5168] in java-interest

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

Bouncing Ball...take 2...

daemon@ATHENA.MIT.EDU (Paul Benati)
Tue Jan 30 16:01:54 1996

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

Group,

I have another question.  In the bouncing ball program (attached below) 
when the user depresses the mouse the ball stops its movement.  When the 
ball is stopped, then the user can depress the <CTRL> and mouse button 
to draw a line from the ball's origin to the mouse cursor location.  My 
question is, why doesn't the event's (x,y) location equate to the location 
of the cursor?  [I'm on a Windows95 box.]

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);
    setCursor(CROSSHAIR_CURSOR);

    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;
    int     modifiers = e.modifiers;

    switch (e.id)
    {
      case e.MOUSE_DOWN:
      {
        if((moveBall == false) && (modifiers == 2))
        {
          ball.drawVelocityVector(e.x, e.y);
        }
        else
        {
          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 boolean velocityVector = false;
  private int oldX2              = -1;
  private int oldY2;
  private int newX2;
  private int newY2;
  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 drawVelocityVector(int x, int y)
  {
    velocityVector = true;

    newX2 = x;
    newY2 = y;

    repaint(1);
  }

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

    calculateBallVelocity();
    calculateBallPosition(windowWidth, windowHeight);

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

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

    if(velocityVector)
    {
//      if(oldX2 != -1)
//      {
//        g.drawLine(xOrigin, yOrigin, oldX2, oldY2);
System.out.println("origin = ("+xOrigin+","+yOrigin+")");
System.out.println("new    = ("+newX2+","+newY2+")");
        g.drawLine(xOrigin, yOrigin, newX2, newY2);

        oldX2 = newX2;
        oldY2 = newY2;
//      }
//      else
//      {
//        g.drawLine(xOrigin, yOrigin, newX2, newY2);
//
//        oldX2 = newX2;
//        oldY2 = newY2;
//      }

      velocityVector = false;
    }

    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