Step 7
Overview
In this step, you will be adding code to your classes to handle new drawing
methods and their parameters.
You will write a fair amount of code, but it can be easy if you take the
time to understand what you are doing.
It can be easy because you have done it all before, so you have existing
code to work with as an example.
There are three substeps, each described in its own section below.
- Write code to handle new drawing method parameters.
- Write code to handle new drawing methods.
- Write code to incorporate new drawing method parameters into the draw
panel title.
Handle New Drawing Method Parameters
There are 6 new drawing method parameters that are needed: width, height,
arc width, arc height, start angle, and arc angle.
Like the x and y drawing method parameters that you already have, these all
have type int and are all controlled by JSlider controls.
In order to handle each new drawing method parameter, you need to do the
following.
-
Add an instance variable for its slider control in the GraphicsApplet
class.
It should be initialized by invoking the createSlider() helper
method.
The following table provides the initialization information that you will
need.
Variable | Title | Min | Max | Start
|
---|
widthControl | "width" | 0 | 200 | 100
|
heightControl | "height" | 0 | 200 | 100
|
arcWidthControl | "arcWidth" | 0 | 200 | 100
|
arcHeightControl | "arcHeight" | 0 | 200 | 100
|
startAngleControl | "startAngle" | 0 | 720 | 0
|
arcAngleControl | "arcAngle" | 0 | 360 | 180
|
-
Add a statement into the GraphicsApplet createControlPanel()
helper method that adds the slider control to the control panel.
-
Add a getter method to the GraphicsApplet class that returns the value of
the slider control.
You should use the following names for the methods.
Method Name
|
---|
getWidth
|
getHeight
|
getArcWidth
|
getArcHeight
|
getStartAngle
|
getArcAngle
|
-
Add an instance variable for its value in the DrawPanel class.
You should use the following names for the variables.
Variable Name
|
---|
width
|
height
|
arcWidth
|
arcHeight
|
startAngle
|
arcAngle
|
-
Add a statement into the DrawPanel updateDrawing() method
that gets the appropriate value from the controller and assigns it to the
instance variable.
After making these changes, you should recompile the two classes.
If you get any errors, they are most likely due to inconsistent spellings
of variable or method names or omission of their declarations.
You can also run the applet with appletviewer.
You should see all of the new controls, but they will not have any effects
on the drawings until after the next substep.
Handle New Drawing Methods
There are 8 new drawing methods that are needed.
Drawing Method Name
|
---|
drawRect
|
drawOval
|
drawRoundRect
|
drawArc
|
fillRect
|
fillOval
|
fillRoundRect
|
fillArc
|
You will deal with each of these drawing methods in the same way that you
are now dealing with the drawString drawing method.
-
Add a statement in the GraphicsApplet createMethodControl() helper
method.
This statement adds a literal string, the drawing method name, as an item
to the method control.
-
Add an if statement to the DrawPanel paintComponent()
method.
The condition for this statement tests if the method variable is
equal to the new drawing method name.
The statement inside the if statement should send the appropriate
draw method message to the Graphics parameter of the
paintComponent() method.
You will need the x, y, width, and height drawing parameters for all of the
new drawing methods.
The drawRoundRect and fillRoundRect drawing method both need the arcWidth
and arcHeight drawing parameters.
The drawArc and fillArc drawing methods both need the startAngle and
arcAngle drawing parameters.
If you have any questions about the order of the drawing parameters, take a
look at the demonstration applet at the end of this step.
You can see which parameter goes where by operation controls, noting which
of the numbers change in the draw panel title.
After making these changes, you should recompile the two classes.
If you get any errors, they are most likely due to inconsistent spellings
of variables or omission of their declarations.
You can also run the applet with appletviewer.
The controls should affect the drawing in the draw panel, but the title in
its border will not be correct.
That will be fixed in the next substep.
Incorporate New Drawing Method Parameters into the Draw Panel Title
The last thing you need to do in this step is add some if
statements into the changeTitle() method in the DrawPanel class.
These statements should incorporate the appropriate parameters into the
message local variable.
They should be added just before the statement that appends the close
parenthesis and semicolon.
Here are the rules for appending parameters to message.
-
If the name of the drawing method does not end with "String" then
you need to append the width and height parameters and their comma
separators.
In Java, a boolean expression is negated by placing an exclamation point in
front of it, as in !method.endsWith("String").
-
If the name of the drawing method ends with "RoundRect" then you need to
append the arc width and arc height parameters and their comma separators.
-
If the name of the drawing method ends with "Arc" then you need to append
the start angle and arc angle parameters and their comma separators.
After making these changes, you should recompile the DrawPanel class and
run the applet with appletviewer.
Now the draw panel title should be correct.
Do you have the commas in the right place?
Your applet should look like the following demonstration applet.
Check all of the controls to make sure they are working correctly.
Demonstration Applet