Language:EN
Pages: 46
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
figure filled and unfilled ovalspublic abstract vo

Figure filled and unfilled ovalspublic abstract void drawarc int

2
Simple Graphics

In this chapter:

• Point

• Polygon

• Image

• MediaTracker

You rarely need to create a Graphics object yourself; its constructor is protected and is only called by the subclasses that extend Graphics. How then do you get a

20

21

protected Graphics ()
Because Graphics is an abstract class, it doesn’t have a visible constructor. The way to get a Graphics object is to ask for one by calling getGraphics() or to use the one given to you by the Component.paint() or Component.update() method.

The abstract methods of the Graphics class are implemented by some windowing system–specific class. You rarely need to know which subclass of Graphics you are using, but the classes you actually get (if you are using the JDK) are sun.awt.win32.Win32Graphics ( JDK1.0), sun.awt.window.WGraphics ( JDK1.1), sun.awt.motif.X11Graphics, or sun.awt.macos.MacGraphics.

CHAPTER 2: SIMPLE GRAPHICS

public Graphics create (int x, int y, int width, int height)
This method creates a second reference to a subset of the drawing area of the graphics context. The new Graphics object covers the rectangle from (x, y) through (x+width-1, y+height-1) in the original object. The coordinate space of the new Graphics context is translated so that the upper left corner is (0, 0) and the lower right corner is (width, height). Shifting the coordinate system of the new object makes it easier to work within a portion of the drawing area without using offsets.

Drawing strings

public abstract Font getFont ()
The getFont() method returns the current Font of the graphics context. See Chapter 3, Fonts and Colors, for more on what you can do with fonts. You can-not get meaningful results with getFont() until the applet or application is displayed on the screen (generally, not in init() of an applet or main() of an application).

public abstract void setFont (Font font)
The setFont() method changes the current Font to font. If font is not avail-able on the current platform, the system chooses a default. To change the cur-rent font to 12 point bold TimesRoman:

23

For more information about Font and FontMetrics, see Chapter 3.

Painting

public abstract void clipRect (int x, int y, int width, int height)
The clipRect() method reduces the drawing area to the intersection of the current drawing area and the rectangular area from (x, y) to (x+width-1, y+height-1). Any future drawing operations outside this clipped area will have no effect. Once you clip a drawing area, you cannot increase its size with clipRect(); the drawing area can only get smaller. (However, if the clipRect() call is in paint(), the size of the drawing area will be reset to its original size on subsequent calls to paint().) If you want the ability to draw to the entire area, you must create a second Graphics object that contains a copy of the drawing area before calling clipRect() or use setClip(). The following code is a simple applet that demonstrates clipping; Figure 2-1 shows the result.

CHAPTER 2: SIMPLE GRAPHICS

The paint() method for this applet starts by setting the foreground color to red. It then creates a copy of the Graphics context for clipping, saving the original object so it can draw on the entire screen later. The applet then draws a rectangle, sets the clipping area to a smaller region, and draws a diagonal line across the rectangle from upper left to lower right. Because clipping is in effect, only part of the line is displayed. The applet then discards the clipped Graphics object and draws an unclipped line from lower left to upper right using the original object g.

public abstract void setClip(int x, int y, int width, int height) #
This setClip() method allows you to change the current clipping area based on the parameters provided. setClip() is similar to clipRect(), except that it is not limited to shrinking the clipping area. The current drawing area becomes the rectangular area from (x, y) to (x+width-1, y+height-1); this area may be larger than the previous drawing area.

25

getClipRect() is the Java 1.0 name for this method.

public abstract Shape getClip () #
The getClip() method returns a Shape that describes the clipping area of a Graphics object. That is, it returns the same thing as getClipBounds() but as a Shape, instead of as a Rectangle. By calling Shape.getBounds(), you can get the (x, y) coordinates of the top left corner of the clipping area along with its width and height. In the near future, it is hard to imagine the actual object that getClip() returns being anything other than a Rectangle.

