import java.applet.*; import java.awt.*; //import java.io.IOException; import java.lang.Runnable; @SuppressWarnings("serial") public class bouncingBall extends Applet implements Runnable { Graphics k; Image bufferscreen; int DELAYBALL = (int)((Math.random()*5)+5); int x,y,x1,y1; int dx=4; int dy=4; int dx1=4; int dy1=4; int radius = 49; int radius1 = 49; public void init() { this.setSize(800, 600); setBackground(Color.decode("0x0009F0")); bufferscreen = createImage(800,600); k = bufferscreen.getGraphics(); y = (radius + ((int)(Math.random()*(301-radius)))); x = (radius + ((int)(Math.random()*(401-radius)))); y1 = (radius1 + ((int)((Math.random()*(301-radius1)+300)))); x1 = (radius1 + ((int)((Math.random()*(401-radius1)+400)))); } public void run() // will run this thread { while (true) { repaint(); // ask JVM to call paint. sleeper(); move(); } } // run private void sleeper() { try { Thread.sleep(DELAYBALL); // requires a catch or throw } catch (InterruptedException e) { System.out.println("problem is " + e.getMessage()); } } public void start() { System.out.println(" start "); Thread myThread = new Thread(this); myThread.start(); System.out.println(" hello world "); } // start private void move() // rules for movement { int w = this.getWidth(); // get new window size int h = this.getHeight(); if ( x> (w-radius) || x (w-radius1) || x1(h-radius) || y(h-radius1) || y1x1 && y>y1 && dx>0 && dy<0 && dx1<0 && dy1>0) { System.out.println("TRUTH"); dx = -dx; dy = -dy; dx1 = -dx1; dy1 = -dy1; lastresort = false; } if (lastresort) { System.out.println("UNEXPECTED"); dx = -dx; dy = -dy; dx1 = -dx1; dy1 = -dy1; lastresort = false; } // watch x and y change }}// end move // The JVM calls paint. public void paint(Graphics g) { try { k.setColor(Color.decode("0x0F0F"+((int)(Math.random()*100)))); } catch (NumberFormatException e) { System.out.println("problem is " + e.getMessage()); } //k.fillOval(((x-radius)-dx), ((y-radius)-dx), 2*radius, 2*radius); //k.setColor(Color.CYAN); k.fillOval(x-radius, y-radius, 2*radius, 2*radius); k.setColor(Color.RED); k.fillOval(x, y, 5, 5); k.setColor(Color.decode("0xFF"+((int)(Math.random()*100))+"00")); //k.fillOval(((x1-radius1)-dx1), ((y1-radius1)-dx1), 2*radius1, 2*radius1); //k.setColor(Color.ORANGE); k.fillOval(x1-radius1, y1-radius1, 2*radius1, 2*radius1); k.setColor(Color.RED); k.fillOval(x1, y1, 5, 5); g.drawImage(bufferscreen,0,0,this); } // paint public void update(Graphics g) { paint(g); } } // applet