Week 5 Programming Assignment
Write a Screen Saver program that generates shapes of random colors and sizes at random positions within the display area. Add controls so typing different keys on the keyboard control which shape is being randomly drawn, and which cause the window to change size and background color as described below.
1. You can NOT use loops of any kind in this program.
2. Your member variables must be private.
3. Your screen saver class must inherit from the JPanel class and it must have a JFrame member variable.
4. Your program must keep track of the number of shapes it draws on the screen and after 25 shapes have been drawn, it must clear the screen. This is most easily done by calling the base class (JPanel’s) paintComponent method.
5. You should use the Timer object to generate ActionEvents to drive your Screen Saver. Set the timer to fire an ActionEvent once every 300 milliseconds. This will cause your actionPerformed method execute, which should simply call the repaint method. Calling the repaint method causes your paintComponent method to execute.
6. Your paintComponent method will do all the work to control what is being drawn on the screen. It keeps a counter (member variable) of how many shapes have been drawn on the screen and when the counter hits 25, it calls the base class paintComponent method to erase the display area.
7. Your program will keep a drawingState (member variable). Your paintComponent method will use this to decide which shape is to be drawn on the screen. The states and the shape to be drawn in that state are listed below.
8. Your program MUST HAVE 4 private methods, one for each of the 4 different drawing states. Each of the methods takes a Graphics object as a parameter. The 4 different methods are drawOval, drawRectangle, drawLine, drawArc. When called, each method will generate random position values, random width and height values, and any other random values needed to draw that specific shape. These methods are called from your paintComponent method depending on which drawing state has been selected from the keyboard – explained later. For example, if the drawing state is 1, then paintComponent calls the drawOval method.
9. Each of the 4 drawing methods will need to get a random Color object that determines the color to be used for drawing. This must be done by calling another private method, getRandomColor, that returns a Color object whenever called. This method should generate three random numbers between 1 and 255 and create a Color object using those three random values. This new random color object is returned by this method. This method will also be called from your KeyListener event handler as described below.
10. Make your screen saver the full size of the screen. The technique for how to do this is shown in the code below. A member variable will need to be added to keep track of whether the display area is currently full size or half size (more later).
11. The size and position of the shapes that you draw must vary based on the size of the screen. At least part of each shape drawn must be visible on the screen. The starting coordinates for each shape should be a random number between 1 and half of the current screen width and height. This guarantees that at least part of your shape will be visible. Your random length and width values should also be scaled by the current length and width of the display.
12. You will need to write an event handler inner class that extends the KeyAdapter class. You will need to add the keyPressed method to handle KeyPressed events.
13. Your event handler class must call System.exit when the ‘x’ key is pressed.
14. When the ‘s’ key is pressed, your event handler must change the size of the window from full screen size to half size (half the width and height of the screen), or from half size back to full size. That is, each time the ‘s’ key is pressed the size of the display toggles between full and half size. This requires changing the size of the frame.
15. When the ‘c’ key is pressed, your event handler must change the color of the background of the display to a random color. This means it must get a new random color by calling the getRandomColor method and then call the panel’s setBackground method with the new color object.
16. The shape to be drawn is controlled by from the keyboard. The following keys control the drawing state:
‘o’ 1 – ovals
‘r’ 2 – rectangles
‘l’ 3 – lines
‘a’ 4 – arcs
Whenever one of these keys is pressed, the drawing state should be changed.
17. Whenever ANY of these 6 keys are pressed (all but the ‘x’), the counter variable should be set so that the next time paintComponent runs, it will erase the screen, and the repaint method should be called.
The following example program illustrates how to create a frame the size of the screen and how to kill the program when any key is pressed.
public class FullSize extends JPanel
private JFrame frame = new JFrame(“FullSize”);
private Rectangle rectangle;
// Remove the title bar, min, max, close stuff
// Add a Key Listener to the frame
// Add this panel object to the frame
// Get the dimensions of the screen
rectangle = GraphicsEnvironment.getLocalGraphicsEnvironment()
// Set the size of the frame to the size of the screen
// Remember that we are currently at full size
full = true;
// This method will run when any key is pressed in the window
class KeyHandler extends KeyAdapter
public void keyPressed(KeyEvent e)
// Terminate the program.
public static void main(String args)
FullSize obj = new FullSize();
Take a screen shot of the output of this program. Paste the screen shot and your source code for your program into a Word document. Submit the Word document to the drop box.