// Theresa Walunas // SE 450 Section 303, Tuesday 5:45 - 9:00 pm // Assignment #4, Due Date 5/09/00 // Source File Name: FallingStarCanvas.java Class File: FallingStarCanvas.class // Adding GUI Widgets to Class developed in Assigment#3: This file // extends the canvas class to create a canvas containing the animation // developed in FallingStar.class. No major modifications of that class have been // made with respect to object motion. Several methods have been added, to allow // certain aspects of the animation to be manipulated. They are: // // void setStarColor which takes a Color object and sets the starcolor field to that object // void setBckGrndColor which takes a Color object and sets the background. It also modifies // the color the star object is outlined in so that the outline remains // visible // void setRectPosition which takes a Point object and moves the top-left corner of the // 3D rectangle to that position. // // In this animation, the color for the star is no longer random. It is intended that the // star color will be controlled via a GUI interface. import java.awt.*; public class FallingStarCanvas extends Canvas { // Initial field settings Image image; Graphics offscreen; Dimension d; // Background color Color bckgrnd = Color.black; // Text fields Font titlefont, caughtfont; String title, caught; Color fontcolor = Color.white; // 3D Rectangle Fields int xrect, yrect; int dxrect = -2, dyrect = 5; int rgfh = 2, rgfw = 2; int rech, recw; Rectangle r = new Rectangle(); // Polygon Fields int polypoints = 5; int dx = 0, dy = 5, rnd, shift; int polyx[] = {12, 36, 6, 30, 18}; int polyy[] = {30, 12, 12, 30, 0}; Polygon p = new Polygon(polyx, polyy, polypoints); Rectangle rp = new Rectangle(); Color starcolor = Color.yellow; Color staroutline = Color.white; public void init(){ // 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 start dimensions of 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 offscreen image buffer if invoked for the first time if (image == null){ image = createImage (d.width, d.height); offscreen = image.getGraphics(); } // Draw the background offscreen.setColor(bckgrnd); offscreen.fillRect(0,0,d.width, d.height); //Invoke star motion 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 Star offscreen.setColor (starcolor); offscreen.fillPolygon(p); offscreen.setColor (staroutline); offscreen.drawPolygon(p); // Invoke rectangle motion 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 rectangle motion 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(fontcolor); 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); } public void setStarColor (Color c){ starcolor = c; } public void setBckGrndColor (Color c) { bckgrnd = c; if (Color.black.equals(c)){ staroutline = Color.white; } else { staroutline = Color.blue; } fontcolor = (new Color(255-c.getRed(), 255 - c.getGreen(), 255 - c.getBlue())); } public void setRectPosition (Point newposition){ r.x = newposition.x; r.y = newposition.y; } }