/* Selles programmis saab näha põrkavaid palle. Vajab ka jupikest BouncingBall.java.*/ import java.awt.*; import java.awt.event.*; public class BouncingBallsApplet extends SimpleAnimationApplet implements MouseMotionListener, ActionListener{ BouncingBall redBall; BouncingBall bBall; BouncingBall cBall; BouncingBall dBall; BouncingBall eBall; Color colB = new Color((int)(Math.random()*240), (int)(Math.random()*240), (int)(Math.random()*240)); Color colC = new Color((int)(Math.random()*240), (int)(Math.random()*240), (int)(Math.random()*240)); Color colD = new Color((int)(Math.random()*240), (int)(Math.random()*240), (int)(Math.random()*240)); Color colE = new Color((int)(Math.random()*240), (int)(Math.random()*240), (int)(Math.random()*240)); public boolean red=true; public void init() { int w = getSize().width; // Applet laius, kasutatakse palli paika panemisel int h = getSize().height; // Applet kõrgus redBall = new BouncingBall(Color.red, w/2, h/2, 16); bBall = new BouncingBall(colB, w/2, h/2, 14); cBall = new BouncingBall(colC, w/2, h/2, 10); dBall = new BouncingBall(colD, w/2, h/2, 8); eBall = new BouncingBall(colE, w/2, h/2, 20); addMouseMotionListener(this); setMillisecondsPerFrame(50); // iga raami jaoks 50 msek setLayout(new BorderLayout()); Button p= new Button("Punane kao ära"); add(p, BorderLayout.SOUTH); p.addActionListener(this); validate(); } // end init() public void mouseMoved(MouseEvent evt) {} public void mouseDragged(MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); redBall.headTowards(x,y); bBall.headTowards(x,y); cBall.headTowards(x,y); dBall.headTowards(x,y); eBall.headTowards(x,y); } void setRed(boolean red1){red=red1;} //public void drawFrame(Graphics g, int width, int height) { // Kutsutakse välja iga uue raami joonistamisel, joonistab musta tausta public void drawFrame(Graphics g) { // kutsub välja doFrame meetodi int width=400; int height=400; g.setColor(Color.gray); g.fillRect(0,0,width,height); if (red) {redBall.doFrame(g,width,height);} bBall.doFrame(g,width,height); cBall.doFrame(g,width,height); dBall.doFrame(g,width,height); eBall.doFrame(g,width,height); // System.out.println("Joonistatakse"); } // end drawFrame() public void actionPerformed(ActionEvent e){ red=false; } public static void main(String argumendid[]){ Frame f=new Frame(); BouncingBallsApplet ap=new BouncingBallsApplet(); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.out.println("Programmi ots"); System.exit(0); } }); f.add(ap); f.setSize(400, 440); ap.init(); f.setVisible(true); ap.start(); } }// end class BouncingBallsApplet