// Luke Rogers // ID: 9320530 // ME 498/599 Computer Graphics - M.A. Ganter // // Please note to use this program you will need WindowListener.class in the // same subdirectory (folder) as this code. // // Draws randomly placed rectangles on the screen and fills them with a gradient // import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import WindowListener.*; import java.awt.Component.*; // A simple program to demonstrate simple Java graphics. class Randomizer extends Canvas { static int theFrameWidth; static int theFrameHeight; static int numRects; static int theMaxSize = 0; static Frame theFrame = new Frame(); // This is the paint method. It is called inside of the "show" method. public void paint(Graphics theGraphic) { // Cast into the graphics 2D world Graphics2D the2DGraphic = (Graphics2D) theGraphic; for (int i = 1; i < numRects; i++) { // Get the current size of the frame Dimension theCanvasSize = theFrame.getSize(); int theCanvasWidth = theCanvasSize.width; int theCanvasHeight = theCanvasSize.height; // Get random location and size for the object double rect_x = Math.random() * theCanvasWidth; double rect_y = Math.random()* theCanvasHeight; double rect_height = Math.random() * theMaxSize; double rect_width = Math.random() * theMaxSize; // Set the random rotation angle double rotation = Math.random(); // Create points for gradient paint Point2D theFirstPoint = new Point2D.Double(rect_x,rect_y); Point2D theSecondPoint = new Point2D.Double(rect_x + rect_width,rect_y + rect_height); // generate the random color integers int r1 = (int)Math.rint(Math.random() * 255); int g1 = (int)Math.rint(Math.random() * 255); int b1 = (int)Math.rint(Math.random() * 255); int r2 = (int)Math.rint(Math.random() * 255); int g2 = (int)Math.rint(Math.random() * 255); int b2 = (int)Math.rint(Math.random() * 255); // set the random colors Color theFirstColor = new Color(r1,g1,b1); Color theSecondColor = new Color(r2,g2,b2); // make the gradient paint object GradientPaint theGradient = new GradientPaint(theFirstPoint,theFirstColor,theSecondPoint,theSecondColor); // make the rectangle Rectangle2D theRect = new Rectangle2D.Double(rect_x,rect_y,rect_width,rect_height); // set the gradient paint object on the rectangle the2DGraphic.setPaint(theGradient); the2DGraphic.fill(theRect); // Create an affine transformation. AffineTransform theTransformation = new AffineTransform(); // set the affine transformation to rotate the rectangle theTransformation.rotate(rotation,rect_x,rect_y); // apply the transformation to the scene which also draws the object. the2DGraphic.setTransform(theTransformation); } } // takes input from the console which is the number of rectangles to draw public static void main(String[] args) { // Check for input, if none return usage try { String arg0 = new String(args[0]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Usage: Randomizer <#_rectangles> {max_rect_size}"); System.exit(0); } // make sure the first arg is is an integer try { numRects = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.out.println(args[0] + " is not a valid #_rectangles integer!"); System.exit(0); } // If there is no second argument then catch the exception try { String arg1 = new String(args[1]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("No max_rect_size entered..."); System.out.println("Defaulting to 100"); // Set the Max Size to a default of 100 theMaxSize = 100; } // make sure the second arg is is an integer if (theMaxSize != 100) { try { theMaxSize = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.out.println(args[1] + " is not a valid max_rect_size integer!"); System.exit(0); } } // set the frame dimensions to the max screen size Dimension theScreenDimensions = Toolkit.getDefaultToolkit().getScreenSize(); theFrameWidth = theScreenDimensions.width; theFrameHeight = theScreenDimensions.height; // Or set the frame dimensions using integers //int theFrameWidth = 1024; //int theFrameHeight = 768; // creat a listener to provide "window close" support WindowListener theWindow = new WindowListener(); // create a new Randomizer object (note: it is a canvas in reality) Randomizer theCanvas = new Randomizer(); // create a frame and provide a title //Frame theFrame = new Frame(); // set the frame title theFrame.setTitle("Luke's Random Rectangle Generator"); // add the listener to the frame theFrame.addWindowListener(theWindow); // add the canvas (Randomizer) to the frame in the center theFrame.add(theCanvas, BorderLayout.CENTER); theFrame.pack(); // set the frame's size theFrame.setLocation(theScreenDimensions.width/2 - theFrameWidth/2, theScreenDimensions.height/2 - theFrameHeight/2); theFrame.setSize(theFrameWidth,theFrameHeight); // show it (note: show calls method paint amongst other things) theFrame.show(); } }