0% found this document useful (0 votes)
70 views3 pages

Ball - Java: TCP1311 Object Oriented Programming Lab-6

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 3

TCP1311 Object Oriented Programming Lab-6 Page 1

Lab 6
Ball.java

import java.awt.*;
import java.applet.*;
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;
x=posx;
y=posy;
s=siz;
dx=movx;
dy=movy;
}
public void move(){
x+=dx;
y+=dy;
if(x<BallWorld.left){
x=BallWorld.left;
dx=-dx;
}
if(x>BallWorld.right-s){
x=BallWorld.right-s;
dx=-dx;
}
if(y<BallWorld.top){
y=BallWorld.top;
TCP1311 Object Oriented Programming Lab-6 Page 2

dy=-dy;
}
if(y>BallWorld.bottom-s){
y=BallWorld.bottom-s;
dy=-dy;
}

}
public void draw(Graphics g){
move();
g.setColor(c);
g.fillOval(x,y,s,s);
}
}

BallWorld.java
import java.awt.*;

import java.applet.*;

public class BallWorld extends Applet{

public static int left=50, right=350, top=50, bottom=350;

Ball b1=new Ball(Color.red,100,200,80,10,15);

Ball b2=new Ball(Color.green,200,100,20,15,10);

Ball b3=new Ball(Color.magenta,250,150,15,6,10);

Ball b4=new Ball(Color.cyan,250,300,25,-6,-10);

public void paint(Graphics g){

g.setColor(Color.blue);

g.drawRect(left,top,right-left,bottom-top);
TCP1311 Object Oriented Programming Lab-6 Page 3

b1.draw(g); //draw ball 1

b2.draw(g); //draw ball 2

b3.draw(g); //draw ball 3

b4.draw(g); //draw ball 4

slow(100);

repaint();

public void slow(int t){

try{

Thread.sleep(t);

}catch(Exception e){}

You might also like