Path: blob/master/src/java.desktop/share/classes/sun/print/PeekGraphics.java
41153 views
/*1* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.print;2627import java.util.Map;2829import java.awt.BasicStroke;30import java.awt.Color;31import java.awt.Composite;32import java.awt.Graphics;33import java.awt.Graphics2D;34import java.awt.Font;35import java.awt.FontMetrics;36import java.awt.font.FontRenderContext;37import java.awt.Graphics;38import java.awt.GraphicsConfiguration;39import java.awt.Image;40import java.awt.Paint;41import java.awt.Rectangle;42import java.awt.Shape;43import java.awt.Stroke;44import java.awt.RenderingHints;45import java.awt.RenderingHints.Key;4647import java.awt.font.GlyphVector;48import java.awt.font.TextLayout;4950import java.awt.geom.AffineTransform;51import java.awt.geom.Line2D;52import java.awt.geom.Point2D;53import java.awt.geom.Rectangle2D;54import java.awt.geom.RoundRectangle2D;55import java.awt.image.BufferedImage;56import java.awt.image.BufferedImageOp;57import java.awt.image.ImageObserver;58import java.awt.image.RenderedImage;59import java.awt.image.renderable.RenderableImage;60import java.awt.print.PrinterGraphics;61import java.awt.print.PrinterJob;6263import java.text.AttributedCharacterIterator;6465import sun.java2d.Spans;6667public class PeekGraphics extends Graphics2D68implements PrinterGraphics,69ImageObserver,70Cloneable {7172/**73* Drawing methods will be forwarded to this object.74*/75Graphics2D mGraphics;7677/**78* The PrinterJob controlling the current printing.79*/80PrinterJob mPrinterJob;8182/**83* Keeps track of where drawing occurs on the page.84*/85private Spans mDrawingArea = new Spans();8687/**88* Track information about the types of drawing89* performed by the printing application.90*/91private PeekMetrics mPrintMetrics = new PeekMetrics();9293/**94* If true the application will only be drawing AWT style95* graphics, no Java2D graphics.96*/97private boolean mAWTDrawingOnly = false;9899/**100* The new PeekGraphics2D will forward state changing101* calls to 'graphics'. 'printerJob' is stored away102* so that the printing application can get the PrinterJob103* if needed.104*/105public PeekGraphics(Graphics2D graphics, PrinterJob printerJob) {106107mGraphics = graphics;108mPrinterJob = printerJob;109}110111/**112* Return the Graphics2D object that does the drawing113* for this instance.114*/115public Graphics2D getDelegate() {116return mGraphics;117}118119/**120* Set the Graphics2D instance which will do the121* drawing.122*/123public void setDelegate(Graphics2D graphics) {124mGraphics = graphics;125}126127public PrinterJob getPrinterJob() {128return mPrinterJob;129}130131/**132* The caller promises that only AWT graphics will be drawn.133* The print system can use this information to make general134* assumptions about the types of graphics to be drawn without135* requiring the application to draw the contents multiple136* times.137*/138public void setAWTDrawingOnly() {139mAWTDrawingOnly = true;140}141142public boolean getAWTDrawingOnly() {143return mAWTDrawingOnly;144}145146/**147* Return a Spans instance describing the parts of the page in148* to which drawing occurred.149*/150public Spans getDrawingArea() {151return mDrawingArea;152}153154/**155* Returns the device configuration associated with this Graphics2D.156*/157public GraphicsConfiguration getDeviceConfiguration() {158return ((RasterPrinterJob)mPrinterJob).getPrinterGraphicsConfig();159}160161/* The Delegated Graphics Methods */162163/**164* Creates a new {@code Graphics} object that is165* a copy of this {@code Graphics} object.166* @return a new graphics context that is a copy of167* this graphics context.168* @since 1.0169*/170public Graphics create() {171PeekGraphics newGraphics = null;172173try {174newGraphics = (PeekGraphics) clone();175newGraphics.mGraphics = (Graphics2D) mGraphics.create();176177/* This exception can not happen unless this178* class no longer implements the Cloneable179* interface.180*/181} catch (CloneNotSupportedException e) {182// can never happen.183}184185return newGraphics;186}187188/**189* Translates the origin of the graphics context to the point190* (<i>x</i>, <i>y</i>) in the current coordinate system.191* Modifies this graphics context so that its new origin corresponds192* to the point (<i>x</i>, <i>y</i>) in this graphics context's193* original coordinate system. All coordinates used in subsequent194* rendering operations on this graphics context will be relative195* to this new origin.196* @param x the <i>x</i> coordinate.197* @param y the <i>y</i> coordinate.198* @since 1.0199*/200public void translate(int x, int y) {201mGraphics.translate(x, y);202}203204/**205* Concatenates the current transform of this Graphics2D with a206* translation transformation.207* This is equivalent to calling transform(T), where T is an208* AffineTransform represented by the following matrix:209* <pre>210* [ 1 0 tx ]211* [ 0 1 ty ]212* [ 0 0 1 ]213* </pre>214*/215public void translate(double tx, double ty) {216mGraphics.translate(tx, ty);217}218219/**220* Concatenates the current transform of this Graphics2D with a221* rotation transformation.222* This is equivalent to calling transform(R), where R is an223* AffineTransform represented by the following matrix:224* <pre>225* [ cos(theta) -sin(theta) 0 ]226* [ sin(theta) cos(theta) 0 ]227* [ 0 0 1 ]228* </pre>229* Rotating with a positive angle theta rotates points on the positive230* x axis toward the positive y axis.231* @param theta The angle of rotation in radians.232*/233public void rotate(double theta) {234mGraphics.rotate(theta);235}236237/**238* Concatenates the current transform of this Graphics2D with a239* translated rotation transformation.240* This is equivalent to the following sequence of calls:241* <pre>242* translate(x, y);243* rotate(theta);244* translate(-x, -y);245* </pre>246* Rotating with a positive angle theta rotates points on the positive247* x axis toward the positive y axis.248* @param theta The angle of rotation in radians.249* @param x The x coordinate of the origin of the rotation250* @param y The x coordinate of the origin of the rotation251*/252public void rotate(double theta, double x, double y) {253mGraphics.rotate(theta, x, y);254}255256/**257* Concatenates the current transform of this Graphics2D with a258* scaling transformation.259* This is equivalent to calling transform(S), where S is an260* AffineTransform represented by the following matrix:261* <pre>262* [ sx 0 0 ]263* [ 0 sy 0 ]264* [ 0 0 1 ]265* </pre>266*/267public void scale(double sx, double sy) {268mGraphics.scale(sx, sy);269}270271/**272* Concatenates the current transform of this Graphics2D with a273* shearing transformation.274* This is equivalent to calling transform(SH), where SH is an275* AffineTransform represented by the following matrix:276* <pre>277* [ 1 shx 0 ]278* [ shy 1 0 ]279* [ 0 0 1 ]280* </pre>281* @param shx The factor by which coordinates are shifted towards the282* positive X axis direction according to their Y coordinate283* @param shy The factor by which coordinates are shifted towards the284* positive Y axis direction according to their X coordinate285*/286public void shear(double shx, double shy) {287mGraphics.shear(shx, shy);288}289290/**291* Gets this graphics context's current color.292* @return this graphics context's current color.293* @see java.awt.Color294* @see java.awt.Graphics#setColor295* @since 1.0296*/297public Color getColor() {298return mGraphics.getColor();299}300301/**302* Sets this graphics context's current color to the specified303* color. All subsequent graphics operations using this graphics304* context use this specified color.305* @param c the new rendering color.306* @see java.awt.Color307* @see java.awt.Graphics#getColor308* @since 1.0309*/310public void setColor(Color c) {311mGraphics.setColor(c);312}313314/**315* Sets the paint mode of this graphics context to overwrite the316* destination with this graphics context's current color.317* This sets the logical pixel operation function to the paint or318* overwrite mode. All subsequent rendering operations will319* overwrite the destination with the current color.320* @since 1.0321*/322public void setPaintMode() {323mGraphics.setPaintMode();324}325326/**327* Sets the paint mode of this graphics context to alternate between328* this graphics context's current color and the new specified color.329* This specifies that logical pixel operations are performed in the330* XOR mode, which alternates pixels between the current color and331* a specified XOR color.332* <p>333* When drawing operations are performed, pixels which are the334* current color are changed to the specified color, and vice versa.335* <p>336* Pixels that are of colors other than those two colors are changed337* in an unpredictable but reversible manner; if the same figure is338* drawn twice, then all pixels are restored to their original values.339* @param c1 the XOR alternation color340* @since 1.0341*/342public void setXORMode(Color c1) {343mGraphics.setXORMode(c1);344}345346/**347* Gets the current font.348* @return this graphics context's current font.349* @see java.awt.Font350* @see java.awt.Graphics#setFont351* @since 1.0352*/353public Font getFont() {354return mGraphics.getFont();355}356357/**358* Sets this graphics context's font to the specified font.359* All subsequent text operations using this graphics context360* use this font.361* @param font the font.362* @see java.awt.Graphics#getFont363* @see java.awt.Graphics#drawChars(char[], int, int, int, int)364* @see java.awt.Graphics#drawString(String, int, int)365* @see java.awt.Graphics#drawBytes(byte[], int, int, int, int)366* @since 1.0367*/368public void setFont(Font font) {369mGraphics.setFont(font);370}371372/**373* Gets the font metrics for the specified font.374* @return the font metrics for the specified font.375* @param f the specified font376* @see java.awt.Graphics#getFont377* @see java.awt.FontMetrics378* @see java.awt.Graphics#getFontMetrics()379* @since 1.0380*/381public FontMetrics getFontMetrics(Font f) {382return mGraphics.getFontMetrics(f);383}384385/**386* Get the rendering context of the font387* within this Graphics2D context.388*/389public FontRenderContext getFontRenderContext() {390return mGraphics.getFontRenderContext();391}392393/**394* Returns the bounding rectangle of the current clipping area.395* The coordinates in the rectangle are relative to the coordinate396* system origin of this graphics context.397* @return the bounding rectangle of the current clipping area.398* @see java.awt.Graphics#getClip399* @see java.awt.Graphics#clipRect400* @see java.awt.Graphics#setClip(int, int, int, int)401* @see java.awt.Graphics#setClip(Shape)402* @since 1.1403*/404public Rectangle getClipBounds() {405return mGraphics.getClipBounds();406}407408409/**410* Intersects the current clip with the specified rectangle.411* The resulting clipping area is the intersection of the current412* clipping area and the specified rectangle.413* This method can only be used to make the current clip smaller.414* To set the current clip larger, use any of the setClip methods.415* Rendering operations have no effect outside of the clipping area.416* @param x the x coordinate of the rectangle to intersect the clip with417* @param y the y coordinate of the rectangle to intersect the clip with418* @param width the width of the rectangle to intersect the clip with419* @param height the height of the rectangle to intersect the clip with420* @see #setClip(int, int, int, int)421* @see #setClip(Shape)422*/423public void clipRect(int x, int y, int width, int height) {424mGraphics.clipRect(x, y, width, height);425}426427428/**429* Sets the current clip to the rectangle specified by the given430* coordinates.431* Rendering operations have no effect outside of the clipping area.432* @param x the <i>x</i> coordinate of the new clip rectangle.433* @param y the <i>y</i> coordinate of the new clip rectangle.434* @param width the width of the new clip rectangle.435* @param height the height of the new clip rectangle.436* @see java.awt.Graphics#clipRect437* @see java.awt.Graphics#setClip(Shape)438* @since 1.1439*/440public void setClip(int x, int y, int width, int height) {441mGraphics.setClip(x, y, width, height);442}443444/**445* Gets the current clipping area.446* @return a {@code Shape} object representing the447* current clipping area.448* @see java.awt.Graphics#getClipBounds449* @see java.awt.Graphics#clipRect450* @see java.awt.Graphics#setClip(int, int, int, int)451* @see java.awt.Graphics#setClip(Shape)452* @since 1.1453*/454public Shape getClip() {455return mGraphics.getClip();456}457458459/**460* Sets the current clipping area to an arbitrary clip shape.461* Not all objects which implement the {@code Shape}462* interface can be used to set the clip. The only463* {@code Shape} objects which are guaranteed to be464* supported are {@code Shape} objects which are465* obtained via the {@code getClip} method and via466* {@code Rectangle} objects.467* @see java.awt.Graphics#getClip()468* @see java.awt.Graphics#clipRect469* @see java.awt.Graphics#setClip(int, int, int, int)470* @since 1.1471*/472public void setClip(Shape clip) {473mGraphics.setClip(clip);474}475476477/**478* Copies an area of the component by a distance specified by479* {@code dx} and {@code dy}. From the point specified480* by {@code x} and {@code y}, this method481* copies downwards and to the right. To copy an area of the482* component to the left or upwards, specify a negative value for483* {@code dx} or {@code dy}.484* If a portion of the source rectangle lies outside the bounds485* of the component, or is obscured by another window or component,486* {@code copyArea} will be unable to copy the associated487* pixels. The area that is omitted can be refreshed by calling488* the component's {@code paint} method.489* @param x the <i>x</i> coordinate of the source rectangle.490* @param y the <i>y</i> coordinate of the source rectangle.491* @param width the width of the source rectangle.492* @param height the height of the source rectangle.493* @param dx the horizontal distance to copy the pixels.494* @param dy the vertical distance to copy the pixels.495* @since 1.0496*/497public void copyArea(int x, int y, int width, int height,498int dx, int dy) {499// This method is not supported for printing so we do nothing here.500}501502/**503* Draws a line, using the current color, between the points504* <code>(x1, y1)</code> and <code>(x2, y2)</code>505* in this graphics context's coordinate system.506* @param x1 the first point's <i>x</i> coordinate.507* @param y1 the first point's <i>y</i> coordinate.508* @param x2 the second point's <i>x</i> coordinate.509* @param y2 the second point's <i>y</i> coordinate.510* @since 1.0511*/512public void drawLine(int x1, int y1, int x2, int y2) {513addStrokeShape(new Line2D.Float(x1, y1, x2, y2));514mPrintMetrics.draw(this);515}516517518519/**520* Fills the specified rectangle.521* The left and right edges of the rectangle are at522* {@code x} and <code>x + width - 1</code>.523* The top and bottom edges are at524* {@code y} and <code>y + height - 1</code>.525* The resulting rectangle covers an area526* {@code width} pixels wide by527* {@code height} pixels tall.528* The rectangle is filled using the graphics context's current color.529* @param x the <i>x</i> coordinate530* of the rectangle to be filled.531* @param y the <i>y</i> coordinate532* of the rectangle to be filled.533* @param width the width of the rectangle to be filled.534* @param height the height of the rectangle to be filled.535* @see java.awt.Graphics#fillRect536* @see java.awt.Graphics#clearRect537* @since 1.0538*/539public void fillRect(int x, int y, int width, int height) {540541addDrawingRect(new Rectangle2D.Float(x, y, width, height));542mPrintMetrics.fill(this);543544}545546/**547* Clears the specified rectangle by filling it with the background548* color of the current drawing surface. This operation does not549* use the current paint mode.550* <p>551* Beginning with Java 1.1, the background color552* of offscreen images may be system dependent. Applications should553* use {@code setColor} followed by {@code fillRect} to554* ensure that an offscreen image is cleared to a specific color.555* @param x the <i>x</i> coordinate of the rectangle to clear.556* @param y the <i>y</i> coordinate of the rectangle to clear.557* @param width the width of the rectangle to clear.558* @param height the height of the rectangle to clear.559* @see java.awt.Graphics#fillRect(int, int, int, int)560* @see java.awt.Graphics#drawRect561* @see java.awt.Graphics#setColor(java.awt.Color)562* @see java.awt.Graphics#setPaintMode563* @see java.awt.Graphics#setXORMode(java.awt.Color)564* @since 1.0565*/566public void clearRect(int x, int y, int width, int height) {567Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);568addDrawingRect(rect);569mPrintMetrics.clear(this);570}571572/**573* Draws an outlined round-cornered rectangle using this graphics574* context's current color. The left and right edges of the rectangle575* are at {@code x} and <code>x + width</code>,576* respectively. The top and bottom edges of the rectangle are at577* {@code y} and <code>y + height</code>.578* @param x the <i>x</i> coordinate of the rectangle to be drawn.579* @param y the <i>y</i> coordinate of the rectangle to be drawn.580* @param width the width of the rectangle to be drawn.581* @param height the height of the rectangle to be drawn.582* @param arcWidth the horizontal diameter of the arc583* at the four corners.584* @param arcHeight the vertical diameter of the arc585* at the four corners.586* @see java.awt.Graphics#fillRoundRect587* @since 1.0588*/589public void drawRoundRect(int x, int y, int width, int height,590int arcWidth, int arcHeight) {591addStrokeShape(new RoundRectangle2D.Float(x, y, width, height, arcWidth, arcHeight));592mPrintMetrics.draw(this);593594}595596/**597* Fills the specified rounded corner rectangle with the current color.598* The left and right edges of the rectangle599* are at {@code x} and <code>x + width - 1</code>,600* respectively. The top and bottom edges of the rectangle are at601* {@code y} and <code>y + height - 1</code>.602* @param x the <i>x</i> coordinate of the rectangle to be filled.603* @param y the <i>y</i> coordinate of the rectangle to be filled.604* @param width the width of the rectangle to be filled.605* @param height the height of the rectangle to be filled.606* @param arcWidth the horizontal diameter607* of the arc at the four corners.608* @param arcHeight the vertical diameter609* of the arc at the four corners.610* @see java.awt.Graphics#drawRoundRect611* @since 1.0612*/613public void fillRoundRect(int x, int y, int width, int height,614int arcWidth, int arcHeight) {615Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);616addDrawingRect(rect);617mPrintMetrics.fill(this);618}619620/**621* Draws the outline of an oval.622* The result is a circle or ellipse that fits within the623* rectangle specified by the {@code x}, {@code y},624* {@code width}, and {@code height} arguments.625* <p>626* The oval covers an area that is627* <code>width + 1</code> pixels wide628* and <code>height + 1</code> pixels tall.629* @param x the <i>x</i> coordinate of the upper left630* corner of the oval to be drawn.631* @param y the <i>y</i> coordinate of the upper left632* corner of the oval to be drawn.633* @param width the width of the oval to be drawn.634* @param height the height of the oval to be drawn.635* @see java.awt.Graphics#fillOval636* @since 1.0637*/638public void drawOval(int x, int y, int width, int height) {639addStrokeShape(new Rectangle2D.Float(x, y, width, height));640mPrintMetrics.draw(this);641}642643/**644* Fills an oval bounded by the specified rectangle with the645* current color.646* @param x the <i>x</i> coordinate of the upper left corner647* of the oval to be filled.648* @param y the <i>y</i> coordinate of the upper left corner649* of the oval to be filled.650* @param width the width of the oval to be filled.651* @param height the height of the oval to be filled.652* @see java.awt.Graphics#drawOval653* @since 1.0654*/655public void fillOval(int x, int y, int width, int height) {656Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);657addDrawingRect(rect);658mPrintMetrics.fill(this);659660}661662663/**664* Draws the outline of a circular or elliptical arc665* covering the specified rectangle.666* <p>667* The resulting arc begins at {@code startAngle} and extends668* for {@code arcAngle} degrees, using the current color.669* Angles are interpreted such that 0 degrees670* is at the 3 o'clock position.671* A positive value indicates a counter-clockwise rotation672* while a negative value indicates a clockwise rotation.673* <p>674* The center of the arc is the center of the rectangle whose origin675* is (<i>x</i>, <i>y</i>) and whose size is specified by the676* {@code width} and {@code height} arguments.677* <p>678* The resulting arc covers an area679* <code>width + 1</code> pixels wide680* by <code>height + 1</code> pixels tall.681* @param x the <i>x</i> coordinate of the682* upper-left corner of the arc to be drawn.683* @param y the <i>y</i> coordinate of the684* upper-left corner of the arc to be drawn.685* @param width the width of the arc to be drawn.686* @param height the height of the arc to be drawn.687* @param startAngle the beginning angle.688* @param arcAngle the angular extent of the arc,689* relative to the start angle.690* @see java.awt.Graphics#fillArc691* @since 1.0692*/693public void drawArc(int x, int y, int width, int height,694int startAngle, int arcAngle) {695addStrokeShape(new Rectangle2D.Float(x, y, width, height));696mPrintMetrics.draw(this);697698}699700/**701* Fills a circular or elliptical arc covering the specified rectangle.702* <p>703* The resulting arc begins at {@code startAngle} and extends704* for {@code arcAngle} degrees.705* Angles are interpreted such that 0 degrees706* is at the 3 o'clock position.707* A positive value indicates a counter-clockwise rotation708* while a negative value indicates a clockwise rotation.709* <p>710* The center of the arc is the center of the rectangle whose origin711* is (<i>x</i>, <i>y</i>) and whose size is specified by the712* {@code width} and {@code height} arguments.713* <p>714* The resulting arc covers an area715* <code>width + 1</code> pixels wide716* by <code>height + 1</code> pixels tall.717* @param x the <i>x</i> coordinate of the718* upper-left corner of the arc to be filled.719* @param y the <i>y</i> coordinate of the720* upper-left corner of the arc to be filled.721* @param width the width of the arc to be filled.722* @param height the height of the arc to be filled.723* @param startAngle the beginning angle.724* @param arcAngle the angular extent of the arc,725* relative to the start angle.726* @see java.awt.Graphics#drawArc727* @since 1.0728*/729public void fillArc(int x, int y, int width, int height,730int startAngle, int arcAngle) {731Rectangle2D.Float rect = new Rectangle2D.Float(x, y,width, height);732addDrawingRect(rect);733mPrintMetrics.fill(this);734735}736737/**738* Draws a sequence of connected lines defined by739* arrays of <i>x</i> and <i>y</i> coordinates.740* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.741* The figure is not closed if the first point742* differs from the last point.743* @param xPoints an array of <i>x</i> points744* @param yPoints an array of <i>y</i> points745* @param nPoints the total number of points746* @see java.awt.Graphics#drawPolygon(int[], int[], int)747* @since 1.1748*/749public void drawPolyline(int[] xPoints, int[] yPoints,750int nPoints) {751if (nPoints > 0) {752int x = xPoints[0];753int y = yPoints[0];754755for (int i = 1; i < nPoints; i++) {756drawLine(x, y, xPoints[i], yPoints[i]);757x = xPoints[i];758y = yPoints[i];759}760}761762}763764/**765* Draws a closed polygon defined by766* arrays of <i>x</i> and <i>y</i> coordinates.767* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.768* <p>769* This method draws the polygon defined by {@code nPoint} line770* segments, where the first <code>nPoint - 1</code>771* line segments are line segments from772* <code>(xPoints[i - 1], yPoints[i - 1])</code>773* to <code>(xPoints[i], yPoints[i])</code>, for774* 1 ≤ <i>i</i> ≤ {@code nPoints}.775* The figure is automatically closed by drawing a line connecting776* the final point to the first point, if those points are different.777* @param xPoints a an array of {@code x} coordinates.778* @param yPoints a an array of {@code y} coordinates.779* @param nPoints a the total number of points.780* @see java.awt.Graphics#fillPolygon781* @see java.awt.Graphics#drawPolyline782* @since 1.0783*/784public void drawPolygon(int[] xPoints, int[] yPoints,785int nPoints) {786if (nPoints > 0) {787drawPolyline(xPoints, yPoints, nPoints);788drawLine(xPoints[nPoints - 1], yPoints[nPoints - 1],789xPoints[0], yPoints[0]);790}791792}793794/**795* Fills a closed polygon defined by796* arrays of <i>x</i> and <i>y</i> coordinates.797* <p>798* This method draws the polygon defined by {@code nPoint} line799* segments, where the first <code>nPoint - 1</code>800* line segments are line segments from801* <code>(xPoints[i - 1], yPoints[i - 1])</code>802* to <code>(xPoints[i], yPoints[i])</code>, for803* 1 ≤ <i>i</i> ≤ {@code nPoints}.804* The figure is automatically closed by drawing a line connecting805* the final point to the first point, if those points are different.806* <p>807* The area inside the polygon is defined using an808* even-odd fill rule, also known as the alternating rule.809* @param xPoints a an array of {@code x} coordinates.810* @param yPoints a an array of {@code y} coordinates.811* @param nPoints a the total number of points.812* @see java.awt.Graphics#drawPolygon(int[], int[], int)813* @since 1.0814*/815public void fillPolygon(int[] xPoints, int[] yPoints,816int nPoints) {817if (nPoints > 0) {818int minX = xPoints[0];819int minY = yPoints[0];820int maxX = xPoints[0];821int maxY = yPoints[0];822823for (int i = 1; i < nPoints; i++) {824825if (xPoints[i] < minX) {826minX = xPoints[i];827} else if (xPoints[i] > maxX) {828maxX = xPoints[i];829}830831if (yPoints[i] < minY) {832minY = yPoints[i];833} else if (yPoints[i] > maxY) {834maxY = yPoints[i];835}836}837838addDrawingRect(minX, minY, maxX - minX, maxY - minY);839}840841mPrintMetrics.fill(this);842843}844845846/**847* Draws the text given by the specified string, using this848* graphics context's current font and color. The baseline of the849* first character is at position (<i>x</i>, <i>y</i>) in this850* graphics context's coordinate system.851* @param str the string to be drawn.852* @param x the <i>x</i> coordinate.853* @param y the <i>y</i> coordinate.854* @see java.awt.Graphics#drawBytes855* @see java.awt.Graphics#drawChars856* @since 1.0857*/858public void drawString(String str, int x, int y) {859860drawString(str, (float)x, (float)y);861}862863/**864* Draws the text given by the specified iterator, using this865* graphics context's current color. The iterator has to specify a font866* for each character. The baseline of the867* first character is at position (<i>x</i>, <i>y</i>) in this868* graphics context's coordinate system.869* The rendering attributes applied include the clip, transform,870* paint or color, and composite attributes.871* For characters in script systems such as Hebrew and Arabic,872* the glyphs may be draw from right to left, in which case the873* coordinate supplied is the location of the leftmost character874* on the baseline.875* @param iterator the iterator whose text is to be drawn876* @param x,y the coordinates where the iterator's text should be drawn.877* @see #setPaint878* @see java.awt.Graphics#setColor879* @see #setTransform880* @see #setComposite881* @see #setClip882*/883public void drawString(AttributedCharacterIterator iterator,884int x, int y) {885886drawString(iterator, (float)x, (float)y);887}888889/**890* Draws the text given by the specified iterator, using this891* graphics context's current color. The iterator has to specify a font892* for each character. The baseline of the893* first character is at position (<i>x</i>, <i>y</i>) in this894* graphics context's coordinate system.895* The rendering attributes applied include the clip, transform,896* paint or color, and composite attributes.897* For characters in script systems such as Hebrew and Arabic,898* the glyphs may be draw from right to left, in which case the899* coordinate supplied is the location of the leftmost character900* on the baseline.901* @param iterator the iterator whose text is to be drawn902* @param x,y the coordinates where the iterator's text should be drawn.903* @see #setPaint904* @see java.awt.Graphics#setColor905* @see #setTransform906* @see #setComposite907* @see #setClip908*/909public void drawString(AttributedCharacterIterator iterator,910float x, float y) {911if (iterator == null) {912throw new913NullPointerException("AttributedCharacterIterator is null");914}915916TextLayout layout = new TextLayout(iterator, getFontRenderContext());917layout.draw(this, x, y);918}919920921/**922* Draws as much of the specified image as is currently available.923* The image is drawn with its top-left corner at924* (<i>x</i>, <i>y</i>) in this graphics context's coordinate925* space. Transparent pixels in the image do not affect whatever926* pixels are already there.927* <p>928* This method returns immediately in all cases, even if the929* complete image has not yet been loaded, and it has not been dithered930* and converted for the current output device.931* <p>932* If the image has not yet been completely loaded, then933* {@code drawImage} returns {@code false}. As more of934* the image becomes available, the process that draws the image notifies935* the specified image observer.936* @param img the specified image to be drawn.937* @param x the <i>x</i> coordinate.938* @param y the <i>y</i> coordinate.939* @param observer object to be notified as more of940* the image is converted.941* @see java.awt.Image942* @see java.awt.image.ImageObserver943* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)944* @since 1.0945*/946public boolean drawImage(Image img, int x, int y,947ImageObserver observer) {948949if (img == null) {950return true;951}952953/* The ImageWaiter creation does not return until the954* image is loaded.955*/956ImageWaiter dim = new ImageWaiter(img);957958addDrawingRect(x, y, dim.getWidth(), dim.getHeight());959mPrintMetrics.drawImage(this, img);960961return mGraphics.drawImage(img, x, y, observer);962}963964965/**966* Draws as much of the specified image as has already been scaled967* to fit inside the specified rectangle.968* <p>969* The image is drawn inside the specified rectangle of this970* graphics context's coordinate space, and is scaled if971* necessary. Transparent pixels do not affect whatever pixels972* are already there.973* <p>974* This method returns immediately in all cases, even if the975* entire image has not yet been scaled, dithered, and converted976* for the current output device.977* If the current output representation is not yet complete, then978* {@code drawImage} returns {@code false}. As more of979* the image becomes available, the process that draws the image notifies980* the image observer by calling its {@code imageUpdate} method.981* <p>982* A scaled version of an image will not necessarily be983* available immediately just because an unscaled version of the984* image has been constructed for this output device. Each size of985* the image may be cached separately and generated from the original986* data in a separate image production sequence.987* @param img the specified image to be drawn.988* @param x the <i>x</i> coordinate.989* @param y the <i>y</i> coordinate.990* @param width the width of the rectangle.991* @param height the height of the rectangle.992* @param observer object to be notified as more of993* the image is converted.994* @see java.awt.Image995* @see java.awt.image.ImageObserver996* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)997* @since 1.0998*/999public boolean drawImage(Image img, int x, int y,1000int width, int height,1001ImageObserver observer) {10021003if (img == null) {1004return true;1005}1006addDrawingRect(x, y, width, height);1007mPrintMetrics.drawImage(this, img);10081009return mGraphics.drawImage(img, x, y, width, height, observer);10101011}10121013/**1014* Draws as much of the specified image as is currently available.1015* The image is drawn with its top-left corner at1016* (<i>x</i>, <i>y</i>) in this graphics context's coordinate1017* space. Transparent pixels are drawn in the specified1018* background color.1019* <p>1020* This operation is equivalent to filling a rectangle of the1021* width and height of the specified image with the given color and then1022* drawing the image on top of it, but possibly more efficient.1023* <p>1024* This method returns immediately in all cases, even if the1025* complete image has not yet been loaded, and it has not been dithered1026* and converted for the current output device.1027* <p>1028* If the image has not yet been completely loaded, then1029* {@code drawImage} returns {@code false}. As more of1030* the image becomes available, the process that draws the image notifies1031* the specified image observer.1032* @param img the specified image to be drawn.1033* @param x the <i>x</i> coordinate.1034* @param y the <i>y</i> coordinate.1035* @param bgcolor the background color to paint under the1036* non-opaque portions of the image.1037* @param observer object to be notified as more of1038* the image is converted.1039* @see java.awt.Image1040* @see java.awt.image.ImageObserver1041* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1042* @since 1.01043*/1044public boolean drawImage(Image img, int x, int y,1045Color bgcolor,1046ImageObserver observer) {10471048if (img == null) {1049return true;1050}10511052/* The ImageWaiter creation does not return until the1053* image is loaded.1054*/1055ImageWaiter dim = new ImageWaiter(img);10561057addDrawingRect(x, y, dim.getWidth(), dim.getHeight());1058mPrintMetrics.drawImage(this, img);10591060return mGraphics.drawImage(img, x, y, bgcolor, observer);1061}106210631064/**1065* Draws as much of the specified image as has already been scaled1066* to fit inside the specified rectangle.1067* <p>1068* The image is drawn inside the specified rectangle of this1069* graphics context's coordinate space, and is scaled if1070* necessary. Transparent pixels are drawn in the specified1071* background color.1072* This operation is equivalent to filling a rectangle of the1073* width and height of the specified image with the given color and then1074* drawing the image on top of it, but possibly more efficient.1075* <p>1076* This method returns immediately in all cases, even if the1077* entire image has not yet been scaled, dithered, and converted1078* for the current output device.1079* If the current output representation is not yet complete then1080* {@code drawImage} returns {@code false}. As more of1081* the image becomes available, the process that draws the image notifies1082* the specified image observer.1083* <p>1084* A scaled version of an image will not necessarily be1085* available immediately just because an unscaled version of the1086* image has been constructed for this output device. Each size of1087* the image may be cached separately and generated from the original1088* data in a separate image production sequence.1089* @param img the specified image to be drawn.1090* @param x the <i>x</i> coordinate.1091* @param y the <i>y</i> coordinate.1092* @param width the width of the rectangle.1093* @param height the height of the rectangle.1094* @param bgcolor the background color to paint under the1095* non-opaque portions of the image.1096* @param observer object to be notified as more of1097* the image is converted.1098* @see java.awt.Image1099* @see java.awt.image.ImageObserver1100* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1101* @since 1.01102*/1103public boolean drawImage(Image img, int x, int y,1104int width, int height,1105Color bgcolor,1106ImageObserver observer) {11071108if (img == null) {1109return true;1110}11111112addDrawingRect(x, y, width, height);1113mPrintMetrics.drawImage(this, img);11141115return mGraphics.drawImage(img, x, y, width, height, bgcolor, observer);11161117}11181119/**1120* Draws as much of the specified area of the specified image as is1121* currently available, scaling it on the fly to fit inside the1122* specified area of the destination drawable surface. Transparent pixels1123* do not affect whatever pixels are already there.1124* <p>1125* This method returns immediately in all cases, even if the1126* image area to be drawn has not yet been scaled, dithered, and converted1127* for the current output device.1128* If the current output representation is not yet complete then1129* {@code drawImage} returns {@code false}. As more of1130* the image becomes available, the process that draws the image notifies1131* the specified image observer.1132* <p>1133* This method always uses the unscaled version of the image1134* to render the scaled rectangle and performs the required1135* scaling on the fly. It does not use a cached, scaled version1136* of the image for this operation. Scaling of the image from source1137* to destination is performed such that the first coordinate1138* of the source rectangle is mapped to the first coordinate of1139* the destination rectangle, and the second source coordinate is1140* mapped to the second destination coordinate. The subimage is1141* scaled and flipped as needed to preserve those mappings.1142* @param img the specified image to be drawn1143* @param dx1 the <i>x</i> coordinate of the first corner of the1144* destination rectangle.1145* @param dy1 the <i>y</i> coordinate of the first corner of the1146* destination rectangle.1147* @param dx2 the <i>x</i> coordinate of the second corner of the1148* destination rectangle.1149* @param dy2 the <i>y</i> coordinate of the second corner of the1150* destination rectangle.1151* @param sx1 the <i>x</i> coordinate of the first corner of the1152* source rectangle.1153* @param sy1 the <i>y</i> coordinate of the first corner of the1154* source rectangle.1155* @param sx2 the <i>x</i> coordinate of the second corner of the1156* source rectangle.1157* @param sy2 the <i>y</i> coordinate of the second corner of the1158* source rectangle.1159* @param observer object to be notified as more of the image is1160* scaled and converted.1161* @see java.awt.Image1162* @see java.awt.image.ImageObserver1163* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1164* @since 1.11165*/1166public boolean drawImage(Image img,1167int dx1, int dy1, int dx2, int dy2,1168int sx1, int sy1, int sx2, int sy2,1169ImageObserver observer) {11701171if (img == null) {1172return true;1173}11741175int width = dx2 - dx1;1176int height = dy2 - dy1;11771178addDrawingRect(dx1, dy1, width, height);1179mPrintMetrics.drawImage(this, img);11801181return mGraphics.drawImage(img, dx1, dy1, dx2, dy2,1182sx1, sy1, sx2, sy2, observer);11831184}118511861187/**1188* Draws as much of the specified area of the specified image as is1189* currently available, scaling it on the fly to fit inside the1190* specified area of the destination drawable surface.1191* <p>1192* Transparent pixels are drawn in the specified background color.1193* This operation is equivalent to filling a rectangle of the1194* width and height of the specified image with the given color and then1195* drawing the image on top of it, but possibly more efficient.1196* <p>1197* This method returns immediately in all cases, even if the1198* image area to be drawn has not yet been scaled, dithered, and converted1199* for the current output device.1200* If the current output representation is not yet complete then1201* {@code drawImage} returns {@code false}. As more of1202* the image becomes available, the process that draws the image notifies1203* the specified image observer.1204* <p>1205* This method always uses the unscaled version of the image1206* to render the scaled rectangle and performs the required1207* scaling on the fly. It does not use a cached, scaled version1208* of the image for this operation. Scaling of the image from source1209* to destination is performed such that the first coordinate1210* of the source rectangle is mapped to the first coordinate of1211* the destination rectangle, and the second source coordinate is1212* mapped to the second destination coordinate. The subimage is1213* scaled and flipped as needed to preserve those mappings.1214* @param img the specified image to be drawn1215* @param dx1 the <i>x</i> coordinate of the first corner of the1216* destination rectangle.1217* @param dy1 the <i>y</i> coordinate of the first corner of the1218* destination rectangle.1219* @param dx2 the <i>x</i> coordinate of the second corner of the1220* destination rectangle.1221* @param dy2 the <i>y</i> coordinate of the second corner of the1222* destination rectangle.1223* @param sx1 the <i>x</i> coordinate of the first corner of the1224* source rectangle.1225* @param sy1 the <i>y</i> coordinate of the first corner of the1226* source rectangle.1227* @param sx2 the <i>x</i> coordinate of the second corner of the1228* source rectangle.1229* @param sy2 the <i>y</i> coordinate of the second corner of the1230* source rectangle.1231* @param bgcolor the background color to paint under the1232* non-opaque portions of the image.1233* @param observer object to be notified as more of the image is1234* scaled and converted.1235* @see java.awt.Image1236* @see java.awt.image.ImageObserver1237* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1238* @since 1.11239*/1240public boolean drawImage(Image img,1241int dx1, int dy1, int dx2, int dy2,1242int sx1, int sy1, int sx2, int sy2,1243Color bgcolor,1244ImageObserver observer) {12451246if (img == null) {1247return true;1248}12491250int width = dx2 - dx1;1251int height = dy2 - dy1;12521253addDrawingRect(dx1, dy1, width, height);1254mPrintMetrics.drawImage(this, img);12551256return mGraphics.drawImage(img, dx1, dy1, dx2, dy2,1257sx1, sy1, sx2, sy2, bgcolor, observer);12581259}126012611262/**1263* Draws an image, applying a transform from image space into user space1264* before drawing.1265* The transformation from user space into device space is done with1266* the current transform in the Graphics2D.1267* The given transformation is applied to the image before the1268* transform attribute in the Graphics2D state is applied.1269* The rendering attributes applied include the clip, transform,1270* and composite attributes. Note that the result is1271* undefined, if the given transform is noninvertible.1272* @param img The image to be drawn.1273* @param xform The transformation from image space into user space.1274* @see #transform1275* @see #setTransform1276* @see #setComposite1277* @see #clip1278* @see #setClip1279*/1280public void drawRenderedImage(RenderedImage img,1281AffineTransform xform) {12821283if (img == null) {1284return;1285}12861287mPrintMetrics.drawImage(this, img);1288mDrawingArea.addInfinite();1289}129012911292public void drawRenderableImage(RenderableImage img,1293AffineTransform xform) {12941295if (img == null) {1296return;1297}12981299mPrintMetrics.drawImage(this, img);1300mDrawingArea.addInfinite();1301}13021303/**1304* Disposes of this graphics context and releases1305* any system resources that it is using.1306* A {@code Graphics} object cannot be used after1307* {@code dispose} has been called.1308* <p>1309* When a Java program runs, a large number of {@code Graphics}1310* objects can be created within a short time frame.1311* Although the finalization process of the garbage collector1312* also disposes of the same system resources, it is preferable1313* to manually free the associated resources by calling this1314* method rather than to rely on a finalization process which1315* may not run to completion for a long period of time.1316* <p>1317* Graphics objects which are provided as arguments to the1318* {@code paint} and {@code update} methods1319* of components are automatically released by the system when1320* those methods return. For efficiency, programmers should1321* call {@code dispose} when finished using1322* a {@code Graphics} object only if it was created1323* directly from a component or another {@code Graphics} object.1324* @see java.awt.Graphics#finalize1325* @see java.awt.Component#paint1326* @see java.awt.Component#update1327* @see java.awt.Component#getGraphics1328* @see java.awt.Graphics#create1329* @since 1.01330*/1331public void dispose() {1332mGraphics.dispose();1333}13341335/**1336* Empty finalizer as no clean up needed here.1337*/1338@SuppressWarnings("deprecation")1339public void finalize() {1340}13411342/* The Delegated Graphics2D Methods */13431344/**1345* Strokes the outline of a Shape using the settings of the current1346* graphics state. The rendering attributes applied include the1347* clip, transform, paint or color, composite and stroke attributes.1348* @param s The shape to be drawn.1349* @see #setStroke1350* @see #setPaint1351* @see java.awt.Graphics#setColor1352* @see #transform1353* @see #setTransform1354* @see #clip1355* @see #setClip1356* @see #setComposite1357*/1358public void draw(Shape s) {1359addStrokeShape(s);1360mPrintMetrics.draw(this);1361}136213631364/**1365* Draws an image, applying a transform from image space into user space1366* before drawing.1367* The transformation from user space into device space is done with1368* the current transform in the Graphics2D.1369* The given transformation is applied to the image before the1370* transform attribute in the Graphics2D state is applied.1371* The rendering attributes applied include the clip, transform,1372* and composite attributes. Note that the result is1373* undefined, if the given transform is noninvertible.1374* @param img The image to be drawn.1375* @param xform The transformation from image space into user space.1376* @param obs The image observer to be notified as more of the image1377* is converted.1378* @see #transform1379* @see #setTransform1380* @see #setComposite1381* @see #clip1382* @see #setClip1383*/1384public boolean drawImage(Image img,1385AffineTransform xform,1386ImageObserver obs) {13871388if (img == null) {1389return true;1390}13911392mDrawingArea.addInfinite();1393mPrintMetrics.drawImage(this, img);13941395return mGraphics.drawImage(img, xform, obs);139613971398// if (mDrawingArea[0] != null) {1399// Rectangle2D.Double bbox = new Rectangle2D.Double();1400// Point2D leftTop = new Point2D.Double(0, 0);1401// Point2D rightBottom = new Point2D.Double(getImageWidth(img),1402// getImageHeight(img));14031404// xform.transform(leftTop, leftTop);1405// xform.transform(rightBottom, rightBottom);14061407// bbox.setBoundsFromDiagonal(leftTop, rightBottom);1408// addDrawingRect(bbox);14091410// }1411}141214131414/**1415* Draws a BufferedImage that is filtered with a BufferedImageOp.1416* The rendering attributes applied include the clip, transform1417* and composite attributes. This is equivalent to:1418* <pre>1419* img1 = op.filter(img, null);1420* drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);1421* </pre>1422* @param op The filter to be applied to the image before drawing.1423* @param img The BufferedImage to be drawn.1424* @param x,y The location in user space where the image should be drawn.1425* @see #transform1426* @see #setTransform1427* @see #setComposite1428* @see #clip1429* @see #setClip1430*/1431public void drawImage(BufferedImage img,1432BufferedImageOp op,1433int x,1434int y) {14351436if (img == null) {1437return;1438}14391440mPrintMetrics.drawImage(this, (RenderedImage) img);1441mDrawingArea.addInfinite();1442}144314441445/**1446* Draws a string of text.1447* The rendering attributes applied include the clip, transform,1448* paint or color, font and composite attributes.1449* @param str The string to be drawn.1450* @param x,y The coordinates where the string should be drawn.1451* @see #setPaint1452* @see java.awt.Graphics#setColor1453* @see java.awt.Graphics#setFont1454* @see #transform1455* @see #setTransform1456* @see #setComposite1457* @see #clip1458* @see #setClip1459*/1460public void drawString(String str,1461float x,1462float y) {14631464if (str.length() == 0) {1465return;1466}1467/* Logical bounds close enough and is used for GlyphVector */1468FontRenderContext frc = getFontRenderContext();1469Rectangle2D bbox = getFont().getStringBounds(str, frc);1470addDrawingRect(bbox, x, y);1471mPrintMetrics.drawText(this);1472}14731474/**1475* Draws a GlyphVector.1476* The rendering attributes applied include the clip, transform,1477* paint or color, and composite attributes. The GlyphVector specifies1478* individual glyphs from a Font.1479* @param g The GlyphVector to be drawn.1480* @param x,y The coordinates where the glyphs should be drawn.1481* @see #setPaint1482* @see java.awt.Graphics#setColor1483* @see #transform1484* @see #setTransform1485* @see #setComposite1486* @see #clip1487* @see #setClip1488*/1489public void drawGlyphVector(GlyphVector g,1490float x,1491float y) {14921493Rectangle2D bbox = g.getLogicalBounds();1494addDrawingRect(bbox, x, y);1495mPrintMetrics.drawText(this);14961497}14981499/**1500* Fills the interior of a Shape using the settings of the current1501* graphics state. The rendering attributes applied include the1502* clip, transform, paint or color, and composite.1503* @see #setPaint1504* @see java.awt.Graphics#setColor1505* @see #transform1506* @see #setTransform1507* @see #setComposite1508* @see #clip1509* @see #setClip1510*/1511public void fill(Shape s) {1512addDrawingRect(s.getBounds());1513mPrintMetrics.fill(this);15141515}151615171518/**1519* Checks to see if the outline of a Shape intersects the specified1520* Rectangle in device space.1521* The rendering attributes taken into account include the1522* clip, transform, and stroke attributes.1523* @param rect The area in device space to check for a hit.1524* @param s The shape to check for a hit.1525* @param onStroke Flag to choose between testing the stroked or1526* the filled shape.1527* @return True if there is a hit, false otherwise.1528* @see #setStroke1529* @see #fill1530* @see #draw1531* @see #transform1532* @see #setTransform1533* @see #clip1534* @see #setClip1535*/1536public boolean hit(Rectangle rect,1537Shape s,1538boolean onStroke) {15391540return mGraphics.hit(rect, s, onStroke);1541}15421543/**1544* Sets the Composite in the current graphics state. Composite is used1545* in all drawing methods such as drawImage, drawString, draw,1546* and fill. It specifies how new pixels are to be combined with1547* the existing pixels on the graphics device in the rendering process.1548* @param comp The Composite object to be used for drawing.1549* @see java.awt.Graphics#setXORMode1550* @see java.awt.Graphics#setPaintMode1551* @see java.awt.AlphaComposite1552*/1553public void setComposite(Composite comp) {1554mGraphics.setComposite(comp);1555}155615571558/**1559* Sets the Paint in the current graphics state.1560* @param paint The Paint object to be used to generate color in1561* the rendering process.1562* @see java.awt.Graphics#setColor1563* @see java.awt.GradientPaint1564* @see java.awt.TexturePaint1565*/1566public void setPaint(Paint paint) {1567mGraphics.setPaint(paint);1568}15691570/**1571* Sets the Stroke in the current graphics state.1572* @param s The Stroke object to be used to stroke a Shape in1573* the rendering process.1574* @see BasicStroke1575*/1576public void setStroke(Stroke s) {1577mGraphics.setStroke(s);1578}15791580/**1581* Sets the preferences for the rendering algorithms.1582* Hint categories include controls for rendering quality and1583* overall time/quality trade-off in the rendering process.1584* @param hintCategory The category of hint to be set.1585* @param hintValue The value indicating preferences for the specified1586* hint category.1587* @see RenderingHints1588*/1589public void setRenderingHint(Key hintCategory, Object hintValue) {1590mGraphics.setRenderingHint(hintCategory, hintValue);1591}15921593/**1594* Returns the preferences for the rendering algorithms.1595* @param hintCategory The category of hint to be set.1596* @return The preferences for rendering algorithms.1597* @see RenderingHints1598*/1599public Object getRenderingHint(Key hintCategory) {1600return mGraphics.getRenderingHint(hintCategory);1601}16021603/**1604* Sets the preferences for the rendering algorithms.1605* Hint categories include controls for rendering quality and1606* overall time/quality trade-off in the rendering process.1607* @param hints The rendering hints to be set1608* @see RenderingHints1609*/1610public void setRenderingHints(Map<?,?> hints) {1611mGraphics.setRenderingHints(hints);1612}16131614/**1615* Adds a number of preferences for the rendering algorithms.1616* Hint categories include controls for rendering quality and1617* overall time/quality trade-off in the rendering process.1618* @param hints The rendering hints to be set1619* @see RenderingHints1620*/1621public void addRenderingHints(Map<?,?> hints) {1622mGraphics.addRenderingHints(hints);1623}16241625/**1626* Gets the preferences for the rendering algorithms.1627* Hint categories include controls for rendering quality and1628* overall time/quality trade-off in the rendering process.1629* @see RenderingHints1630*/1631public RenderingHints getRenderingHints() {1632return mGraphics.getRenderingHints();1633}16341635/**1636* Composes a Transform object with the transform in this1637* Graphics2D according to the rule last-specified-first-applied.1638* If the currrent transform is Cx, the result of composition1639* with Tx is a new transform Cx'. Cx' becomes the current1640* transform for this Graphics2D.1641* Transforming a point p by the updated transform Cx' is1642* equivalent to first transforming p by Tx and then transforming1643* the result by the original transform Cx. In other words,1644* Cx'(p) = Cx(Tx(p)).1645* A copy of the Tx is made, if necessary, so further1646* modifications to Tx do not affect rendering.1647* @param Tx The Transform object to be composed with the current1648* transform.1649* @see #setTransform1650* @see AffineTransform1651*/1652public void transform(AffineTransform Tx) {1653mGraphics.transform(Tx);1654}16551656/**1657* Sets the Transform in the current graphics state.1658* @param Tx The Transform object to be used in the rendering process.1659* @see #transform1660* @see AffineTransform1661*/1662public void setTransform(AffineTransform Tx) {1663mGraphics.setTransform(Tx);1664}16651666/**1667* Returns the current Transform in the Graphics2D state.1668* @see #transform1669* @see #setTransform1670*/1671public AffineTransform getTransform() {1672return mGraphics.getTransform();1673}16741675/**1676* Returns the current Paint in the Graphics2D state.1677* @see #setPaint1678* @see java.awt.Graphics#setColor1679*/1680public Paint getPaint() {1681return mGraphics.getPaint();1682}16831684/**1685* Returns the current Composite in the Graphics2D state.1686* @see #setComposite1687*/1688public Composite getComposite() {1689return mGraphics.getComposite();1690}16911692/**1693* Sets the background color in this context used for clearing a region.1694* When Graphics2D is constructed for a component, the backgroung color is1695* inherited from the component. Setting the background color in the1696* Graphics2D context only affects the subsequent clearRect() calls and1697* not the background color of the component. To change the background1698* of the component, use appropriate methods of the component.1699* @param color The background color that should be used in1700* subsequent calls to clearRect().1701* @see #getBackground1702* @see Graphics#clearRect1703*/1704public void setBackground(Color color) {1705mGraphics.setBackground(color);1706}17071708/**1709* Returns the background color used for clearing a region.1710* @see #setBackground1711*/1712public Color getBackground() {1713return mGraphics.getBackground();1714}17151716/**1717* Returns the current Stroke in the Graphics2D state.1718* @see #setStroke1719*/1720public Stroke getStroke() {1721return mGraphics.getStroke();1722}17231724/**1725* Intersects the current clip with the interior of the specified Shape1726* and sets the current clip to the resulting intersection.1727* The indicated shape is transformed with the current transform in the1728* Graphics2D state before being intersected with the current clip.1729* This method is used to make the current clip smaller.1730* To make the clip larger, use any setClip method.1731* @param s The Shape to be intersected with the current clip.1732*/1733public void clip(Shape s) {1734mGraphics.clip(s);1735}17361737/**1738* Return true if the Rectangle {@code rect}1739* intersects the area into which the application1740* has drawn.1741*/1742public boolean hitsDrawingArea(Rectangle rect) {17431744return mDrawingArea.intersects((float) rect.getMinY(),1745(float) rect.getMaxY());1746}17471748/**1749* Return the object holding the summary of the1750* drawing done by the printing application.1751*/1752public PeekMetrics getMetrics() {1753return mPrintMetrics;1754}17551756/* Support Routines for Calculating the Drawing Area */17571758/**1759* Shift the rectangle 'rect' to the position ('x', 'y')1760* and add the resulting rectangle to the area representing1761* the part of the page which is drawn into.1762*/1763private void addDrawingRect(Rectangle2D rect, float x, float y) {17641765addDrawingRect((float) (rect.getX() + x),1766(float) (rect.getY() + y),1767(float) rect.getWidth(),1768(float) rect.getHeight());17691770}17711772private void addDrawingRect(float x, float y, float width, float height) {17731774Rectangle2D.Float bbox = new Rectangle2D.Float(x, y, width, height);1775addDrawingRect(bbox);1776}17771778/**1779* Add the rectangle 'rect' to the area representing1780* the part of the page which is drawn into.1781*/1782private void addDrawingRect(Rectangle2D rect) {17831784/* For testing purposes the following line can be uncommented.1785When uncommented it causes the entire page to be rasterized1786thus eliminating errors caused by a faulty bounding box1787calculation.1788*/1789//mDrawingArea.addInfinite();1790179117921793AffineTransform matrix = getTransform();17941795Shape transShape = matrix.createTransformedShape(rect);17961797Rectangle2D transRect = transShape.getBounds2D();17981799mDrawingArea.add((float) transRect.getMinY(),1800(float) transRect.getMaxY());180118021803}18041805/**1806* Add the stroked shape to the area representing1807* the part of the page which is drawn into.1808*/1809private void addStrokeShape(Shape s) {1810Shape transShape = getStroke().createStrokedShape(s);1811addDrawingRect(transShape.getBounds2D());1812}18131814/* Image Observer */18151816/**1817* Notify this object when the height or width become available1818* for an image.1819*/1820public synchronized boolean imageUpdate(Image img, int infoFlags,1821int x, int y,1822int width, int height) {18231824boolean gotInfo = false;18251826if((infoFlags & (WIDTH | HEIGHT)) != 0) {1827gotInfo = true;1828notify();1829}18301831return gotInfo;1832}18331834private synchronized int getImageWidth(Image img) {18351836/* Wait for the width the image to1837* become available.1838*/1839while (img.getWidth(this) == -1) {1840try {1841wait();1842} catch (InterruptedException e) {1843}1844}184518461847return img.getWidth(this);1848}18491850private synchronized int getImageHeight(Image img) {18511852/* Wait for the height the image to1853* become available.1854*/1855while (img.getHeight(this) == -1) {1856try {1857wait();1858} catch (InterruptedException e) {1859}1860}186118621863return img.getHeight(this);1864}18651866/**1867* This private class does not return from its constructor1868* until 'img's width and height are available.1869*/1870protected class ImageWaiter implements ImageObserver {18711872private int mWidth;1873private int mHeight;1874private boolean badImage = false;18751876ImageWaiter(Image img) {1877waitForDimensions(img);1878}18791880public int getWidth() {1881return mWidth;1882}18831884public int getHeight() {1885return mHeight;1886}18871888private synchronized void waitForDimensions(Image img) {1889mHeight = img.getHeight(this);1890mWidth = img.getWidth(this);1891while (!badImage && (mWidth < 0 || mHeight < 0)) {1892try {1893Thread.sleep(50);1894} catch(InterruptedException e) {1895// do nothing.1896}1897mHeight = img.getHeight(this);1898mWidth = img.getWidth(this);1899}1900if (badImage) {1901mHeight = 0;1902mWidth = 0;1903}1904}19051906public synchronized boolean imageUpdate(Image image, int flags,1907int x, int y, int w, int h) {19081909boolean dontCallMeAgain = (flags & (HEIGHT | ABORT | ERROR)) != 0;1910badImage = (flags & (ABORT | ERROR)) != 0;19111912return dontCallMeAgain;1913}19141915}1916}191719181919