1711 Recitation Laboratory 3:
- Learn to implement a class from a problem statement.
- Learn how to import Java code from an external source and incorporate it
into your project.
- Learn how to put an applet on a web site.
- Get practice using simple looks.
- Get practice using arrays of objects.
- Get practice creating applets.
Hand-in Requirements (Deliverables):
All projects and laboratories will be submitted electronically through
Zip up your entire project directory to submit as the source.
(Right click on the project folder and follow the SendTo link.)
Grading will be as follows:
This is a graphics project. The final code that your submit should not
contain any references to System.out.
- Part 1: 5 points
- Part 2: 5 points
- Part 3: 4 points
- Part 4: 4 points
- Part 5: 2 points
You can see an example of the final project
In this recitation you will get experience displaying graphical objects and
using classes that were written by others.
When completed you will have an applet with 10 bouncing balls that you will
put on your web site.
Part I: Project setup
(You can start this when you come into class but the presenter will
go through it with you to get you started.)
- Create an eclipse project called lab03.
- All classes in this project will be in the balls package.
- Create a Ball class that represents a 2-dimensional ball.
- A Ball has a position (x and y coordinates of the center) that are of type double.
- A Ball has a radius (also a double).
- A Ball has a color.
- A Ball can be drawn. This means that there is a method:
public void draw(Graphics g)
that draws the ball. Use the
fillOval(int x, int y, int width, int height)
of the Graphics class to draw the ball.
You will have to take into account:
- fillOval uses the upper left corner of the bounding box,
not the center.
- You must convert the double values to ints for use in
- You must use the names x, y, and color
for the names of your Ball attributes.
- Create a class called BallsApplet that extends JApplet.
- Insert a comment near the top of the BallsApplet.java file
that contains your full name.
- Make an init method in BallsApplet that sets the size
to 400 by 500.
- Have the init method create a single green Ball at position
(30,30) and having radius 10.
- Make a
public void paint(Graphics g);
that calls the draw method of the Ball you created.
- Run the applet and make sure that it displays the ball.
Part II: Adding physics
- Get a copy of Physics.java.
You can download it from here.
Store it on your Z: drive (not in your project directory).
- Import Physics.java into your eclipse project:
- In the eclipse package explorer right click balls under
- Select Import
- Choose General -> File System -> Next
- Browse to your Z: drive and select the Physics.java
file you downloaded.
- Click OK, then Finish
- You should see Physics.java as one of the available java files
- Modify the Ball class as follows:
- add attributes velocityX and velocityY, both
doubles, initialized to 0.
- add getter and setter methods for x, y,
color, velocityX, and velocityY.
- add the method:
public boolean intersectsBall(Ball b);
For now, this method will always return false.
- add the method:
public void setCollided():
For now this method will do nothing.
- Run your applet again, and fix any errors.
- Set velocityX and velocityY of the ball you
created to 10.
- Add the following to the end of your init in your applet:
Physics physics = new Physics(this,this,10);
- Try running our applet again.
- You should see the ball leaving a trail of where it has been.
To remove this trail, add the following to be beginning of your
Rectangle bounds = getBounds();
On your own
Part III: More balls
- Write the method:
private int randomRange(int low, int high)
that returns a random integer between low and high,
- Write the following method in your applet:
private Ball makeRandomBall();
It creates and returns a green ball with random values for the
x, y, radius, velocityX,
The radius should be between 2 and 10.
x and y should be between 20 and 200.
The velocities should be between -40 and +40.
- Create an array of 20 random balls in your applet instead of
just one ball.
Add all of the balls to physics and modify paint
so that it draws all of the balls.
- Run an test your applet.
Part IV: Collisions
- Write the code for instersectsBall.
It should return true if the 2 balls intersect.
Two balls intersect if the distance between their centers is less than
the sum of their radii.
(You might want to google "Distance Formula" and choose the Wikipedia
You can avoid taking square roots by comparing the square of the
distance to the square of the sum of the radii.
- The Physics class will call the setCollided
method of balls that have undergone collisions.
Have this method turn the balls red.
You can be creative here.
In addition to setting the color to
you can modify the way the balls look in some other way.☺
- In your paint methods, keep track of the number of red and
green balls. You can tell if a ball is red by using:
- Have the paint method display the number of red and green
balls in the upper left corner of the applet.
Also display your name in the upper right corner above the counts.
- When there are no more green balls, have the paint method
call stopIt() in the Physics class. This will stop all motion.
Part V: Putting the project on the web
- Open an explorer window and migrate to your workspace directory.
- Go to the balls directory.
- Go to the bin directory.
- Open another explorer window and migrate to the public_html directory
of you Z: drive.
- Create a directory called lab3 and go into that directory.
- Drag the balls directory from
your-workspace\balls\bin\ into public_html\lab3.
- Run the applet from eclipse.
You should have a file whose name starts with
balls.ballsApplet in your
Drag this to public_html\lab3 also.
Note: this file may go away if you kill the applet.
- Start up the SSH Shell client
- Log into your account.
- If you have not already done so, get a copy of the allowall
program by typing:
- In a browser, go to:
- You should see the balls.ballsApplet file there.
- Click on it and this should start you applet in the browser.
- When run from a browser, the size of the applet is determined by
the parameters in the balls.ballsApplet file.
The default values are probably 200 by 200.
You can edit these to make the applet larger.
- Add a comment to the beginning of your source code for
BallsApplet giving the URL of your applet on the web.
It should be something like:
You should be able to run the applet by cutting and pasting this
into your browser.