<< Chapter < Page | Chapter >> Page > |
Save as type Graphics2D in a variable named g2
The graphics context for the Picture object is saved in Listing 3 as type Graphics2D . The reference is saved in the reference variable named g2 .
Save the width and height of the Picture object
The last two statements in Listing 3 get and save the width and the height of the image encapsulated in the Picture object.
Translate the origin to the center of the image
By default, the origin (with coordinates of 0,0) is in the upper-left corner of the image. However, we would like to be able to work with acoordinate system in which the origin is at the center.
Listing 4 calls the translate method to move the origin to the center of the image.
Listing 4 - Translate the origin to the center of the image. |
---|
g2.translate(width/2,height/2); |
(Fortunately, you already understand affine transforms. Otherwise, you might not be able to understand the documentation for the translate method.)
From this point forward...
From this point forward, we can think of the coordinates of the pixel at the very center of the object as having values of 0,0. Locations to the left ofcenter have negative X coordinates and locations above the center have negative Y coordinates.
Draw the black horizontal and vertical axes
The next thing we want to do is to draw the black horizontal and vertical axes that you see in the center of the image in Figure 1 . This is accomplished by the code in Listing 5 .
Listing 5 - Draw the black horizontal and vertical axes. |
---|
//Set the drawing color to black
g2.setColor(Color.BLACK);//Draw x-axis
g2.draw(new Line2D.Double(-width/2, 0.0,width/2, 0.0));
//Draw y-axisg2.draw(new Line2D.Double(0.0, -width/2,
0.0, height/2)); |
Set the drawing color to black
Listing 5 begins by calling the setColor method to set the drawing color to Color.BLACK.
(BLACK is a static constant in the Color class that represents the color black.)
A new Line2D.Double object
The fourth line of code in Listing 5 instantiates a new object of the Line2D.Double class. This object represents a line extending between two points specified by coordinate values passed as parameters to theconstructor.
A black horizontal line
The first pair of coordinate values specifies the left end of the black horizontal line in Figure 1 . The second pair of coordinate values specifies the right end of the black horizontal line in Figure 1 .
(See my Lesson Number 300 for an explanation of the somewhat unusual name of a class consisting of two words separated by a period: Line2D.Double .)
Pass the line object to the Draw method
The new object's reference is passed to the draw method, which is responsible for causing the line to be drawn on the graphics context.
A black vertical axis
The last statement in Listing 5 draws the black vertical line shown in Figure 1 .
Coordinates relative to the origin at the center
Note that in both cases, the end points of the line are specified using coordinate values that are relative to the origin, which is positioned at thecenter of the drawing context.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?