* It should be simple for Sun to fix this bug; one would expect clipping to a Polygon to be the same as clipping to the Polygon’s bounding rectangle.

26

CHAPTER 2: SIMPLE GRAPHICS

import java.awt.*;
public class xor extends java.applet.Applet { public void init () {
setBackground (Color.red);
}
public void paint (Graphics g) {
g.setColor (Color.green);
g.setXORMode (Color.blue);
g.fillRect (10, 10, 100, 100);
g.fillRect (10, 60, 100, 100);
}
}

Although it’s hard to visualize what color XOR mode will pick, there is one impor-tant special case. Let’s say that there are only two colors: a background color (the

27

second fillRect()

Figure 2–2: Drawing in XOR mode

Versions 1.0.2 and 1.1 of AWT always draw solid lines that are one pixel wide; there is no support for line width or fill patterns. A future version should support lines with variable widths and patterns.

public abstract void drawLine (int x1, int y1, int x2, int y2)
The drawLine() method draws a line on the graphics context in the current color from (x1, y1) to (x2, y2). If (x1, y1) and (x2, y2) are the same point, you will draw a point. There is no method specific to drawing a point. The follow-ing code generates the display shown in Figure 2-3.

CHAPTER 2: SIMPLE GRAPHICS

g.drawLine (5, 75, 5, 75);

Figure 2–3: Drawing lines and points with drawLine()

public void drawRect (int x, int y, int width, int height)
The drawRect() method draws a rectangle on the drawing area in the current color from (x, y) to (x+width, y+height). If width or height is negative, noth-ing is drawn.

2.1 GRAPHICS

29

Figure 2-4 shows how AWT generates rounded corners. Figure 2-5 shows the collection of rectangles created by the following code. The rectangles in Fig-ure 2-5 are filled and unfilled, with rounded and square corners.

g.drawRect (25, 10, 50, 75);
g.fillRect (25, 110, 50, 75);
g.drawRoundRect (100, 10, 50, 75, 60, 50); g.fillRoundRect (100, 110, 50, 75, 60, 50);

CHAPTER 2: SIMPLE GRAPHICS

Figure 2–5: Varieties of rectangles

only if the current color is not black. If raised is true, the rectangle looks like a button waiting to be pushed. If raised is false, the rectangle looks like a depressed button. To enhance the shadow effect, the depressed area is given a slightly deeper shade of the drawing color. If width or height is negative, the shadow appears from another direction, and the rectangle isn’t filled. (Differ-ent platforms could deal with this differently. Try to ensure the parameters have positive values.)

2.1 GRAPHICS

31

Figure 2–7: Filled and unfilled ovals

public abstract void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) The drawArc() method draws an arc in the current color within an invisible bounding rectangle from (x, y) to (x+width, y+height). The arc starts at startAngle degrees and goes to startAngle + arcAngle degrees. An angle of 0 degrees is at the 3 o’clock position; angles increase counter-clockwise. If

CHAPTER 2: SIMPLE GRAPHICS

g.drawArc (25, 10, 50, 75, 0, 360);
g.fillArc (25, 110, 50, 75, 0, 360);
g.drawArc (100, 10, 50, 75, 45, 215);
g.fillArc (100, 110, 50, 75, 45, 215);

2.1 GRAPHICS

33

public abstract void drawPolygon (int xPoints[], int yPoints[], int numPoints)
The drawPolygon() method draws a path of numPoints nodes by plucking one element at a time out of xPoints and yPoints to make each point. The path is drawn in the current color. If either xPoints or yPoints does not have num-Points elements, drawPolygon() throws a run-time exception. In 1.0.2, this exception is an IllegalArgumentException; in 1.1, it is an ArrayIndexOutOf-BoundsException. This change shouldn’t break older programs, since you are not required to catch run-time exceptions.

x=25 x=75 x=125 x=175 x=225

