Java2D introduces many new capabilities for creating unique and impressive graphics. We will add a small subset of these features to the drawing application you created in Exercise 11.18. In this version of the drawing application, you will enable the user to specify gradients for filling shapes and to change stroke characteristics for drawing lines and outlines of shapes. The user will be able to choose which colors compose the gradient and set the width and dash length of the stroke.
First, you must update the MyShape hierarchy to support Java2D functionality. Make the following changes in class MyShape:
- Change abstract method draw's parameter type from Graphics to Graphics2D.
- Change all variables of type Color to type Paint to enable support for gradients. [Note: Recall that class Color implements interface Paint.]
- Add an instance variable of type Stroke in class MyShape and a Stroke parameter in the constructor to initialize the new instance variable. The default stroke should be an instance of class BasicStroke.
Classes MyLine, MyBoundedShape, MyOval and MyRect should each add a Stroke parameter to their constructors. In the draw methods, each shape should set the Paint and the Stroke before drawing or filling a shape. Since Graphics2D is a subclass of Graphics, we can continue to use Graphics methods drawLine, drawOval, fillOval, etc. to draw the shapes. When these methods are called, they will draw the appropriate shape using the specified Paint and Stroke settings.
Next, you will update the DrawPanel to handle the Java2D features. Change all Color variables to Paint variables. Declare an instance variable currentStroke of type Stroke and provide a set method for it. Update the calls to the individual shape constructors to include the Paint and Stroke arguments. In method paintComponent, cast the Graphics reference to type Graphics2D and use the Graphics2D reference in each call to MyShape method draw.
Next, make the new Java2D features accessible from the GUI. Create a JPanel of GUI components for setting the Java2D options. Add these components at the top of the DrawFrame below the panel that currently contains the standard shape controls (see Fig. 12.34). These GUI components should include:
- A check box to specify whether to paint using a gradient
- Two JButtons that each show a JColorChooser dialog to allow the user to choose the first and second color in the gradient. (These will replace the JComboBox used for choosing the color in Exercise 11.18.)
- A text field for entering the Stroke width
- A text field for entering the Stroke dash length
- A check box for selecting whether to draw a dashed or solid line
Figure 12.34. Drawing with Java2D.
If the user selects to draw with a gradient, set the Paint on the DrawPanel to be a gradient of the two colors chosen by the user. The expression
new GradientPaint( 0, 0, color1, 50, 50, color2, true ) )
creates a GradientPaint that cycles diagonally from the upper-left to the bottom-right every 50 pixels. Variables color1 and color2 represent the colors chosen by the user. If the user does not select to use a gradient, then simply set the Paint on the DrawPanel to be the first Color chosen by the user.
For strokes, if the user chooses a solid line, then create the Stroke with the expression
new BasicStroke( width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND )
where variable width is the width specified by the user in the line-width text field. If the user chooses a dashed line, then create the Stroke with the expression
new BasicStroke( width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
10, dashes, 0 )
where width again is the width in the line-width field, and dashes is an array with one element whose value is the length specified in the dash-length field. The Panel and Stroke objects should be passed to the shape object's constructor when the shape is created in DrawPanel.
|