Goals

Submission
  • You do not need to submit the warmup exercise.

  • Part 0

  • Create a public_html directory if you don't already have one. This will enable you to access your applets on the Web. Use the following sequence of commands.
    cd                             # change to your root directory
    mkdir public_html              # create the directory
    chmod 755 public_html          # adjust read permissions
    

  • Copy the following files from /u/cs126/files/bounce/ into your public_html directory, and make the appropriate files world readable.
    MovingBall.html   MovingBall.java   Ball.java   sun.gif
    splat.au          earth.gif         gong.au
    
    You can do this with the following commands:
    cd ~/public_html               # move to appropriate directory
    cp /u/cs126/files/bounce/* .   # copy files
    rm *.class                     # delete byte-compiled code
    

  • Compile with the command:
    javac MovingBall.java
    
    This will create the following two files:
    Ball.class   MovingBall.class
    

  • To execute the applet, use the command:
    appletviewer MovingBall.html
    
    If everything goes properly, you should see a picture of the sun moving off the screen. If not, make sure you did the previous steps correctly. You will not be able to continue until you get this working.

  • To view under a Web browser, you need to set the appropriate files to be world readable. To do this, use the command:
    chmod 644 *.html *.gif *.au *.class
    
    Next, load the page www.cs.princeton.edu/~login/MovingBall.html in your Web browser. After recompiling the applet, you may need to enter Shift-Reload or Ctrl-Reload instead of Reload to properly reload your applet.

  • Part 1    (bounce off window)

  • Depending on the speed of your computer, you may wish to adjust the speed of the sun before continuing. ~To do this, change the third and fourth arguments to the constructor.
    b1 = new Ball(-0.3, 0.4, 0.001, -0.0005, 10, image1, sound1, this);
    

  • Modify the file Ball.java so that the ball stays within the window. Keep the x and y components between -1/2 and 1/2. Note that method move determines the size of the current window and rescales the x and y components to fill up the window.

  • Part 2    (sound)

  • Add a sound effect when the ball hits the window boundary. The following method call will play the .au sound file associated with the ball.
    sound.play();
    

  • If you are working the 101 lab, kindly adjust the volume before running your applet. To do this, right lock on the background and select Applications -> Audio Contro.

  • Part 3    (add a second ball)

  • Modify MovingBall.java to add a second ball with a different initial position and velocity. You can use the .gif file earth.gif and the sound file gong.au. Here's what the final applet should look like.

  • Feel free to add more balls. You can find additional images and sound files from the Web.

  • Challenge for the bored

  • Add gravity, and have the ball lose a certain fraction of its velocity when it bounces off a wall.

  • If two balls collide, have them bounce off each other using the physics of elastic collision.

  • Enrichment Links

  • Check the Java section of the Frequently Asked Questions page for more info and relevant links.

  • Check out Oxford Sound Archive or Wave Central for more sound files.



  • Kevin Wayne