// CPS616 Assignment 2 // Mehmet SEN // Using threads to construct a applet, in which dynamically sizing // bubbles are moving. // HOW TO USE THREADS? // 1. Add the words "implements Runnable" to the applet class // 2. Define an instance variable to hold the applet's thread // 3. Modify start() method to spawn a thread and start it running. // 4. Create a run() method that contains the actual code that starts // your applet running // // In the following program Bubble class constructs a thread and // BubblesApplet class creates an array of independent threads using the // Bubble class. You can watch all four steps by looking at the Bubble // class to create an independent thread import java.awt.*; import java.lang.Math; import java.util.Random; public class BubblesApplet extends java.applet.Applet { public int W = 400; public int H = 300; public int N = 15; public final int minSize = 10; public final int maxSize = 50; public final int minNaptime = 1; public final int maxNaptime = 500; public final Color colors[] = { Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.magenta, Color.blue, Color.cyan, Color.white, Color.gray, Color.lightGray, Color.darkGray }; public final Color bg = Color.black; boolean threadSuspended = false; Image im; Graphics offscreen; Random rand; Bubble bubbles[] = new Bubble[N]; public void init() { Rectangle r=bounds(); W = r.width; H = r.height; resize(W, H); rand = new Random((long)System.currentTimeMillis()); try { im = createImage(W, H); offscreen = im.getGraphics(); } catch (Exception e) { offscreen = null; } } /* Create each bubble with some random parameters and start its thread */ public void start() { for (int i=0; i.5)?-2:2; dy = (Math.random()>.5)?-2:2; if (color == null) { color = Color.black; } current = previous = s; } /* main thread run method */ public void run() { thread.setPriority(Thread.MIN_PRIORITY+1); while (thread != null) { changed = true; previous = current; if (growing) { current += growthFactor; } else { current -= growthFactor; } if (current == size || current == 0) { growing = !growing; } try { thread.sleep(naptime); } catch (InterruptedException e) {}; applet.repaint(); } thread = null; } /* Here , thread is started */ public void start() { if (thread == null) { thread = new Thread(this, Integer.toString(threadNum++)); thread.start(); } } public void stop() { thread = null; } /* Erase last position and paint the new one */ public void paint(Graphics g) { int prevHalf = (int)(previous/2); int curHalf = (int)(current/2); g.setColor(bg); g.fillOval(ox-prevHalf, oy-prevHalf, previous, previous); checkboundary(); ox += dx; oy += dy; g.setColor(color); g.fillOval(ox-curHalf, oy-curHalf, current, current); } public void checkboundary() { int curHalf = (int)(current/2); int nx = ox+dx; /* caculate new location */ int ny = oy+dy; /* check if new location out of boundary */ if ( (nx < curHalf) || (nx+current >= rect.width+curHalf) ) dx = -dx; if ( (ny < curHalf) || (ny+current >= rect.height+curHalf) ) dy = -dy; // System.out.println("nx:"+nx+ " ny:"+ny + "cur:"+current+ " dy:"+dy +"rect.y "+rect.y+ " rect.height "+ rect.height+"rect.x "+rect.x+ " rect.width "+ rect.width); } } // end Bubble class