Enhance the Drawing Shapes application that you created in this tutorial. Improve the application so that once you finish drawing a shape, the shape will be given a random velocity and begin to move, bouncing off the walls of the PaintJPanel. Your output should be capable of looking look like Fig. 27.38.



a) Copying the template to your working directory. Copy the C:Examples Tutorial27ExercisesMovingShapes directory to your C:SimplyJava directory.

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

c) Adding a method to your MyMovingShape class to change the position of the shape.

The abstract superclass for this inheritance hierarchy has been renamed MyMoving-

Shape. Add a public method named moveShape to the class. It should take no arguments and have no return type. Two new instance variables, dx and dy, have been added to the MyMovingShape class for you. Variable dx holds the distance along the x-axis that the shape must travel in one move. Variable dy holds the distance along the y-axis that the shape must travel in one move. Add dx to the x1 and x2 values and add dy to the y1 and y2 values. Follow good programming practice by using the cor- responding get and set methods instead of modifying the variables directly.

d) Fini


```
1 // MovingShapes.java
2 // Application allows user to create shapes that move around screen.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class MovingShapes extends JFrame
8 {
9 // JComboBox to choose a drawing shape
10 private JComboBox shapeJComboBox;
11
12 // JButton to select a new color
13 private JButton colorJButton;
14
15 // PaintJPanel for drawing shapes
16 private PaintJPanel painterPaintJPanel;
17
18 // String array for storing the choices in shapeJComboBox
19 private String[] shapeTypes = { "Line", "Rectangle", "Oval" };
20
21 // no-argument constructor
22 public MovingShapes()
23 {
24 createUserInterface();
25 }
26
27 // create and position GUI components; register event handlers
28 private void createUserInterface()
29 {
30 // get content pane for attaching GUI components
31 Container contentPane = getContentPane();
32
33 // enable explicit positioning of GUI components
34 contentPane.setLayout( null );
35
36 // set up controlsJPanel
37 JPanel controlsJPanel = new JPanel();
38 controlsJPanel.setBounds( 0, 0, 400, 40 );
39 controlsJPanel.setLayout( null );
40 contentPane.add( controlsJPanel );
41
42 // set up painterPaintJPanel
43 painterPaintJPanel = new PaintJPanel();
44 painterPaintJPanel.setBounds( 0, 40, 400, 340 );
45 painterPaintJPanel.setBackground( Color.WHITE );
46 contentPane.add( painterPaintJPanel );
47
48 // set up shapeJComboBox
49 shapeJComboBox = new JComboBox( shapeTypes );
50 shapeJComboBox.setBounds( 100, 2, 90, 24 );
51 controlsJPanel.add( shapeJComboBox );
52 shapeJComboBox.addActionListener(
53
54 new ActionListener() // anonymous inner class
55 {
56 // event handler called when shapeJComboBox is selected
57 public void actionPerformed( ActionEvent event )
58 {
59 shapeJComboBoxActionPerformed( event );
60 }
61
62 } // end anonymous inner class
63
64 ); // end call to addActionListener
65
66 // set up colorJButton
67 colorJButton = new JButton( "Color" );
68 colorJButton.setBounds( 210, 2, 80, 24 );
69 controlsJPanel.add( colorJButton );
70 colorJButton.addActionListener(
71
72 new ActionListener() // anonymous inner class
73 {
74 // event handler called when colorJButton is pressed
75 public void actionPerformed( ActionEvent event )
76 {
77 colorJButtonActionPerformed( event );
78 }
79
80 } // end anonymous inner class
81
82 ); // end call to addActionListener
83
84 // set properties of application's window
85 setTitle( "Moving Shapes" ); // set title bar string
86 setSize( 408, 407 ); // set window size
87 setVisible( true ); // display window
88
89 } // end method createUserInterface
90
91 // select a new color for the shape
92 private void colorJButtonActionPerformed( ActionEvent event )
93 {
94 Color selection = JColorChooser.showDialog( null,
95 "Select a Color", Color.BLACK );
96 colorJButton.setBackground( selection );
97 painterPaintJPanel.setCurrentColor( selection );
98
99 } // end method colorJButtonActionPerformed
100
101 // set the selected shape in the painting panel
102 private void shapeJComboBoxActionPerformed( ActionEvent e )
103 {
104 painterPaintJPanel.setCurrentShapeType(
105 shapeJComboBox.getSelectedItem().toString() );
106
107 } // end method shapeJComboBoxActionPerformed
108
109 // main method
110 public static void main( String args[] )
111 {
112 MovingShapes application = new MovingShapes();
113 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
114
115 } // end method main
116
117 } // end class MovingShapes1
```

