Вы находитесь на странице: 1из 4

TCP1311 Object Oriented Programming Lab-6 Page 1

Lab 6
Problem Definition
The program has to draw a ball, which moves inside a frame, and bounces back into the
window when it hits any of the four boundaries.
Ball (x, y)

left right

Identification of Requirements
We need two classes:
1. Class “Ball” which will define the attributes (color, size, position, direction of
movement), and behavior (move, draw).
2. Class “BallWorld” which is an applet that will generate the display. The main
functions of this class are:
• Create ball object (Object of class Ball, described below).
• Define the frame boundaries.
• Display the current position of the ball object inside the frame.
• Update the display.

The structures of these two classes are given below.

Edit the following file, and save as Ball.java

import java.awt.*;
public class Ball{
private Color c; //Color
private int s; //Size
private int x, y; //Position
private int dx, dy; //Direction
public Ball(Color col, int posx, int posy, int siz, int movx, int
TCP1311 Object Oriented Programming Lab-6 Page 2

public void move(){
x+=dx; //Update x, y
if(x<BallWorld.left){ //Hit the left boundary

---complete the code including checks for other boundaries ---

public void draw(Graphics g){

Note how the attributes are defined. The constructor is used to initialize the variables
with the values supplied through the arguments.

The following figure explains the position and direction of movement of the ball.

Position (x, y)
s Ball
Direction of movement (dx, dy)
(x+dx, y+dy)
size = s

The direction of movement is indicated by two variables dx, dy. If (x, y) is the
current position, the next position is x+dx , y+ dy. Note that (x, y) is the upper left
corner position, and not the center. Depending on the direction, dx, dy may be either
positive or negative.
The function move() of Ball.java, is used to update the current position of the ball with
the direction of movement dx, dy. This is done as follows:
(a) Update x, y with dx, dy, using the formula x= x+dx; y=y+dy.
TCP1311 Object Oriented Programming Lab-6 Page 3

(b) If the ball hits one of the frame boundaries its direction of movement has to be
reversed. The frame boundaries (left, right, top, bottom) are defined as static
variables in the class BallWorld.java. Use these variables to detect if the ball has
crossed the frame boundary and to change the direction. An example is given

if(x < BallWorld.left){ //gone beyond left of frame.

x = BallWorld.left; //cannot go beyond the frame
dx = -dx; //reverse x-direction

(x, y) Gone beyond left boundary

Reverse x-direction of movement


Task 1: In the method move(), add similar statements as above to check the position of
the ball with respect to all the four frame boundaries.

The function draw(Graphics g) in Ball.java does the following.

(a) Move the ball to the next position (call move() )
(b) Draw the ball at the current position (set fill color to ‘c’, and draw the ball)

This being an applet has the display related functions. The structure of the applet is given
below. Edit the following file and save as BallWorld.java.

import java.awt.*;
import java.applet.*;
public class BallWorld extends Applet{
public static int left=50, right=350, top=50, bottom=350;
TCP1311 Object Oriented Programming Lab-6 Page 4

---Create a ball object “b” here ---

public void paint(Graphics g){

b.draw(g); //draw ball
slow(100); //slow the display
public void slow(int t){
}catch(Exception e){}

Task 2: In the BallWorld.java applet, first create a ball object.

The display is updated every 100 ms, by calling repaint();. This function in the applet
class will call the paint() method again, causing the display to be continuously updated.

Task 3: Compilation and Running

Compile both the files BallWorld.java, Ball.java.
Change the class name inside view.html to BallWorld.class
Use the applet viewer to run the applet.

Multiple Objects:
Once the above program is developed and tested to working order, do the following
modification to the class BallWorld.java:
Create several objects of the ball class inside the init() method, each ball having
independent set of attributes (color, size, initial position, initial direction of movement).
Modify the code in “BallWorld.java” appropriately to accommodate multiple objects.