HW 5 Assignment #5 Due: Wed, June 29 Objective This assignment will help you gain basic practice with Graphics and Java2D classes. Task Do the following 3 exercises, each involving the use of painting with Graphics and/or Graphics2D class features. Note that each of these should be set up like the examples we saw in class -- the drawings should be done on a custom JPanel that is embedded as the content area of a JFrame. Filenames should be Triangles.java Snowman.java ScreenSaver.java Instructions Triangles.java -- Write an application that draws random triangles. Write your application so that the drawing is done on a JPanel, embedded in a JFrame with starting size 500 x 500. (Note that JFrames can be resized by the user, just like normal windows in standard computer applications). On your JPanel, you will draw 5 random triangles, each filled with a randomly chosen color. To choose a random color, choose each of the RGB values randomly (range 0-255). Use class GeneralPath to create the triangles, and method fill from class Graphics2D to draw them. The triangle endpoints should be chosen chosen so that they could be any valid points within the JPanel. Note that this is not always in a range of 500 x 500 -- if the application frame is expanded, the panel area will be larger. Do your drawing in method paintComponent. (Notice that this means that refreshes to the panel will result in new random trangles. For example, resize the window and the internal components will be redrawn, because paintComponent will be called again). Snowman.java: Write an application that draws a snowman. The snowman's exact size and position will be based on given proportions and centered in the current JPanel. Details: Build your application as a JPanel embedded in a JFrame. Set the frame's starting size as 600 x 400. The snowman is drawn on the JPanel The main body of the snowman should consist of three circles, sitting on top of each other The bottom circle takes up half of the snowman's height. The middle and top circle take up 2/3 and 1/3 of the remaining height, respectively Each of these circles is drawn with a random color The full snowman should take up 3/4 of the panel's height, and it should be centered horizontally and vertically in the panel. (Note: This means the exact locations and sizes of circles will change if the panel is resized) The snowman should have two eyes (black) drawn in the top circle, aligned proportionally somewhere near the top of the head (i.e. where eyes would naturally go) The snowman should have two arms (black) drawn coming out of the top part of the middle circle. They should just be lines that stick out from the middle "body" section, angled upwards. Draw these so that they adjust proportionally as the window is resized. (They should always fully appear in the actual panel!) NO portion of the drawing should ever leave the viewable area of the panel (provided that the width is enough, based on the given height -- note that the size of the snowman is mandated by the height of the drawing area). Sample screenshots, based on different window sizes: Screenshot 1 Screenshot 2 ScreenSaver.java: Write an application that simulates a screen saver. The application will randomly draw 50 ovals to the screen (various sizes and locations), then it should refresh every 1 second. For the drawing of the ovals, follow these rules: Draw on a JPanel imbedded in a JFrame, as before, and set the starting frame size to 800 x 600. Each run through paintComponent should draw 50 random ovals, which must show up within the JPanel boundaries For each oval, pick a random color For each oval, pick a random paint brush thickness between 1 and 10 For each oval, pick a random location within the panel, and a random size (height/width). Note that the chosen height/width need to ensure that the oval remains WITHIN the current JPanel. i.e. your ovals should be fully viewable on the currently shown panel (the only exception is that your ovals might slightly overlap the panel boundaries due to paintbrush thickness) For the refresh of the screen, you'll need to use class javax.swing.Timer. This class sets up a timer event so that the actionPerformed gets called after a given fixed time interval. In your actionPerformed method, you can simply call repaint() on your panel, and a new set of ovals will be drawn. See this example for an illustration on the usage of the Timer class: Bouncing Ball General Any member data (static and instance variables) of classes you write should be private Our in-class examples illustrated usage of several Java2D and drawing concepts, such as the libraries GeneralPath, Color, JPanel inside a JFrame, paintbrush/line thickness settings, drawing various shapes -- make sure to refer to these, and the related libraries in the Java API! Other functions you will find helpful are getHeight() and getWidth(), part of every Swing component (inherited from class JComponent) Make sure your code is appropriately documented and easy to read Submitting: Pack up your files (including source code, as always) into a single jar-file called "hw5.jar". Since this contains three programs in it, you do NOT need to make it runnable. Just make sure that when it's unpacked, the following commands will run the executable programs:
java Triangles
java Snowman
java ScreenSaver
As usual, submit your jar file through the Blackboard submission link.