// Name of the file :: Clacker.java // created by :: Sridhar Kandukuri // Date :: 07/15/98 // Description :: This program makes use of the Color object of // java.awt class. // To compile :: javac Clacker.java // To execute :: appletviewer Clacker.html // The program simulates the Clacker balls. A set of 5 balls are hung // from a point. The first ball, when dropped from a distance hits the // second ball, the impact of which travels to the fifth which flies off // in the direction of impact. It then comes back to hit the fourth ball // the impact of which sets the first ball in motion import java.applet.Applet; import java.awt.*; public class Clacker extends Applet { private Graphics page; // the initial coordinates of the first and fifth balls private int xleft = 70; private int xright = 170; // the increment/decrement of the first and fifth balls // in the x and y directions private int ldx = 5; private int rdx = 0; private int ldy = 3; private int rdy = 0; // the initial heights of the balls are contained in these variables private int lheigth = 80; private int rheigth = 92; private int heigth = 100; public void init() { setVisible(true); page = getGraphics(); }// init public void start() { // select a background setBackground(Color.blue); // activate the XOR mode page.setXORMode (getBackground()); // color of one ball page.setColor(Color.red); // the thread and the ball page.drawLine(130,50,110,heigth-3); page.fillOval(100,heigth-3,20,20); // create a new Color, using another form of Color constructor Color GREEN = new Color(0,255,0); page.setColor(GREEN); // the thread and the ball page.drawLine(130,50,130,heigth); page.fillOval(120,heigth,20,20); // create a new Color using the float parameters in the // constructor Color YELLOW = new Color(0,0.5f,0.5f); page.setColor(YELLOW); // the thread and the ball page.drawLine(130,50,150,heigth-3); page.fillOval(140,heigth-3,20,20); // the above 3 balls are the middle balls // since they do not change position - they need not be // redrawn again and again // infinite loop. // keep drawing balls do { draw_balls(); }while (true); } // the delay funtion, which delays the systems for some time // so that the effect of movement of the balls can be seen public void delay() { int isleep = 200; try { Thread.sleep(isleep); } catch (Exception e) { return; } } public void draw_balls() { // create a new Color using the integer form of the constructor Color brown = new Color(107,69,38); page.setColor(brown); // the thread and the ball page.drawLine(130,50,xleft+5,lheigth); page.fillOval(xleft-10,lheigth,20,20); // create another color using the integer form of the // constructor - the bits in the integer represent the // red, green and blue values of the color Color new_color = new Color(123456); page.setColor(new_color); // the thread and the ball page.drawLine(130,50,xright-5,rheigth); page.fillOval(xright-10,rheigth,20,20); // on hitting the second ball // change the direction of the fifth ball // in both the dimensions if ( (xleft==90) && (ldx==5) ) { ldx = 0; rdx = 5; ldy = 0; rdy = -3; } // when the first ball reaches the left limit, // change its direction in both dimensions if ( (xleft==70) && (ldx==-5) ) { ldx = 5; ldy = 3; } // when the fifth ball hits the fourth ball // change the direction of the first ball // in both directions if ( (xright==170) && (rdx==-5) ) { ldx = -5; rdx = 0; ldy = -3; rdy = 0; } // when the fifth ball reaches the right limit // change its direction in both the directions if ( (xright==190) && (rdx==5) ) { rdx = -5; rdy = 3; } // call delay - so that the new position of the balls is seen delay(); // erase the balls by over writing them again // this when done in the XOR mode accomplishes the tast page.setColor(brown); page.drawLine(130,50,xleft+5,lheigth); page.fillOval(xleft-10,lheigth,20,20); page.setColor(new_color); page.drawLine(130,50,xright-5,rheigth); page.fillOval(xright-10,rheigth,20,20); // update the values of the new postion of the balls xleft += ldx; xright += rdx; lheigth += ldy; rheigth += rdy; }//end draw_balls // end class Clacker }