```
1 // PaintJPanel.java
2 // Creates a component that allows a user to draw shapes.
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import javax.swing.*;
8
9 public class PaintJPanel extends JPanel
10 {
11 // Timer for repainting and moving every 30 milliseconds
12 private Timer moveTimer;
13
14 // ArrayList for storing all of the shapes
15 private ArrayList shapeArrayList = new ArrayList();
16
17 // Color for storing the currently selected color
18 private Color currentColor;
19
20 // String for storing which shape type is currently being drawn
21 private String currentShapeType = "Line";
22
23 // MyMovingShape for storing the shape currently being drawn
24 private MyMovingShape currentShape;
25
26 // constructor
27 public PaintJPanel()
28 {
29 addMouseListener(
30
31 new MouseAdapter() // anonymous inner class
32 {
33 // event handler called when mouse button is pressed
34 public void mousePressed( MouseEvent event )
35 {
36 paintJPanelMousePressed( event );
37 }
38
39 // event handler called when mouse button is released
40 public void mouseReleased( MouseEvent event )
41 {
42 paintJPanelMouseReleased( event );
43 }
44
45 } // end anonymous inner class
46
47 ); // end call to addMouseListener
48
49 addMouseMotionListener(
50
51 new MouseMotionAdapter() // anonymous inner class
52 {
53 // event handler called when the mouse is dragged
54 public void mouseDragged( MouseEvent event )
55 {
56 paintJPanelMouseDragged( event );
57 }
58
59 } // end anonymous inner class
60
61 ); // end call to addMouseMotionListener
62
63 // set timer for moving shapes
64 moveTimer = new Timer( 30,
65
66 new ActionListener() // anonymous inner class
67 {
68 // event handler called every 30 milliseconds
69 public void actionPerformed( ActionEvent event )
70 {
71 moveTimerActionPerformed( event );
72 }
73
74 } // end anonymous inner class
75
76 ); // end moveTimer declaration
77
78 moveTimer.start(); // start timer
79
80 } // end constructor
81
82 // paint all the shapes
83 public void paintComponent( Graphics g )
84 {
85 super.paintComponent( g );
86
87 MyMovingShape nextShape;
88 Iterator shapesIterator = shapeArrayList.iterator();
89
90 // iterate through all of the shapes
91 while ( shapesIterator.hasNext() )
92 {
93 // draw each shape
94 nextShape = ( MyMovingShape ) shapesIterator.next();
95 nextShape.draw( g );
96 }
97
98 // if the user is currently drawing a shape
99 if ( currentShape != null )
100 {
101 // draw the shape currently being drawn by a user
102 currentShape.draw( g );
103 }
104
105 } // end paintComponent
106
107 // add a shape to the ArrayList
108 public void paintJPanelMousePressed( MouseEvent event )
109 {
110 // store the new shape being drawn to currentShape
111 if ( currentShapeType.equals( "Line" ) )
112 {
113 currentShape = new MyLine( event.getX(), event.getY(),
114 event.getX(), event.getY(), currentColor );
115 }
116 else if ( currentShapeType.equals( "Rectangle" ) )
117 {
118 currentShape = new MyRectangle( event.getX(), event.getY(),
119 event.getX(), event.getY(), currentColor );
120 }
121 else if ( currentShapeType.equals( "Oval" ) )
122 {
123 currentShape = new MyOval( event.getX(), event.getY(),
124 event.getX(), event.getY(), currentColor );
125 }
126
127 } // end method paintJPanelMousePressed
128
129 // resize the shape being drawn
130 public void paintJPanelMouseDragged( MouseEvent event )
131 {
132 // set the location of currentShape to its resized location
133 currentShape.setX2( event.getX() );
134 currentShape.setY2( event.getY() );
135
136 } // end method paintJPanelMouseDragged
137
138 // finish resizing the shape being drawn and start its movement
139 public void paintJPanelMouseReleased( MouseEvent event )
140 {
141 // set the location of currentShape to its resized location
142 currentShape.setX2( event.getX() );
143 currentShape.setY2( event.getY() );
144
145 // add completed shape to shapeArray
146 shapeArrayList.add( currentShape );
147
148 // the user is no longer drawing so set currentShape to null
149 currentShape = null;
150
151 } // end method paintJPanelMouseReleased
152
153 // move the shapes
154 private void moveTimerActionPerformed( ActionEvent event )
155 {

Computer Science & Information Technology

You might also like to view...

the command chmod 777 myfile

a: grants rwx access to user ID number 777 to use myfile b: grants rwx access to all users of myfile c: denies rwx access to all users of myfile d: grants rwx access only to the owner of myfile e: makes myfile a hidden file

Computer Science & Information Technology

What indicates a page break?

A) A red line B) A dotted line C) A solid bold line D) A blue line

Computer Science & Information Technology

Goal Seek would be the best what-if analysis tool to use to discover the necessary interest rate to keep your car payment below $500 per month

Indicate whether the statement is true or false

Computer Science & Information Technology

Explain what functions and features are included in Backstage view.

What will be an ideal response?

Computer Science & Information Technology