Assignment #5

Due: Tues, Nov 5

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

Instructions

  1. 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).
     

  2. 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: Sample screenshots, based on different window sizes:
     
  3. 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:

    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
     

  4. General

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 Canvas submission link.