In this exercise, you will implement a GUI application that uses the MyShape hierarchy from Exercise 10.10 to create an interactive drawing application. You will create two classes for the GUI and provide a test class that launches the application. The classes of the MyShape hierarchy require no additional changes.
The first class to create is a subclass of JPanel called DrawPanel, which represents the area on which the user draws the shapes. Class DrawPanel should have the following instance variables:
- An array shapes of type MyShape that will store all the shapes the user draws.
- An integer shapeCount that counts the number of shapes in the array.
- An integer shapeType that determines the type of shape to draw.
- A MyShape currentShape that represents the current shape the user is drawing.
- A Color currentColor that represents the current drawing color.
- A boolean filledShape that determines whether to draw a filled shape.
- A JLabel statusLabel that represents the status bar. The status bar will display the coordinates of the current mouse position.
Class DrawPanel should also declare the following methods:
- Overridden method paintComponent that draws the shapes in the array. Use instance variable shapeCount to determine how many shapes to draw. Method paintComponent should also call currentShape's draw method, provided that currentShape is not null.
- Set methods for the shapeType, currentColor and filledShape.
- Method clearLastShape should clear the last shape drawn by decrementing instance variable shapeCount. Ensure that shapeCount is never less than zero.
- Method clearDrawing should remove all the shapes in the current drawing by setting shapeCount to zero.
Methods clearLastShape and clearDrawing should call method repaint (inherited from JPanel) to refresh the drawing on the DrawPanel by indicating that the system should call method paintComponent.
Class DrawPanel should also provide event handling to enable the user to draw with the mouse. Create a single inner class that both extends MouseAdapter and implements MouseMotionListener to handle all mouse events in one class.
In the inner class, override method mousePressed so that it assigns currentShape a new shape of the type specified by shapeType and initializes both points to the mouse position. Next, override method mouseReleased to finish drawing the current shape and place it in the array. Set the second point of currentShape to the current mouse position and add currentShape to the array. Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.
Override method mouseMoved to set the text of the statusLabel so that it displays the mouse coordinatesthis will update the label with the coordinates every time the user moves (but does not drag) the mouse within the DrawPanel. Next, override method mouseDragged so that it sets the second point of the currentShape to the current mouse position and calls method repaint. This will allow the user to see the shape while dragging the mouse. Also, update the JLabel in mouseDragged with the current position of the mouse.
Create a constructor for DrawPanel that has a single JLabel parameter. In the constructor, initialize statusLabel with the value passed to the parameter. Also initialize array shapes with 100 entries, shapeCount to 0, shapeType to the value that represents a line, currentShape to null and currentColor to Color.BLACK. The constructor should then set the background color of the DrawPanel to Color.WHITE and register the MouseListener and MouseMotionLister so the JPanel properly handles mouse events.
Next, create a JFrame subclass called DrawFrame that provides a GUI that enables the user to control various aspects of drawing. For the layout of the DrawFrame, we recommend a BorderLayout, with the components in the NORTH region, the main drawing panel in the CENTER region, and a status bar in the SOUTH region, as in Fig. 11.49. In the top panel, create the components listed below. Each component's event handler should call the appropriate method in class DrawPanel.
Figure 11.49. Interface for drawing shapes.
(This item is displayed on page 594 in the print version)
- A button to undo the last shape drawn.
- A button to clear all shapes from the drawing.
- A combo box for selecting the color from the 13 predefined colors.
- A combo box for selecting the shape to draw.
- A check box that specifies whether a shape should be filled or unfilled.
Declare and create the interface components in DrawFrame's constructor. You will need to create the status bar JLabel before you create the DrawPanel, so you can pass the JLabel as an argument to DrawPanel's constructor. Finally, create a test class that initializes and displays the DrawFrame to execute the application.