// Project Obstacle
// Term project CIS 525
// Fall 2001
// BouncingBalls.java
// Ashish Rungta
import java.awt.*;
import java.applet.*;
import java.util.*;
class Obstacle
{
public Rectangle r;
Graphics g;
public Obstacle(int x,int y,int w,int h)
{
r=new Rectangle(x,y,w,h);
}
public void paint(Graphics gr)
{
g=gr;
g.setColor(Color.red);
g.draw3DRect(r.x,r.y,r.width,r.height,true);
}
}
class CollideBall
{
int width, height;
public static final int diameter=20;
//coordinates and value of increment
double x, y, xinc, yinc, coll_x, coll_y;
boolean collide;
Color color;
Graphics g;
Rectangle r;
AudioClip hitObstacle;
//the constructor
public CollideBall(int w, int h, int x, int y, double xinc,
double yinc, Color c, AudioClip hitObstacle)
{
width=w;
height=h;
this.x=x;
this.y=y;
this.xinc=xinc;
this.yinc=yinc;
this.hitObstacle=hitObstacle;
color=c;
r=new Rectangle(150,80,130,90);
}
public double getCenterX() {return x+diameter/2;}
public double getCenterY() {return y+diameter/2;}
public void alterRect(int x, int y, int w, int h)
{
r.move(x,y);
r.resize(w,h);
}
public void move()
{
if (collide)
{
double xvect=coll_x-getCenterX();
double yvect=coll_y-getCenterY();
if((xinc>0 && xvect>0) || (xinc<0 && xvect<0))
xinc=-xinc;
if((yinc>0 && yvect>0) || (yinc<0 && yvect<0))
yinc=-yinc;
collide=false;
}
x+=xinc;
y+=yinc;
//when the ball bumps against a boundary, it bounces off
if(x<6 || x>width-diameter)
{
xinc=-xinc;
x+=xinc;
}
if(y<6 || y>height-diameter)
{
yinc=-yinc;
y+=yinc;
}
//cast ball coordinates to integers
int x=(int)this.x;
int y=(int)this.y;
//bounce off the obstacle
//left border
if(x>r.x-diameter&&x0&&y>r.y-diameter&&yr.x+r.width-7&&xinc<0&&y>r.y-diameter&&yr.y-diameter&&y0&&x>r.x-diameter&&xr.y+r.height-7&&yinc<0&&x>r.x-diameter&&xo.r.x-2&&xo.r.y&&yo.r.x+o.r.width-2&&xo.r.y&&yo.r.y-2&&x>o.r.x&&xo.r.y+o.r.height-2&&ybounds.width)x=bounds.width;
if(y>bounds.height)y=bounds.height;
if(drag)
{
//disallow to move past border
int ox=endx+xtemp;
int oy=endy+ytemp;
if(ox<5)ox=5;
if(oy<5)oy=5;
if(ox>bounds.width-o.r.width)
ox=bounds.width-o.r.width;
if(oy>bounds.height-o.r.height)
oy=bounds.height-o.r.height;
o.r.move(ox,oy);
west=o.r.x;
north=o.r.y;
east=o.r.x+o.r.width;
south=o.r.y+o.r.height;
}
else
{
if(shiftNW){west=x;north=y;}
else if(shiftNE){east=x;north=y;}
else if(shiftSW){west=x;south=y;}
else if(shiftSE){east=x;south=y;}
else if(shiftW)west=x;
else if(shiftE)east=x;
else if(shiftN)north=y;
else if(shiftS)south=y;
//disallow resizing below 40*40
int MIN=40;
if(east-west < MIN)
{
//shiftNW=shiftNE=shiftSW=shiftSE=shiftW=shiftE=false;
if(shiftW||shiftNW||shiftSW)
west=east-MIN;
if(shiftE||shiftNE||shiftSE)
east=west+MIN;
}
if(south-north < MIN)
{
//shiftNW=shiftNE=shiftSW=shiftSE=shiftN=shiftS=false;
if(shiftN||shiftNW||shiftNE)
north=south-MIN;
if(shiftS||shiftSE||shiftSW)
south=north+MIN;
}
}
//report altering of obstacle to ball objects and obstacle
for(int i=0;i < MAX;i++)
ball[i].alterRect(o.r.x,o.r.y,o.r.width,o.r.height);
o.r.move(west,north);
o.r.resize(east-west, south-north);
changeCursor(x,y);
return true;
}
// The two balls collision
private void handleCollision()
{
//we iterate through all the balls, checking for collision
for(int i=0;i < MAX;i++)
for(int j=0;j < MAX;j++)
{
if(i!=j)
{
if(collide(ball[i], ball[j]))
{
ball[i].hit(ball[j]);
ball[j].hit(ball[i]);
collision.play();
}
}
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
gBuffer.setColor(Color.lightGray);
gBuffer.fillRect(0,0,size().width,size().height);
gBuffer.draw3DRect(5,5,size().width-10,size().height-10,false);
//paint the obstacle rectangle
o.paint(gBuffer);
//paint our balls
for(int i=0;i < MAX;i++)
ball[i].paint(gBuffer);
g.drawImage (Buffer,0,0, this);
}
}