The scan line starts at the left edge of the screen; at this point there haven’t been

34

CHAPTER 2: SIMPLE GRAPHICS

int[] xPoints[] = {{50, 25, 25, 75, 75},
{50, 25, 25, 75, 75},
{100, 100, 150, 100, 150, 150, 125, 100, 150}, {100, 100, 150, 100, 150, 150, 125, 100, 150}}; int[] yPoints[] = {{10, 35, 85, 85, 35, 10},
{110, 135, 185, 185, 135},
{85, 35, 35, 85, 85, 35, 10, 35, 85},
{185, 135, 135, 185, 185, 135, 110, 135, 185}}; int nPoints[] = {5, 5, 9, 9};
g.drawPolygon (xPoints[0], yPoints[0], nPoints[0]);
g.fillPolygon (xPoints[1], yPoints[1], nPoints[1]);
g.drawPolygon (new Polygon(xPoints[2], yPoints[2], nPoints[2])); g.fillPolygon (new Polygon(xPoints[3], yPoints[3], nPoints[3]));

Drawing images

35

Java 1.0 Java 1.1

Figure 2–10: Filled and unfilled polygons

36

CHAPTER 2: SIMPLE GRAPHICS

With Java 1.1, you don’t need to use drawImage() for scaling; you can prescale the image with the Image.getScaledInstance() method, then use the previ-ous version of drawImage().

import java.awt.*;
import java.applet.*;
public class drawingImages extends Applet {
Image i, j;
public void init () {
i = getImage (getDocumentBase(), "rosey.jpg");
j = getImage (getDocumentBase(), "rosey.gif");
}
public void paint (Graphics g) {
g.drawImage (i, 10, 10, this);
g.drawImage (i, 10, 85, 150, 200, this);
g.drawImage (j, 270, 10, Color.lightGray, this);
g.drawImage (j, 270, 85, 150, 200, Color.lightGray, this); }
}

public abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) #
The drawImage() method draws a portion of image onto the screen. It takes the part of the image with corners at (sx1, sy1) and (sx2, sy2); it places this

37

drawImage() flips the image if source and destination endpoints are not the same corners, crops the image if the destination is smaller than the original size, and scales the image if the destination is larger than the original size. It does not do rotations, only flips (i.e., it can produce a mirror image or an image rotated 180 degrees but not an image rotated 90 or 270 degrees).

public abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color backgroundColor, ImageObser ver obser ver) #
The drawImage() method draws a portion of image onto the screen. It takes the part of the image with corners at (sx1, sy1) and (sx2, sy2); it places this rectangular snippet on the screen with one corner at (dx1, dy1) and another at (dx2, dy2), using observer as its ImageObserver. (Think of d for destination location and s for source image.) backgroundColor is the color of the back- ground seen through the transparent parts of the image. If no part of the image is transparent, you will not see backgroundColor. This method returns true if the object is fully drawn, false other wise.

CHAPTER 2: SIMPLE GRAPHICS

// Java 1.1 only
import java.awt.*;
import java.applet.*;
public class drawingImages11 extends Applet {
Image i, j;
public void init () {
i = getImage (getDocumentBase(), "rosey.gif");
}
public void paint (Graphics g) {
g.drawImage (i, 10, 10, this);
g.drawImage (i, 10, 85,
i.getWidth(this)+10, i.getHeight(this)+85,
i.getWidth(this), i.getHeight(this), 0, 0, this); g.drawImage (i, 270, 10,
i.getWidth(this)+270, i.getHeight(this)*2+10, 0, 0, i.getWidth(this), i.getHeight(this), Color.gray, this); g.drawImage (i, 10, 170,
i.getWidth(this)*2+10, i.getHeight(this)+170, 0, i.getHeight(this)/2, i.getWidth(this)/2, 0, this); }
}

Miscellaneous methods

39

Figure 2–12: Flipped, mirrored, and cropped images

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Margaret Dean

PageId: ELIC14530B