// Theresa L. Walunas // SE 450, Tuesday Night Class // Homework #3: Animation Applet // Due: 4/18/00 import java.awt.*; public class FallingStar extends java.applet.Applet implements Runnable { protected Image image; protected Graphics offscreen; protected Dimension d; // Background color fields (taken from HTML) protected int bkgdr, bkgdg, bkgdb; // Printed text fields Font titlefont, caughtfont; String title,caught; // 3D Rectangle Fields protected int xrect, yrect; protected int dxrect = -2, dyrect= 5; protected int rgfh = 2, rgfw = 2; protected int rech, recw; // Polygon Fields protected int polypoints=5; protected int dx = 0, dy = 5, rnd, shift; protected int polyx[]= {12, 36, 6, 30, 18}; protected int polyy[]= {30, 12, 12, 30, 0}; protected Polygon p = new java.awt.Polygon (polyx, polyy, polypoints); protected Rectangle rp = new java.awt.Rectangle(); protected Rectangle r = new java.awt.Rectangle(); public void init() { // Parameterized background color set up String color = getParameter("bkgdr"); if (color != null){ bkgdr = Integer.parseInt(color); } else { bkgdr = 0; } color = getParameter("bkgdg"); if (color != null){ bkgdg = Integer.parseInt(color); } else { bkgdg = 0; } color = getParameter("bkgdb"); if (color != null){ bkgdb = Integer.parseInt(color); } else { bkgdb = 0; } // Parameterized frames per second String frames = getParameter("fps"); if (frames != null){ delay = 1000/Integer.parseInt(frames); } else { delay = 100; } // Initialize Text Settings titlefont = new Font("Helvetica", Font.BOLD, 24); caughtfont = new Font("Helvetica", Font.PLAIN, 12); title = "Catch a Falling Star"; caught = "Caught the Falling Star!"; // Determine size of applet space d = getSize(); // Create randomized dimensions for 3D rectangle xrect = d.width/2; yrect = d.height/2; rech = (int)(Math.random() * d.height * 0.25); recw = (int)(Math.random() * d.width * 0.25); r = new Rectangle(xrect,yrect,recw, rech); } public void update (Graphics g) { // Create randomized colors for shapes int red = (int)(Math.random() *256); int blue = (int)(Math.random() *256); int green = (int)(Math.random() *256); // Create off-screen image buffer if it is // invoked for the first time. if (image == null) { image = createImage (d.width, d.height); offscreen = image.getGraphics(); } // Draw the background offscreen.setColor(new Color(bkgdr, bkgdg, bkgdb)); offscreen.fillRect(0,0,d.width, d.height); // Print Text at top of Box offscreen.setColor(new Color (255-bkgdr, 255-bkgdg, 255-bkgdb)); offscreen.setFont(titlefont); FontMetrics fm = offscreen.getFontMetrics(); int titlewidth = fm.stringWidth(title); offscreen.drawString(title, (d.width-titlewidth)/2, titlefont.getSize()); offscreen.drawLine (0, titlefont.getSize() +10, d.width, titlefont.getSize()+10); // Motion for star polygon. When "falling star" // reaches bottom of applet box, restart at // random position along the top of the box. p.translate(dx,dy); rp = p.getBounds(); if (rp.y >= d.height){ rnd = (int)(Math.random() * (d.width/rp.width)); shift = rnd * rp.width; Polygon q = new Polygon(polyx, polyy, polypoints); q.translate(shift,titlefont.getSize()+20); p = q; } // Draw filled star Polygon offscreen.setColor(new Color (red,green,blue)); offscreen.fillPolygon(p); offscreen.setColor(new Color(255,255,255)); offscreen.drawPolygon(p); // Motion for 3DRectangle if (r.x < 10 || (r.x + r.width > d.width - 10)){ dxrect = -dxrect; rgfh = -rgfh; rgfw = -rgfw; } if (r.y < titlefont.getSize() + 20 || r.y + r.height > d.height - 10){ dyrect = -dyrect; rgfh = -rgfh; rgfw = -rgfw; } r.x += dxrect; r.y += dyrect; r.height += rgfh; r.width += rgfw; if (r.height < 5 || r.height > d.height *.25) { rgfh = -rgfh; r.height +=rgfh; } if (r.width < 5 || r.width > d.width *.25) { rgfw = -rgfw; r.width += rgfw; } // Draw 3DRectangle (outline) offscreen.setColor(new Color(red,green,blue)); offscreen.draw3DRect(r.x, r.y, r.width, r.height, true); // Copy offscreen image to the screen // If "falling star" caught by box, print out "caught" // phrase, draw inmage to screen and pause thread then // restart pathway of star from top at new position if ((rp.x>= r.x) && (rp.y >= r.y) && (rp.x + rp.width <= r.x + r.width) && (rp.y + rp.height <= r.y + r.height)){ rnd = (int)(Math.random() * (d.width/rp.width)); shift = rnd * rp.width; Polygon q = new Polygon(polyx, polyy, polypoints); q.translate(shift,0); p = q; offscreen.setColor(new Color (255-bkgdr, 255-bkgdg, 255-bkgdb)); offscreen.setFont(caughtfont); offscreen.drawString(caught, r.x, r.y+r.height+caughtfont.getSize()); g.drawImage (image, 0,0, this); try { Thread.currentThread().sleep(1000); } catch (InterruptedException e){} } else { g. drawImage (image, 0,0, this); } } public void paint (Graphics g){ update(g); } // The animation applet idiom protected Thread movingThread; protected int delay = 100; public void start(){ movingThread = new Thread(this); movingThread.start(); } public void stop(){ movingThread = null; } public void run(){ while (Thread.currentThread() == movingThread) { try { Thread.currentThread().sleep(delay); } catch (InterruptedException e){} repaint(); } } }