import java.applet.Applet; import java.applet.*; import java.awt.*; public class manyball extends Applet implements Runnable { private Color background; private Color foreground; private boolean running; public Image image; public Graphics offscreen; public Thread runner; private boolean bump; //border sizes static final int startX=10; static final int startY=10; private int maxX; private int maxY; //sounds AudioClip beep; AudioClip hurt; AudioClip gong; static final int maxBalls=4; ball b[]; public void init() { //allocate balls b = new ball[maxBalls]; b[0] = new ball(20,150,Color.red,20,2,2); b[1] = new ball(32,78,Color.blue,35,1,-3); b[2] = new ball(223,218,Color.green,40,-3,-2); b[3] = new ball(10,15,Color.gray,25,-3,-1); background = Color.black; foreground = Color.black; //find screen size maxX=size().width; maxY=size().height; //create offscreen image image=createImage(maxX,maxY); offscreen=image.getGraphics(); Font f=new Font("Dialog",Font.BOLD,10); offscreen.setFont(f); offscreen.drawString("Bouncing Balls 3.0",9,10); //sounds beep=getAudioClip(getDocumentBase(),"beep.au"); hurt=getAudioClip(getDocumentBase(),"hurts.au"); gong=getAudioClip(getDocumentBase(),"chirp1.au"); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { offscreen.setColor(background); offscreen.fillRect(startX,startY,maxX-startX,maxY-startY); bump=false; for (int i=0; i=x3) && (x4>=x1) && (y2>=y3) && (y4>=y1)) { bump=true; } } public void newPath (int i, int j) { int temp; b[i].moveX*=-1; b[i].xPos+=b[i].moveX; temp=b[i].moveX; b[i].moveX=b[i].moveY; b[i].moveY=temp; b[i].yPos+=b[i].moveY; b[i].drawBall(); b[j].moveX*=-1; b[j].xPos+=b[j].moveX; temp=b[j].moveX; b[j].moveX=b[j].moveY; b[j].moveY=temp; b[j].yPos+=b[j].moveY; b[j].drawBall(); if (gong!=null) { gong.play(); } bump=false; } class ball { //instance variables int xPos; int yPos; Color ballColor; int moveX; int moveY; int diameter; //constructor public ball (int xPos, int yPos, Color ballColor,int diameter, int moveX, int moveY) { this.xPos=xPos; this.yPos=yPos; this.ballColor=ballColor; this.diameter=diameter; this.moveX=moveX; this.moveY=moveY; } //calculate positions and draw ball to offscreen image public void drawBall() { if (xPos+moveXmaxX) { moveX*=-1; if (beep!=null) { beep.play(); } if (xPos+moveXmaxX) xPos=maxX-diameter; } else if (yPos+moveYmaxY) { moveY*=-1; if (hurt!=null) { hurt.play(); } if (yPos+moveYmaxY) yPos=maxY-diameter; } xPos+=moveX; yPos+=moveY; offscreen.setColor(ballColor); offscreen.fillOval(xPos,yPos,diameter,diameter); } } }