Page 219 - Introduction to Programming with Java: A Problem Solving Approach
P. 219
5.9 GUI Track: Drawing Images, Lines, Rectangles, and Ovals in Java Applets (Optional) 185
The drawImage method copies a rectangular portion of the source image and pastes an expanded or contracted version of it into a specified rectangle in the destination window. The first parameter is a refer- ence to the source image. The second and third parameters are the destination (display window) coordinates of the top left corner of the copied part of the image. The fourth and fifth parameters are the destination coordinates of the bottom right corner of the copied part of the image. The sixth and seventh parameters are the coordinates of the top left corner of the part of the source image to be copied. The eighth and ninth parameters are the coordinates of the bottom right corner of the part of the source image to be copied. The last parameter enables the method to send out current-status information.
The setColor method establishes a color to be used in subsequent operations that draw lines or geo- metric figures or write text. You pass this method an argument like Color.BLUE, which identifies one of several named constants defined in the Java API Color class. You can find the names of the other named colors in the documentation for the Java API Color class. Most of those names are pretty obvious, so as a practical matter, you can just guess and see what happens.
The drawRect method draws the border of a rectangle using the most recently set color. The first and second parameters are the coordinates of the top left corner of the rectangle in the display window. The third and fourth parameters are the width and height of the rectangle.
The drawLine method draws a straight line between two specified points using the most recently set color. The first and second parameters are the coordinates of the starting point. The third and fourth param- eters are the coordinates of the ending point.
The fillOval method draws an ellipse in the specified rectangle and fills it with the most recently set color. The parameters are like those for drawRect: The first and second parameters are the coordinates of the top left corner of the enclosing rectangle in the display window. The third and fourth parameters are the width and height of the enAclopsinag rgecotangleP, aDndFthe wEidnthhanad hneigchteofrthe oval itself.
The drawString method prints text at the specified position using the most recently set color. The first parameter is the string to be printed. The second and third parameters are the coordinates of the upper left corner of the string.
Using Graphics Methods in a Java Applet
Figure 5.16 provides an example of how you can use the graphics methods in Figure 5.15 to manipulate a photographic image and add your own lines, shapes, and text to it. The overall window in Figure 5.16 is 640 pixels wide and 640 pixels high.
Figure 5.17 shows the Java code needed to render the display in Figure 5.16. The code is all in the body of a method called paint. The paint method’s parameter g is used as a prefix on each of the method calls in paint. It refers to the Graphics object that manages the painting operation. Within paint, the first statement retrieves a reference to the source image. The next statement’s drawImage method employs the source image reference to access the source image.
The drawImage method shrinks the hurricanes.jpg image in Figure 5.13 to two thirds of its original size and pastes it into the upper left corner of the display window. The setColor method sets the current color to blue. The drawRect method draws a square around an area of interest. Then four calls to the drawLine method draw four straight lines to the corners of where a three-times enlargement of the area of interest will go. Another call to the drawImage method pastes an enlarged version of the area of interest at this location. Another call to drawRect puts a rectangle around this enlargement. Then the fillOval method paints a blue oval in the enlargement, and finally the drawString method prints the name “MAX” in the center of this blue oval. Notice how each subsequent operation over-writes or covers all previous operations.