The Circle Painter application should draw a blue circle with a randomly chosen size when the user presses a mouse button anywhere over the JPanel (Fig. 21.32). The application should randomly select a circle diameter in the range from 5 to 199, inclusive. To draw a blue circle with a given diameter (diameter), use the fol- lowing statement:



The drawOval method draws the outline of an oval. Recall that an oval is a circle if the height and width arguments are the same (in this case, the randomly selected width and height). Use the x- and y-coordinates of the mousePressed event as the x- and y-coordi- nates of the circle’s bounding box (that is, the first and second arguments to the drawOval method).

a) Copying the template to your working directory. Copy the C:Examples Tutorial21ExercisesCirclePainter directory to your C:SimplyJava direc- tory.

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

c) Coding the drawJPanelMousePressed method. Find the drawJPanelMousePressed method, which starts at line 58. Add code to the drawJPanelMousePressed method to store the x- coordinate in instance variable x (declared for you in the template) and store the y- coordinate in instance variable y (declared for you in the template), Then, store a random int in the range 5 to 199 into the inst


```
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 // no-argument constructor
18 public DrawJPanel()
19 {
20 // add MouseListener to DrawJPanel
21 addMouseListener(
22
23 new MouseListener() // anonymous inner class
24 {
25 // event handler called when mouse button is pressed
26 public void mousePressed( MouseEvent event )
27 {
28 drawJPanelMousePressed( event );
29 }
30
31 // event handler must exist to implement interface
32 public void mouseReleased( MouseEvent event )
33 {
34 }
35
36 // event handler must exist to implement interface
37 public void mouseClicked( MouseEvent event )
38 {
39 }
40
41 // event handler must exist to implement interface
42 public void mouseEntered( MouseEvent event )
43 {
44 }
45
46 // event handler must exist to implement interface
47 public void mouseExited( MouseEvent event )
48 {
49 }
50
51 } // end anonymous inner class
52
53 ); // end call to new MouseListener
54
55 } // end constructor
56
57 // set dimensions of circle and call repaint
58 private void drawJPanelMousePressed( MouseEvent event )
59 {
60 x = event.getX(); // get x position of mouse
61 y = event.getY(); // get y position of mouse
62
63 // set width to random int from 5 to 199
64 diameter = 5 + generator.nextInt( 194 );
65
66 repaint(); // repaint DrawJPanel
67
68 } // end method drawJPanelMousePressed
69
70 // draw circle
71 public void paintComponent( Graphics g )
72 {
73 super.paintComponent(g);
74
75 // draw circle
76 g.setColor( Color.BLUE );
77 g.drawOval( x, y, diameter, diameter );
78
79 } // end method paintComponent
80
81 } // end class DrawJPanel
```

Computer Science & Information Technology

You might also like to view...

Invalid possibilities for array indices include .

a. Positive integers. b. Negative integers. c. Zero. d. None of the above.

Computer Science & Information Technology

A new thread begins its life cycle by transitioning to the __________ state.

a. runnable. b. waiting. c. terminated. d. new.

Computer Science & Information Technology

Which of the following best describes HIPAA administrative safeguards?

A) Retention, availability, and update requirements related to supporting documentation B) The use of technical security measures to protect ePHI data C) Standards for business associate contracts and other arrangements D) Documented policies and procedures for managing day-to-day operations and access to ePHI

Computer Science & Information Technology

A private IP address can be used to send data over the Internet.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology