Академический Документы
Профессиональный Документы
Культура Документы
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.
top
Frame
Ball (x, y)
left right
bottom
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.
Ball.java
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
movy){
c=col;
TCP1311 Object Oriented Programming Lab-6 Page 2
x=posx;
y=posy;
s=siz;
dx=movx;
dy=movy;
}
public void move(){
x+=dx; //Update x, y
y+=dy;
if(x<BallWorld.left){ //Hit the left boundary
x=BallWorld.left;
dx=-dx;
}
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
below.
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.
BallWorld.java
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
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.