/* This applet shows a red ball bounding around on a black backgound. It is based on the animation framework from SimpleAnimationApplet.java. It also requires the class BoundingBall, which is defined in the file BouncingBall.java. David Eck, 10 October 1998 */ import java.awt.*; import java.awt.event.*; public class BouncingBallsApplet3 extends SimpleAnimationApplet implements MouseMotionListener { BouncingBall[] balls; //pallide massiivi deklarerimine static final int ballCount = 25; //pallide arv Color mycolor; //pallide erinevate värvi jaoks public void init() { // This routine is called by the system when the applet if // first created. In this applet, it creates the red ball // and calls setMillisecondsPerFrame to set the speed of the // animation. addMouseMotionListener(this); //hiire liikumise edasiandmine int w = getSize().width; // Applet width, to be used for positioning the ball. int h = getSize().height; // Applet height, to be used for positioning the ball. balls = new BouncingBall[ballCount]; //uus pall massiivis for (int i = 0; i < ballCount; i++){ int r=(int)(256*Math.random()); //suvalise värvi tekitamiseks int g=(int)(256*Math.random()); //... int b=(int)(256*Math.random()); //... mycolor=new Color(r,g,b); // uus värv balls[i] = new BouncingBall(mycolor,w/2,h/2,10); //i-nda palli olek } setMillisecondsPerFrame(50); // This routine is defined in SimpleAnimationApplet. // It says that I want each frame to take 50 // milliseconds (1/20 second). This is about // twice as fast as the default rate. (Note that // there is no guarantee that the computer can // actally draw frames at the rate you request.) } // end init() public void drawFrame(Graphics g, int width, int height) { // This routine is called by the animation framework every time // a new frame needs to be drawn. For this animation, this // routine draws a black background, then calls the ball's // doFrame method, which draws the ball and updates its state // for the next frame. g.setColor(Color.white); // Fill the applet with a black background. g.fillRect(0,0,width,height); for (int i = 0; i < ballCount; i++) balls[i].doFrame(g,width,height); //i-nda palli joonistamine } // end drawFrame() public void mouseDragged(MouseEvent evt) { int x = evt.getX(); // hiire asukoht int y = evt.getY(); for (int i = 0; i < ballCount; i++) balls[i].headTowards(x,y); //i-nda palli hiire poole suundumine } public void mouseMoved(MouseEvent evt) {} // kuna hiire kursori asukohta pole vaja // siis on tühi. Olemas on sellepärast, et // MouseMotionListener nõuab. } // end class BouncingBallsApplet