Write an application that mimics the behavior of a screen saver. It should draw random shapes onto a black background and the shapes should build up on top of each other until the screen saver resets (every 30 seconds). You have been provided with a Screen Saver application that does not yet display outlined shapes. It uses the MyRectangle and MyOval classes that you created in this tutorial. Add the code that will display random outlined shapes in your output. Your output should look like Fig. 27.34.
a) Copying the template to your working directory. Copy the C:Examples Tutorial27ExercisesAdvancedScreenSaver directory to your C:SimplyJava directory.
b) Opening the template file. Open the MyRectangle.java file in your text editor.
c) Adding an instance variable to the MyRectangle class. At line 7, add a comment indicating that the instance variable is a boolean and will indicate whether or not the rectangle is filled. At line 8, add a private instance variable named filled of type boolean.
d) Modifying the MyRectangle constructor. You will now modify the MyRectangle constructor so that it can accept an additional boolean argument. At line 12, add a boolean argument named fill to the end of the parameter list. At line 16, set the instance variable filled equal to the value of parameter fill and on the same line, add a comment indicating that filled will specify if the shape will be filled.
e) Modifying the draw method. At line 31, add a comment indicating that an if statem
```
1 // ScreenSaver.java
2 // Application simulates a Screen Saver by drawing random shapes.
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class ScreenSaver extends JFrame
7 {
8 // DrawJPanel for displaying shapes
9 private DrawJPanel shapesDrawJPanel;
10
11 // no-argument constructor
12 public ScreenSaver()
13 {
14 createUserInterface();
15 }
16
17 // create and position GUI components; register event handlers
18 private void createUserInterface()
19 {
20 // get content pane for attaching GUI components
21 Container contentPane = getContentPane();
22
23 // enable explicit positioning of GUI components
24 contentPane.setLayout( null );
25
26 // set up shapesDrawJPanel
27 shapesDrawJPanel = new DrawJPanel();
28 shapesDrawJPanel.setBounds( 0, 0, 600, 450 );
29 contentPane.add( shapesDrawJPanel );
30
31 // set properties of application's window
32 setTitle( "Screen Saver" ); // set title bar string
33 setSize( 600, 450 ); // set window size
34 setVisible( true ); // display window
35
36 } // end method createUserInterface
37
38 // main method
39 public static void main( String[] args )
40 {
41 ScreenSaver application = new ScreenSaver();
42 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
43
44 } // end method main
45
46 } // end class ScreenSaver
```
```
1 // DrawJPanel.java
2 // JPanel that allows shapes to be drawn on.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.Random;
8 import javax.swing.*;
9
10 public class DrawJPanel extends JPanel
11 {
12 // Timer to fire when another shape should be drawn
13 private Timer drawTimer;
14
15 // Timer to fire when screen should be cleared
16 private Timer resetTimer;
17
18 // ArrayList for storing all of the shapes
19 private ArrayList shapeArrayList = new ArrayList();
20
21 // Random object for creating shapes
22 private Random randomNumber = new Random();
23
24 // set delay for the next shape to be drawn
25 private final int SHAPE_DELAY = 20;
26
27 // set delay for the screen to be cleared
28 private final int CLEAR_DELAY = 40000;
29
30 // no-argument constructor
31 public DrawJPanel()
32 {
33 setBackground( Color.BLACK ); // set background color
34
35 // set up drawTimer
36 drawTimer = new Timer( SHAPE_DELAY,
37
38 new ActionListener() // anonymous inner class
39 {
40 // event handler called every SHAPE_DELAY
41 public void actionPerformed( ActionEvent event )
42 {
43 drawTimerActionPerformed( event );
44 }
45
46 } // end anonymous inner class
47
48 ); // end drawTimer declaration
49
50 drawTimer.start(); // start timer
51
52 // set up resetTimer
53 resetTimer = new Timer( CLEAR_DELAY,
54
55 new ActionListener() // anonymous inner class
56 {
57 // event handler called every CLEAR_DELAY
58 public void actionPerformed( ActionEvent event )
59 {
60 resetTimerActionPerformed( event );
61 }
62
63 } // end anonymous inner class
64
65 ); // end call to new Timer
66
67 resetTimer.start(); // start timer
68
69 } // end constructor
70
71 // paint all the shapes
72 public void paintComponent( Graphics g )
73 {
74 super.paintComponent( g );
75
76 MyShape nextShape;
77 Iterator shapesIterator = shapeArrayList.iterator();
78
79 // iterate through all of the shapes
80 while ( shapesIterator.hasNext() )
81 {
82 // draw each shape
83 nextShape = ( MyShape ) shapesIterator.next();
84 nextShape.draw( g );
85 }
86
87 } // end method paintComponent
88
89 // paint new shape
90 private void drawTimerActionPerformed( ActionEvent actionEvent )
91 {
92 // select a random number for the shape type
93 int shapeType = randomNumber.nextInt( 4 );
94
95 // randomly generate the x- and y-coordinates
96 int x = randomNumber.nextInt( 660 ) - 35 ;
97 int y = randomNumber.nextInt( 510 ) - 35 ;
98
99 // randomly generate the width and height of the shape
100 int width = randomNumber.nextInt( 60 ) + 6 ;
101 int height = randomNumber.nextInt( 60 ) + 6;
102
103 // randomly generate the color using integers for the red,
104 // green, and blue values
105 int red = randomNumber.nextInt( 200 );
106 int green = randomNumber.nextInt( 200 );
107 int blue = randomNumber.nextInt( 200 );
108 Color shapeColor = new Color( red, green, blue );
109
110 MyShape currentShape; // create new shape
111
112 // if shape is an outlined oval
113 if ( shapeType == 0 )
114 {
115 // create an outlined oval
116 currentShape = new MyOval( x, y, width + x + 20, height + y,
117 shapeColor, false );
118 }
119 else if ( shapeType == 1 ) // else if shape is a filled oval
120 {
121 // create a filled oval
122 currentShape = new MyOval( x, y, width + x + 20, height + y,
123 shapeColor, true );
124 }
125 // else if shape is an outlined rectangle
126 else if ( shapeType == 2 )
127 {
128 // create an outlined rectangle
129 currentShape = new MyRectangle( x, y, width + x,
130 height + y + 20, shapeColor, false );
131 }
132 else // shape is a filled rectangle
133 {
134 // create a filled rectangle
135 currentShape = new MyRectangle( x, y, width + x,
136 height + y + 20, shapeColor, true );
137 }
138
139 // add generated shape to ArrayList
140 shapeArrayList.add( currentShape );
141
142 repaint();
143
144 } // end method drawTimerActionPerformed
145
146 // clear the ArrayList
147 private void resetTimerActionPerformed( ActionEvent actionEvent )
148 {
149 shapeArrayList.clear();
150 }
151
152 } // end class DrawJPanel
```
```
1 // MyShape.java
2 // Superclass for all shape objects.
3 import java.awt.*;
4
5 public abstract class MyShape extends Object
6 {
7 private int x1;
8 private int y1;
9 private int x2;
10 private int y2;
11 private Color color;
12
13 // constructor
14 public MyShape( int firstX, int firstY, int secondX, int secondY,
15 Color shapeColor )
16 {
17 setX1( firstX );
18 setY1( firstY );
19 setX2( secondX );
20 setY2( secondY );
21 setColor( shapeColor );
22
23 } // end constructor
24
25 // set x1 value
26 public void setX1( int x )
27 {
28 x1 = x;
29
30 } // end method setX1
31
32 // get x1 value
33 public int getX1()
34 {
35 return x1;
36
37 } // end method getX1
38
39 // set Y1 value
40 public void setY1( int y )
41 {
42 y1 = y;
43
44 } // end method setY1
45
46 // get Y1 value
47 public int getY1()
48 {
49 return y1;
50
51 } // end method getY1
52
53 // set x2 value
54 public void setX2( int x )
55 {
56 x2 = x;
57
58 } // end method setX2
59
60 // get x2 value
61 public int getX2()
62 {
63 return x2;
64
65 } // end method getX2
66
67 // set y2 value
68 public void setY2( int y )
69 {
70 y2 = y;
71
72 } // end method setY2
73
74 // get y2 value
75 public int getY2()
76 {
77 return y2;
78
79 } // end method getY2
80
81 // set color value
82 public void setColor( Color c )
83 {
84 color = c;
85
86 } // end method setColor
87
88 // get color value
89 public Color getColor()
90 {
91 return color;
92
93 } // end method getColor
94
95 // abstract draw method
96 public abstract void draw( Graphics g );
97
98 } // end class MyShape
```
```
1 // Exercise 27.11: MyRectangle.java
2 // Class that declares a rectangle object.
3 import java.awt.*;
4
5 public class MyRectangle extends MyShape
6 {
7 // boolean for storing whether or not the rectangle is filled
8 private boolean filled;
9
10 // constructor
11 public MyRectangle( int firstX, int firstY, int secondX,
12 int secondY, Color shapeColor, boolean fill )
13 {
14 super( firstX, firstY, secondX, secondY, shapeColor );
15
16 filled = fill; // specify if shape will be filled
17
18 } // end constructor
19
20 // draw a rectangle
21 public void draw( Graphics g )
22 {
23 // set dimensions of bounding box
24 int upperLeftX = Math.min( getX1(), getX2() );
25 int upperLeftY = Math.min( getY1(), getY2() );
26 int width = Math.abs( getX1() - getX2() );
27 int height = Math.abs( getY1() - getY2() );
28
You might also like to view...
The Android operating system is owned and maintained by Amazon
Indicate whether the statement is true or false
All of the following statements are TRUE about protecting VBA code EXCEPT:
A) It is considered best practice to export any VBA modules as text files before protecting them with a password. B) Before a worksheet is protected, ensure that cells that need to be allowed to be changed to use the dashboard are not locked. C) Some managers may need to access the raw data in a workbook and should know VBA to be able to unhide those worksheets. D) Protecting worksheets from intentional or unintentional modification can be done by hiding the worksheets in the Excel workbook.