Page 218 - Introduction to Programming with Java: A Problem Solving Approach
P. 218
184 Chapter 5 Using Pre-Built Methods
necessary information like current position of that window on the computer screen, current color, and cur- rent font type.
Most of Figure 5.15’s methods have pairs of parameters (like int
y coordinates in an image or window. These coordinates are always measured in pixels. The x coordinate is the number of pixels in from the left side of the image or window. The y coordinate is the number of pixels down from the top of the image or window. The x coordinate is like what you probably expect, but the y coordinate might seem funny, because normally we think of y increasing upward. However, in a computer display, y increases downward, because when a computer paints something on a screen, it paints the top line first, the line below second, and so on, in a top-to-bottom sequence.
In a method heading, x and y coordinate parameter names sometimes employ numerical suffixes to distinguish one point from another. In the drawImage method’s parameters, there is also a character prefix before each coordinate identifier. The d prefix stands for “destination,” which means position in the display window. The s prefix stands for “source,” which means position in the original image.
Sometimes numbers in a sequence of parameters are not x and y coordinates. Instead, they are width and height, which are coordinate differences. This is the case for the drawRect and fillOval methods. It’s easy to forget which technique a particular method uses to specify width or height. Does it specify posi- tions of upper left and lower right corners, or does it specify position of upper left corner and then width and height? Be careful. This is a common source of GUI programming errors.
x, int
y), which indicate x and
public boolean drawImage(Image img,
int dx1, int dy1, int dx2, int dy2,
Apago PDF Enhancer
ImageObserver observer)
Selects whatever is between sx1 and sx2 pixels to the right of the left edge of the source image and between sy1 and sy2 pixels below the top of the source image. Scales this selection as re- quired to fit between dx1 and dx2 pixels to the right of the left edge of the destination window and between dy1 and dy2 below the top of the destination window.
public void setColor(Color c)
Establishes a specified painting color.
public void drawRect(int x, int y, int width, int height)
Draws the outline of a rectangle whose upper left corner is x pixels to the right of the left side of the window and y pixels below the top of the window. Uses most recently set color.
public void drawLine(int x1, int y1, int x2, int y2)
Draws a straight line from a point x1 pixels to the right of the left side of the window and y1 pixels below the top of the window to a point x2 pixels to the right of the left side of the window and y2 pixels below the top of the window. Uses most recently set color.
public void fillOval(int x, int y, int width, int height)
Fills an ellipse bounded by the specified rectangle with the most recently set color.
public void drawString(String text, int x, int y)
Prints the specified text on a line that starts x pixels to the right of the left side of the window and y pixels down from the top of the window. Uses the most recently set color.
int sx1, int sy1, int sx2, int sy2,
Figure 5.15 Selected methods from the Java API Graphics class