In this exercise, you will enhance the applica- tion. The advanced Circle Painter application should draw blue circles with a randomly generated diameter when the user presses the left mouse button. When the user presses the right mouse button, the application should draw a red circle with a randomly generated diameter (Fig. 21.33).





a) Copying the template to your working directory. Copy the C:Examples Tutorial21ExercisesAdvancedCirclePainter directory to your C:Simply- Java directory.

b) Opening the template file. Open the DrawJPanel.java file in your text editor.

c) Adding a Color instance variable. In line 17, add instance variable circleColor of type Color to hold the color of the circle to be painted.

d) Determining which button was pressed. Find the drawJPanelMousePressed method, which starts at line 60. In line 68 (before the method call repaint), add an if…else statement that sets circleColor to Color.RED if right mouse button is pressed and sets circleColor to Color.BLUE if left mouse button is pressed.

e) Drawing the appropriate color. Find the paintComponent method, which immedi- ately follows drawJPanelMousePressed. Replace the parameter passed to the set- Color method (Color.BLUE) with circleColor since the color of the drawn circle will change depending on the mouse button t

```
1 // CirclePainter.java
2 // Application draws circles of random sizes when mouse is clicked.
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class CirclePainter extends JFrame
7 {
8 // DrawJPanel for displaying circles
9 private DrawJPanel myDrawJPanel;
10
11 // no-argument constructor
12 public CirclePainter()
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 myDrawJPanel
27 myDrawJPanel = new DrawJPanel();
28 myDrawJPanel.setBounds( 0, 40, 450, 450 );
29 contentPane.add( myDrawJPanel );
30
31 // set properties of application's window
32 setTitle( "Circle Painter" ); // set title bar text
33 setSize( 450, 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 CirclePainter application = new CirclePainter();
42 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
43
44 } // end method main
45
46 } // end class CirclePainter
```
```
1 // DrawJPanel.java
2 // This class defines the DrawJPanel class.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.Random;
6 import javax.swing.*;
7
8 public class DrawJPanel extends JPanel
9 {
10 // Random object to create random numbers
11 private Random generator = new Random();
12
13 int x; // x position of circle
14 int y; // y position of circle
15 int diameter; // diameter of circle
16
17 Color circleColor; // color of circle
18
19 // no-argument constructor
20 public DrawJPanel()
21 {
22 // add MouseListener to DrawJPanel
23 addMouseListener(
24
25 new MouseListener() // anonymous inner class
26 {
27 // event handler called when mouse button is pressed
28 public void mousePressed( MouseEvent event )
29 {
30 drawJPanelMousePressed( event );
31 }
32
33 // event handler called must exist to implement interface
34 public void mouseReleased( MouseEvent event )
35 {
36 }
37
38 // event handler must exist to implement interface
39 public void mouseClicked( MouseEvent event )
40 {
41 }
42
43 // event handler must exist to implement interface
44 public void mouseEntered( MouseEvent event )
45 {
46 }
47
48 // event handler must exist to implement interface
49 public void mouseExited( MouseEvent event )
50 {
51 }
52
53 } // end anonymous inner class
54
55 ); // end call to new MouseListener
56
57 } // end constructor
58
59 // set dimensions of circle and call repaint
60 private void drawJPanelMousePressed( MouseEvent event )
61 {
62 x = event.getX(); // get x position of mouse
63 y = event.getY(); // get y position of mouse
64
65 // set width to random int from 5 to 199
66 diameter = 5 + generator.nextInt( 194 );
67
68 if ( event.isMetaDown() )
69 {
70 // if right mouse button is pressed
71 circleColor = Color.RED;
72 }
73 else
74 {
75 // if left mouse button is pressed
76 circleColor = Color.BLUE;
77 }
78
79 repaint(); // repaint DrawJPanel
80
81 } // end method circlePainterMousePressed
82
83 // draw circle
84 public void paintComponent( Graphics g )
85 {
86 super.paintComponent( g );
87
88 // draw circle
89 g.setColor( circleColor );
90 g.drawOval( x, y, diameter, diameter );
91
92 } // end method paintComponent
93
94 } // end class DrawJPanel
```

Computer Science & Information Technology

You might also like to view...

The programming approach (or paradigm) which uses objects as the foundational element of software design is called ________________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

?A(n) ____ server encrypts data, which prevents unauthorized parties from being able to read or use it.

A. ?secure B. ?dedicated C. ?shared D. ?open-source

Computer Science & Information Technology