home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java 1.2 How-To
/
JavaHowTo.iso
/
3rdParty
/
jbuilder
/
unsupported
/
JDK1.2beta3
/
SOURCE
/
SRC.ZIP
/
java
/
awt
/
print
/
PeekGraphics.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
NeXTSTEP
RISC OS/Acorn
UTF-8
Wrap
Java Source
|
1998-03-20
|
42.8 KB
|
1,181 lines
/*
* @(#)PeekGraphics.java 1.3 98/03/18
*
* Copyright 1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package java.awt.print;
import java.util.Hashtable;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.font.GlyphSet;
import java.awt.font.StyledString;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
public class PeekGraphics extends ProxyGraphics2D implements ImageObserver {
/**
* This is the area containing all of the graphics
* drawn on the page. If it is null then we ran were
* unable to calculate a bounding box and do not know
* the area containing all of the drawing.
*/
private Area mDrawingArea = new Area();
public PeekGraphics(Graphics2D graphics, PageContext pageContext) {
super(graphics, pageContext);
}
public Area getDrawingArea() {
return mDrawingArea;
}
/* Graphics Drawing */
/**
* Draws a line, using the current color, between the points
* <code>(x1, y1)</code> and <code>(x2, y2)</code>
* in this graphics context's coordinate system.
* @param x1 the first point's <i>x</i> coordinate.
* @param y1 the first point's <i>y</i> coordinate.
* @param x2 the second point's <i>x</i> coordinate.
* @param y2 the second point's <i>y</i> coordinate.
* @since JDK1.0
*/
public void drawLine(int x1, int y1, int x2, int y2) {
int minX = x1;
int minY = y1;
int width = Math.abs(x2 - x1) + 1;
int height = Math.abs(y2 - y1) + 1;
if (minX > x2) {
minX = x2;
}
if (minY > y2) {
minY = y2;
}
addDrawingRect(minX, minY, width, height);
}
/**
* Fills the specified rectangle.
* The left and right edges of the rectangle are at
* <code>x</code> and <code>x + width - 1</code>.
* The top and bottom edges are at
* <code>y</code> and <code>y + height - 1</code>.
* The resulting rectangle covers an area
* <code>width</code> pixels wide by
* <code>height</code> pixels tall.
* The rectangle is filled using the graphics context's current color.
* @param x the <i>x</i> coordinate
* of the rectangle to be filled.
* @param y the <i>y</i> coordinate
* of the rectangle to be filled.
* @param width the width of the rectangle to be filled.
* @param height the height of the rectangle to be filled.
* @see java.awt.Graphics#fillRect
* @see java.awt.Graphics#clearRect
* @since JDK1.0
*/
public void fillRect(int x, int y, int width, int height) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
addDrawingRect(rect);
}
/**
* Clears the specified rectangle by filling it with the background
* color of the current drawing surface. This operation does not
* use the current paint mode.
* <p>
* Beginning with Java 1.1, the background color
* of offscreen images may be system dependent. Applications should
* use <code>setColor</code> followed by <code>fillRect</code> to
* ensure that an offscreen image is cleared to a specific color.
* @param x the <i>x</i> coordinate of the rectangle to clear.
* @param y the <i>y</i> coordinate of the rectangle to clear.
* @param width the width of the rectangle to clear.
* @param height the height of the rectangle to clear.
* @see java.awt.Graphics#fillRect(int, int, int, int)
* @see java.awt.Graphics#drawRect
* @see java.awt.Graphics#setColor(java.awt.Color)
* @see java.awt.Graphics#setPaintMode
* @see java.awt.Graphics#setXORMode(java.awt.Color)
* @since JDK1.0
*/
public void clearRect(int x, int y, int width, int height) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
addDrawingRect(rect);
}
/**
* Draws an outlined round-cornered rectangle using this graphics
* context's current color. The left and right edges of the rectangle
* are at <code>x</code> and <code>x + width</code>,
* respectively. The top and bottom edges of the rectangle are at
* <code>y</code> and <code>y + height</code>.
* @param x the <i>x</i> coordinate of the rectangle to be drawn.
* @param y the <i>y</i> coordinate of the rectangle to be drawn.
* @param width the width of the rectangle to be drawn.
* @param height the height of the rectangle to be drawn.
* @param arcWidth the horizontal diameter of the arc
* at the four corners.
* @param arcHeight the vertical diameter of the arc
* at the four corners.
* @see java.awt.Graphics#fillRoundRect
* @since JDK1.0
*/
public void drawRoundRect(int x, int y, int width, int height,
int arcWidth, int arcHeight) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
addDrawingRect(rect);
}
/**
* Fills the specified rounded corner rectangle with the current color.
* The left and right edges of the rectangle
* are at <code>x</code> and <code>x + width - 1</code>,
* respectively. The top and bottom edges of the rectangle are at
* <code>y</code> and <code>y + height - 1</code>.
* @param x the <i>x</i> coordinate of the rectangle to be filled.
* @param y the <i>y</i> coordinate of the rectangle to be filled.
* @param width the width of the rectangle to be filled.
* @param height the height of the rectangle to be filled.
* @param arcWidth the horizontal diameter
* of the arc at the four corners.
* @param arcHeight the vertical diameter
* of the arc at the four corners.
* @see java.awt.Graphics#drawRoundRect
* @since JDK1.0
*/
public void fillRoundRect(int x, int y, int width, int height,
int arcWidth, int arcHeight) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
addDrawingRect(rect);
}
/**
* Draws the outline of an oval.
* The result is a circle or ellipse that fits within the
* rectangle specified by the <code>x</code>, <code>y</code>,
* <code>width</code>, and <code>height</code> arguments.
* <p>
* The oval covers an area that is
* <code>width + 1</code> pixels wide
* and <code>height + 1<code> pixels tall.
* @param x the <i>x</i> coordinate of the upper left
* corner of the oval to be drawn.
* @param y the <i>y</i> coordinate of the upper left
* corner of the oval to be drawn.
* @param width the width of the oval to be drawn.
* @param height the height of the oval to be drawn.
* @see java.awt.Graphics#fillOval
* @since JDK1.0
*/
public void drawOval(int x, int y, int width, int height) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
addDrawingRect(rect);
}
/**
* Fills an oval bounded by the specified rectangle with the
* current color.
* @param x the <i>x</i> coordinate of the upper left corner
* of the oval to be filled.
* @param y the <i>y</i> coordinate of the upper left corner
* of the oval to be filled.
* @param width the width of the oval to be filled.
* @param height the height of the oval to be filled.
* @see java.awt.Graphics#drawOval
* @since JDK1.0
*/
public void fillOval(int x, int y, int width, int height) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
addDrawingRect(rect);
}
/**
* Draws the outline of a circular or elliptical arc
* covering the specified rectangle.
* <p>
* The resulting arc begins at <code>startAngle</code> and extends
* for <code>arcAngle</code> degrees, using the current color.
* Angles are interpreted such that 0 degrees
* is at the 3 o'clock position.
* A positive value indicates a counter-clockwise rotation
* while a negative value indicates a clockwise rotation.
* <p>
* The center of the arc is the center of the rectangle whose origin
* is (<i>x</i>, <i>y</i>) and whose size is specified by the
* <code>width</code> and <code>height</code> arguments.
* <p>
* The resulting arc covers an area
* <code>width + 1</code> pixels wide
* by <code>height + 1</code> pixels tall.
* @param x the <i>x</i> coordinate of the
* upper-left corner of the arc to be drawn.
* @param y the <i>y</i> coordinate of the
* upper-left corner of the arc to be drawn.
* @param width the width of the arc to be drawn.
* @param height the height of the arc to be drawn.
* @param startAngle the beginning angle.
* @param arcAngle the angular extent of the arc,
* relative to the start angle.
* @see java.awt.Graphics#fillArc
* @since JDK1.0
*/
public void drawArc(int x, int y, int width, int height,
int startAngle, int arcAngle) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
addDrawingRect(rect);
}
/**
* Fills a circular or elliptical arc covering the specified rectangle.
* <p>
* The resulting arc begins at <code>startAngle</code> and extends
* for <code>arcAngle</code> degrees.
* Angles are interpreted such that 0 degrees
* is at the 3 o'clock position.
* A positive value indicates a counter-clockwise rotation
* while a negative value indicates a clockwise rotation.
* <p>
* The center of the arc is the center of the rectangle whose origin
* is (<i>x</i>, <i>y</i>) and whose size is specified by the
* <code>width</code> and <code>height</code> arguments.
* <p>
* The resulting arc covers an area
* <code>width + 1</code> pixels wide
* by <code>height + 1</code> pixels tall.
* @param x the <i>x</i> coordinate of the
* upper-left corner of the arc to be filled.
* @param y the <i>y</i> coordinate of the
* upper-left corner of the arc to be filled.
* @param width the width of the arc to be filled.
* @param height the height of the arc to be filled.
* @param startAngle the beginning angle.
* @param arcAngle the angular extent of the arc,
* relative to the start angle.
* @see java.awt.Graphics#drawArc
* @since JDK1.0
*/
public void fillArc(int x, int y, int width, int height,
int startAngle, int arcAngle) {
Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);
addDrawingRect(rect);
}
/**
* Draws a sequence of connected lines defined by
* arrays of <i>x</i> and <i>y</i> coordinates.
* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.
* The figure is not closed if the first point
* differs from the last point.
* @param xPoints an array of <i>x</i> points
* @param yPoints an array of <i>y</i> points
* @param nPoints the total number of points
* @see java.awt.Graphics#drawPolygon(int[], int[], int)
* @since JDK1.1
*/
public void drawPolyline(int xPoints[], int yPoints[],
int nPoints) {
if (nPoints > 0) {
int x = xPoints[0];
int y = yPoints[0];
for (int i = 1; i < nPoints; i++) {
drawLine(x, y, xPoints[i], yPoints[i]);
x = xPoints[i];
y = yPoints[i];
}
}
}
/**
* Draws a closed polygon defined by
* arrays of <i>x</i> and <i>y</i> coordinates.
* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.
* <p>
* This method draws the polygon defined by <code>nPoint</code> line
* segments, where the first <code>nPoint - 1</code>
* line segments are line segments from
* <code>(xPoints[i - 1], yPoints[i - 1])</code>
* to <code>(xPoints[i], yPoints[i])</code>, for
* 1 ≤ <i>i</i> ≤ <code>nPoints</code>.
* The figure is automatically closed by drawing a line connecting
* the final point to the first point, if those points are different.
* @param xPoints a an array of <code>x</code> coordinates.
* @param yPoints a an array of <code>y</code> coordinates.
* @param nPoints a the total number of points.
* @see java.awt.Graphics#fillPolygon
* @see java.awt.Graphics#drawPolyline
* @since JDK1.0
*/
public void drawPolygon(int xPoints[], int yPoints[],
int nPoints) {
if (nPoints > 0) {
drawPolyline(xPoints, yPoints, nPoints);
drawLine(xPoints[nPoints - 1], yPoints[nPoints - 1],
xPoints[0], yPoints[0]);
}
}
/**
* Fills a closed polygon defined by
* arrays of <i>x</i> and <i>y</i> coordinates.
* <p>
* This method draws the polygon defined by <code>nPoint</code> line
* segments, where the first <code>nPoint - 1</code>
* line segments are line segments from
* <code>(xPoints[i - 1], yPoints[i - 1])</code>
* to <code>(xPoints[i], yPoints[i])</code>, for
* 1 ≤ <i>i</i> ≤ <code>nPoints</code>.
* The figure is automatically closed by drawing a line connecting
* the final point to the first point, if those points are different.
* <p>
* The area inside the polygon is defined using an
* even-odd fill rule, also known as the alternating rule.
* @param xPoints a an array of <code>x</code> coordinates.
* @param yPoints a an array of <code>y</code> coordinates.
* @param nPoints a the total number of points.
* @see java.awt.Graphics#drawPolygon(int[], int[], int)
* @since JDK1.0
*/
public void fillPolygon(int xPoints[], int yPoints[],
int nPoints) {
if (nPoints > 0) {
int minX = xPoints[0];
int minY = yPoints[0];
int maxX = xPoints[0];
int maxY = yPoints[0];
for (int i = 1; i < nPoints; i++) {
if (xPoints[i] < minX) {
minX = xPoints[i];
} else if (xPoints[i] > maxX) {
maxX = xPoints[i];
}
if (yPoints[i] < minY) {
minY = yPoints[i];
} else if (yPoints[i] > maxY) {
maxY = yPoints[i];
}
}
addDrawingRect(minX, minY, maxX - minX, maxY - minY);
}
}
/**
* Draws the text given by the specified string, using this
* graphics context's current font and color. The baseline of the
* first character is at position (<i>x</i>, <i>y</i>) in this
* graphics context's coordinate system.
* @param str the string to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @see java.awt.Graphics#drawBytes
* @see java.awt.Graphics#drawChars
* @since JDK1.0
*/
public void drawString(String str, int x, int y) {
drawString(new StyledString(str, getFont()), x, y);
}
/**
* Draws as much of the specified image as is currently available.
* The image is drawn with its top-left corner at
* (<i>x</i>, <i>y</i>) in this graphics context's coordinate
* space. Transparent pixels in the image do not affect whatever
* pixels are already there.
* <p>
* This method returns immediately in all cases, even if the
* complete image has not yet been loaded, and it has not been dithered
* and converted for the current output device.
* <p>
* If the image has not yet been completely loaded, then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param observer object to be notified as more of
* the image is converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.0
*/
public boolean drawImage(Image img, int x, int y,
ImageObserver observer) {
/* The ImageWaiter creation does not return until the
* image is loaded.
*/
ImageWaiter dim = new ImageWaiter(img);
addDrawingRect(x, y, dim.getWidth(), dim.getHeight());
return super.drawImage(img, x, y, observer);
}
/**
* Draws as much of the specified image as has already been scaled
* to fit inside the specified rectangle.
* <p>
* The image is drawn inside the specified rectangle of this
* graphics context's coordinate space, and is scaled if
* necessary. Transparent pixels do not affect whatever pixels
* are already there.
* <p>
* This method returns immediately in all cases, even if the
* entire image has not yet been scaled, dithered, and converted
* for the current output device.
* If the current output representation is not yet complete, then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the image observer by calling its <code>imageUpdate</code> method.
* <p>
* A scaled version of an image will not necessarily be
* available immediately just because an unscaled version of the
* image has been constructed for this output device. Each size of
* the image may be cached separately and generated from the original
* data in a separate image production sequence.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param width the width of the rectangle.
* @param height the height of the rectangle.
* @param observer object to be notified as more of
* the image is converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.0
*/
public boolean drawImage(Image img, int x, int y,
int width, int height,
ImageObserver observer) {
addDrawingRect(x, y, width, height);
return super.drawImage(img, x, y, width, height, observer);
}
/**
* Draws as much of the specified image as is currently available.
* The image is drawn with its top-left corner at
* (<i>x</i>, <i>y</i>) in this graphics context's coordinate
* space. Transparent pixels are drawn in the specified
* background color.
* <p>
* This operation is equivalent to filling a rectangle of the
* width and height of the specified image with the given color and then
* drawing the image on top of it, but possibly more efficient.
* <p>
* This method returns immediately in all cases, even if the
* complete image has not yet been loaded, and it has not been dithered
* and converted for the current output device.
* <p>
* If the image has not yet been completely loaded, then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param bgcolor the background color to paint under the
* non-opaque portions of the image.
* @param observer object to be notified as more of
* the image is converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.0
*/
public boolean drawImage(Image img, int x, int y,
Color bgcolor,
ImageObserver observer) {
/* The ImageWaiter creation does not return until the
* image is loaded.
*/
ImageWaiter dim = new ImageWaiter(img);
addDrawingRect(x, y, dim.getWidth(), dim.getHeight());
return super.drawImage(img, x, y, bgcolor, observer);
}
/**
* Draws as much of the specified image as has already been scaled
* to fit inside the specified rectangle.
* <p>
* The image is drawn inside the specified rectangle of this
* graphics context's coordinate space, and is scaled if
* necessary. Transparent pixels are drawn in the specified
* background color.
* This operation is equivalent to filling a rectangle of the
* width and height of the specified image with the given color and then
* drawing the image on top of it, but possibly more efficient.
* <p>
* This method returns immediately in all cases, even if the
* entire image has not yet been scaled, dithered, and converted
* for the current output device.
* If the current output representation is not yet complete then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* <p>
* A scaled version of an image will not necessarily be
* available immediately just because an unscaled version of the
* image has been constructed for this output device. Each size of
* the image may be cached separately and generated from the original
* data in a separate image production sequence.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param width the width of the rectangle.
* @param height the height of the rectangle.
* @param bgcolor the background color to paint under the
* non-opaque portions of the image.
* @param observer object to be notified as more of
* the image is converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.0
*/
public boolean drawImage(Image img, int x, int y,
int width, int height,
Color bgcolor,
ImageObserver observer) {
addDrawingRect(x, y, width, height);
return super.drawImage(img, x, y, width, height, bgcolor, observer);
}
/**
* Draws as much of the specified area of the specified image as is
* currently available, scaling it on the fly to fit inside the
* specified area of the destination drawable surface. Transparent pixels
* do not affect whatever pixels are already there.
* <p>
* This method returns immediately in all cases, even if the
* image area to be drawn has not yet been scaled, dithered, and converted
* for the current output device.
* If the current output representation is not yet complete then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* <p>
* This method always uses the unscaled version of the image
* to render the scaled rectangle and performs the required
* scaling on the fly. It does not use a cached, scaled version
* of the image for this operation. Scaling of the image from source
* to destination is performed such that the first coordinate
* of the source rectangle is mapped to the first coordinate of
* the destination rectangle, and the second source coordinate is
* mapped to the second destination coordinate. The subimage is
* scaled and flipped as needed to preserve those mappings.
* @param img the specified image to be drawn
* @param dx1 the <i>x</i> coordinate of the first corner of the
* destination rectangle.
* @param dy1 the <i>y</i> coordinate of the first corner of the
* destination rectangle.
* @param dx2 the <i>x</i> coordinate of the second corner of the
* destination rectangle.
* @param dy2 the <i>y</i> coordinate of the second corner of the
* destination rectangle.
* @param sx1 the <i>x</i> coordinate of the first corner of the
* source rectangle.
* @param sy1 the <i>y</i> coordinate of the first corner of the
* source rectangle.
* @param sx2 the <i>x</i> coordinate of the second corner of the
* source rectangle.
* @param sy2 the <i>y</i> coordinate of the second corner of the
* source rectangle.
* @param observer object to be notified as more of the image is
* scaled and converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.1
*/
public boolean drawImage(Image img,
int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2,
ImageObserver observer) {
int width = dx2 - dx1;
int height = dy2 - dy1;
addDrawingRect(dx1, dy1, width, height);
return super.drawImage(img, dx1, dy1, dx2, dy2,
sx1, sy1, sx2, sy2, observer);
}
/**
* Draws as much of the specified area of the specified image as is
* currently available, scaling it on the fly to fit inside the
* specified area of the destination drawable surface.
* <p>
* Transparent pixels are drawn in the specified background color.
* This operation is equivalent to filling a rectangle of the
* width and height of the specified image with the given color and then
* drawing the image on top of it, but possibly more efficient.
* <p>
* This method returns immediately in all cases, even if the
* image area to be drawn has not yet been scaled, dithered, and converted
* for the current output device.
* If the current output representation is not yet complete then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* <p>
* This method always uses the unscaled version of the image
* to render the scaled rectangle and performs the required
* scaling on the fly. It does not use a cached, scaled version
* of the image for this operation. Scaling of the image from source
* to destination is performed such that the first coordinate
* of the source rectangle is mapped to the first coordinate of
* the destination rectangle, and the second source coordinate is
* mapped to the second destination coordinate. The subimage is
* scaled and flipped as needed to preserve those mappings.
* @param img the specified image to be drawn
* @param dx1 the <i>x</i> coordinate of the first corner of the
* destination rectangle.
* @param dy1 the <i>y</i> coordinate of the first corner of the
* destination rectangle.
* @param dx2 the <i>x</i> coordinate of the second corner of the
* destination rectangle.
* @param dy2 the <i>y</i> coordinate of the second corner of the
* destination rectangle.
* @param sx1 the <i>x</i> coordinate of the first corner of the
* source rectangle.
* @param sy1 the <i>y</i> coordinate of the first corner of the
* source rectangle.
* @param sx2 the <i>x</i> coordinate of the second corner of the
* source rectangle.
* @param sy2 the <i>y</i> coordinate of the second corner of the
* source rectangle.
* @param bgcolor the background color to paint under the
* non-opaque portions of the image.
* @param observer object to be notified as more of the image is
* scaled and converted.
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
* @since JDK1.1
*/
public boolean drawImage(Image img,
int dx1, int dy1, int dx2, int dy2,
int sx1, int sy1, int sx2, int sy2,
Color bgcolor,
ImageObserver observer) {
int width = dx2 - dx1;
int height = dy2 - dy1;
addDrawingRect(dx1, dy1, width, height);
return super.drawImage(img, dx1, dy1, dx2, dy2,
sx1, sy1, sx2, sy2, bgcolor, observer);
}
/* Graphics2D Drawing */
/**
* Strokes the outline of a Shape using the settings of the current
* graphics state. The rendering attributes applied include the
* clip, transform, paint or color, composite and stroke attributes.
* @param s The shape to be drawn.
* @see #setStroke
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #transform
* @see #setTransform
* @see #clip
* @see #setClip
* @see #setComposite
*/
public void draw(Shape s) {
addDrawingRect(s.getBounds());
}
/**
* Draws an image, applying a transform from image space into user space
* before drawing.
* The transformation from user space into device space is done with
* the current transform in the Graphics2D.
* The given transformation is applied to the image before the
* transform attribute in the Graphics2D state is applied.
* The rendering attributes applied include the clip, transform,
* and composite attributes. Note that the result is
* undefined, if the given transform is noninvertible.
* @param img The image to be drawn.
* @param xform The transformation from image space into user space.
* @param obs The image observer to be notified as more of the image
* is converted.
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawImage(Image img,
AffineTransform xform,
ImageObserver obs) {
mDrawingArea = null;
super.drawImage(img, xform, obs);
// if (mDrawingArea != null) {
// Rectangle2D.Double bbox = new Rectangle2D.Double();
// Point2D leftTop = new Point2D.Double(0, 0);
// Point2D rightBottom = new Point2D.Double(getImageWidth(img),
// getImageHeight(img));
// xform.transform(leftTop, leftTop);
// xform.transform(rightBottom, rightBottom);
// bbox.setBoundsFromDiagonal(leftTop, rightBottom);
// addDrawingRect(bbox);
// }
}
/**
* Draws a BufferedImage that is filtered with a BufferedImageOp.
* The rendering attributes applied include the clip, transform
* and composite attributes. This is equivalent to:
* <pre>
* img1 = op.filter(img, null);
* drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);
* </pre>
* @param op The filter to be applied to the image before drawing.
* @param img The BufferedImage to be drawn.
* @param x,y The location in user space where the image should be drawn.
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawImage(BufferedImage img,
BufferedImageOp op,
int x,
int y) {
super.drawImage(img, op, x, y);
}
/**
* Draws an image, applying a transform from image space into user space
* before drawing.
* The transformation from user space into device space is done with
* the current transform in the Graphics2D.
* The given transformation is applied to the image before the
* transform attribute in the Graphics2D state is applied.
* The rendering attributes applied include the clip, transform,
* and composite attributes. Note that the result is
* undefined, if the given transform is noninvertible.
* @param img The image to be drawn.
* @param xform The transformation from image space into user space.
* @param obs The image observer to be notified as more of the image
* is converted.
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawRenderedImage(RenderedImage img,
AffineTransform xform) {
super.drawRenderedImage(img, xform);
}
public void drawRenderableImage(RenderableImage img,
AffineTransform xfrom,
Hashtable renderHints,
Hashtable renderHintsObserved) {
super.drawRenderableImage(img, xfrom, renderHints,
renderHintsObserved);
}
/**
* Draws a string of text.
* The rendering attributes applied include the clip, transform,
* paint or color, font and composite attributes.
* @param s The string to be drawn.
* @param x,y The coordinates where the string should be drawn.
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see java.awt.Graphics#setFont
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawString(String s,
float x,
float y) {
/* Map this call to the StyleString draw as we can
* ask the StyledString for its bounds.
*/
drawString(new StyledString(s, getFont()), x, y);
}
/**
* Draws a StyledString.
* The rendering attributes applied include the clip, transform,
* paint or color, and composite attributes. A Font is associated
* with each character in the StyledString.
* @param s The StyledString to be drawn.
* @param x,y The coordinates where the StyledString should be drawn.
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
* @see Font
*/
public void drawString(StyledString s,
float x,
float y) {
/* The bounding box for the StyledString has an origin at
* (0, 0) but (x, y) is at the baseline of the font so
* we need to subtract off the ascent.
*/
addDrawingRect(s.getBounds2D(), x, y - s.getAscent());
}
/**
* Draws a GlyphSet.
* The rendering attributes applied include the clip, transform,
* paint or color, and composite attributes. The glyphSet specifies
* individual glyphs from a Font.
* @param g The GlyphSet to be drawn.
* @param x,y The coordinates where the glyphs should be drawn.
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawString(GlyphSet g,
float x,
float y) {
Rectangle2D.Float bbox =
new Rectangle2D.Float(0,
-g.getAscent(),
g.getAdvance(),
g.getAscent() + g.getDescent());
addDrawingRect(bbox, x, y);
}
public void drawString(TextLayout text, float x, float y) {
addDrawingRect(text.getBounds(), x, y);
}
/**
* Fills the interior of a Shape using the settings of the current
* graphics state. The rendering attributes applied include the
* clip, transform, paint or color, and composite.
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void fill(Shape s) {
addDrawingRect(s.getBounds());
}
/**
* Concatenates the current transform of this Graphics2D with a
* translation transformation.
* This is equivalent to calling transform(T), where T is an
* AffineTransform represented by the following matrix:
* <pre>
* [ 1 0 tx ]
* [ 0 1 ty ]
* [ 0 0 1 ]
* </pre>
*/
public void translate(double tx, double ty) {
super.translate(tx, ty);
}
/**
* Concatenates the current transform of this Graphics2D with a
* rotation transformation.
* This is equivalent to calling transform(R), where R is an
* AffineTransform represented by the following matrix:
* <pre>
* [ cos(theta) -sin(theta) 0 ]
* [ sin(theta) cos(theta) 0 ]
* [ 0 0 1 ]
* </pre>
* Rotating with a positive angle theta rotates points on the positive
* x axis toward the positive y axis.
* @param theta The angle of rotation in radians.
*/
public void rotate(double theta) {
super.rotate(theta);
}
/**
* Concatenates the current transform of this Graphics2D with a
* translated rotation transformation.
* This is equivalent to the following sequence of calls:
* <pre>
* translate(x, y);
* rotate(theta);
* translate(-x, -y);
* </pre>
* Rotating with a positive angle theta rotates points on the positive
* x axis toward the positive y axis.
* @param theta The angle of rotation in radians.
* @param x The x coordinate of the origin of the rotation
* @param y The x coordinate of the origin of the rotation
*/
public void rotate(double theta, double x, double y) {
super.rotate(theta, x, y);
}
/**
* Concatenates the current transform of this Graphics2D with a
* scaling transformation.
* This is equivalent to calling transform(S), where S is an
* AffineTransform represented by the following matrix:
* <pre>
* [ sx 0 0 ]
* [ 0 sy 0 ]
* [ 0 0 1 ]
* </pre>
*/
public void scale(double sx, double sy) {
super.scale(sx, sy);
}
/**
* Concatenates the current transform of this Graphics2D with a
* shearing transformation.
* This is equivalent to calling transform(SH), where SH is an
* AffineTransform represented by the following matrix:
* <pre>
* [ 1 shx 0 ]
* [ shy 1 0 ]
* [ 0 0 1 ]
* </pre>
* @param shx The factor by which coordinates are shifted towards the
* positive X axis direction according to their Y coordinate
* @param shy The factor by which coordinates are shifted towards the
* positive Y axis direction according to their X coordinate
*/
public void shear(double shx, double shy) {
super.shear(shx, shy);
}
/**
* Translate
*/
public void translate(int x, int y) {
super.translate(x, y);
}
/* Image Observer */
/**
* Notify this object when the height or width become available
* for an image.
*/
public synchronized boolean imageUpdate(Image img, int infoFlags,
int x, int y,
int width, int height) {
boolean gotInfo = false;
if((infoFlags & (WIDTH | HEIGHT)) != 0) {
gotInfo = true;
notify();
}
return gotInfo;
}
private synchronized int getImageWidth(Image img) {
/* Wait for the width the image to
* become available.
*/
while (img.getWidth(this) == -1) {
try {
wait();
} catch (InterruptedException e) {
}
}
return img.getWidth(this);
}
private synchronized int getImageHeight(Image img) {
/* Wait for the height the image to
* become available.
*/
while (img.getHeight(this) == -1) {
try {
wait();
} catch (InterruptedException e) {
}
}
return img.getHeight(this);
}
/**
* Shift the rectangle 'rect' to the position ('x', 'y')
* and add the resulting rectangle to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect, float x, float y) {
addDrawingRect((float) (rect.getX() + x),
(float) (rect.getY() + y),
(float) rect.getWidth(),
(float) rect.getHeight());
}
private void addDrawingRect(float x, float y, float width, float height) {
Rectangle2D.Float bbox = new Rectangle2D.Float(x, y, width, height);
addDrawingRect(bbox);
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
if (mDrawingArea != null) {
Area rectArea = new Area(rect);
mDrawingArea.add(rectArea);
}
}
/**
* This private class does not return from its constructor
* until 'img's wdth and height are available.
*/
protected class ImageWaiter implements ImageObserver {
private int mWidth;
private int mHeight;
ImageWaiter(Image img) {
waitForDimensions(img);
}
public int getWidth() {
return mWidth;
}
public int getHeight() {
return mHeight;
}
synchronized private void waitForDimensions(Image img) {
mHeight = img.getHeight(null);
mWidth = img.getWidth(null);
if (mWidth < 0 || mHeight < 0) {
try {
wait();
} catch(InterruptedException e) {
// do nothing.
}
mHeight = img.getHeight(null);
mWidth = img.getWidth(null);
}
}
synchronized public boolean imageUpdate(Image image, int flags,
int x, int y, int w, int h) {
boolean dontCallMeAgain = (flags & (HEIGHT | ABORT | ERROR)) != 0;
if (dontCallMeAgain == true) {
notify();
}
return dontCallMeAgain;
}
}
}