Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/print/PeekGraphics.java
38829 views
/*1* Copyright (c) 1998, 2006, 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</code> object that is165* a copy of this <code>Graphics</code> object.166* @return a new graphics context that is a copy of167* this graphics context.168* @since JDK1.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 JDK1.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 JDK1.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 JDK1.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 JDK1.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 JDK1.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 JDK1.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(java.lang.String, int, int)364* @see java.awt.Graphics#drawString(byte[], int, int, int, int)365* @see java.awt.Graphics#drawBytes(char[], int, int, int, int)366* @since JDK1.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 JDK1.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 JDK1.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 JDK1.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</code> 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 JDK1.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</code>462* interface can be used to set the clip. The only463* <code>Shape</code> objects which are guaranteed to be464* supported are <code>Shape</code> objects which are465* obtained via the <code>getClip</code> method and via466* <code>Rectangle</code> objects.467* @see java.awt.Graphics#getClip()468* @see java.awt.Graphics#clipRect469* @see java.awt.Graphics#setClip(int, int, int, int)470* @since JDK1.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</code> and <code>dy</code>. From the point specified480* by <code>x</code> and <code>y</code>, 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</code> or <code>dy</code>.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</code> will be unable to copy the associated487* pixels. The area that is omitted can be refreshed by calling488* the component's <code>paint</code> 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 JDK1.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 JDK1.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</code> and <code>x + width - 1</code>.523* The top and bottom edges are at524* <code>y</code> and <code>y + height - 1</code>.525* The resulting rectangle covers an area526* <code>width</code> pixels wide by527* <code>height</code> 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 JDK1.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</code> followed by <code>fillRect</code> 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 JDK1.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</code> and <code>x + width</code>,576* respectively. The top and bottom edges of the rectangle are at577* <code>y</code> 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 JDK1.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</code> and <code>x + width - 1</code>,600* respectively. The top and bottom edges of the rectangle are at601* <code>y</code> 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 JDK1.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>, <code>y</code>,624* <code>width</code>, and <code>height</code> 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 JDK1.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 JDK1.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</code> and extends668* for <code>arcAngle</code> 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</code> and <code>height</code> 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 JDK1.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</code> and extends704* for <code>arcAngle</code> 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</code> and <code>height</code> 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 JDK1.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 JDK1.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</code> 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</code>.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</code> coordinates.778* @param yPoints a an array of <code>y</code> coordinates.779* @param nPoints a the total number of points.780* @see java.awt.Graphics#fillPolygon781* @see java.awt.Graphics#drawPolyline782* @since JDK1.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</code> 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</code>.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</code> coordinates.810* @param yPoints a an array of <code>y</code> coordinates.811* @param nPoints a the total number of points.812* @see java.awt.Graphics#drawPolygon(int[], int[], int)813* @since JDK1.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 JDK1.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 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 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</code> returns <code>false</code>. 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 JDK1.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</code> returns <code>false</code>. As more of979* the image becomes available, the process that draws the image notifies980* the image observer by calling its <code>imageUpdate</code> 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 JDK1.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</code> returns <code>false</code>. 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 JDK1.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</code> returns <code>false</code>. 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 JDK1.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</code> returns <code>false</code>. 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 JDK1.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</code> returns <code>false</code>. 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 JDK1.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</code> object cannot be used after1307* <code>dispose</code>has been called.1308* <p>1309* When a Java program runs, a large number of <code>Graphics</code>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</code> and <code>update</code> methods1319* of components are automatically released by the system when1320* those methods return. For efficiency, programmers should1321* call <code>dispose</code> when finished using1322* a <code>Graphics</code> object only if it was created1323* directly from a component or another <code>Graphics</code> 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 JDK1.01330*/1331public void dispose() {1332mGraphics.dispose();1333}13341335/**1336* Empty finalizer as no clean up needed here.1337*/1338public void finalize() {1339}13401341/* The Delegated Graphics2D Methods */13421343/**1344* Strokes the outline of a Shape using the settings of the current1345* graphics state. The rendering attributes applied include the1346* clip, transform, paint or color, composite and stroke attributes.1347* @param s The shape to be drawn.1348* @see #setStroke1349* @see #setPaint1350* @see java.awt.Graphics#setColor1351* @see #transform1352* @see #setTransform1353* @see #clip1354* @see #setClip1355* @see #setComposite1356*/1357public void draw(Shape s) {1358addStrokeShape(s);1359mPrintMetrics.draw(this);1360}136113621363/**1364* Draws an image, applying a transform from image space into user space1365* before drawing.1366* The transformation from user space into device space is done with1367* the current transform in the Graphics2D.1368* The given transformation is applied to the image before the1369* transform attribute in the Graphics2D state is applied.1370* The rendering attributes applied include the clip, transform,1371* and composite attributes. Note that the result is1372* undefined, if the given transform is noninvertible.1373* @param img The image to be drawn.1374* @param xform The transformation from image space into user space.1375* @param obs The image observer to be notified as more of the image1376* is converted.1377* @see #transform1378* @see #setTransform1379* @see #setComposite1380* @see #clip1381* @see #setClip1382*/1383public boolean drawImage(Image img,1384AffineTransform xform,1385ImageObserver obs) {13861387if (img == null) {1388return true;1389}13901391mDrawingArea.addInfinite();1392mPrintMetrics.drawImage(this, img);13931394return mGraphics.drawImage(img, xform, obs);139513961397// if (mDrawingArea[0] != null) {1398// Rectangle2D.Double bbox = new Rectangle2D.Double();1399// Point2D leftTop = new Point2D.Double(0, 0);1400// Point2D rightBottom = new Point2D.Double(getImageWidth(img),1401// getImageHeight(img));14021403// xform.transform(leftTop, leftTop);1404// xform.transform(rightBottom, rightBottom);14051406// bbox.setBoundsFromDiagonal(leftTop, rightBottom);1407// addDrawingRect(bbox);14081409// }1410}141114121413/**1414* Draws a BufferedImage that is filtered with a BufferedImageOp.1415* The rendering attributes applied include the clip, transform1416* and composite attributes. This is equivalent to:1417* <pre>1418* img1 = op.filter(img, null);1419* drawImage(img1, new AffineTransform(1f,0f,0f,1f,x,y), null);1420* </pre>1421* @param op The filter to be applied to the image before drawing.1422* @param img The BufferedImage to be drawn.1423* @param x,y The location in user space where the image should be drawn.1424* @see #transform1425* @see #setTransform1426* @see #setComposite1427* @see #clip1428* @see #setClip1429*/1430public void drawImage(BufferedImage img,1431BufferedImageOp op,1432int x,1433int y) {14341435if (img == null) {1436return;1437}14381439mPrintMetrics.drawImage(this, (RenderedImage) img);1440mDrawingArea.addInfinite();1441}144214431444/**1445* Draws a string of text.1446* The rendering attributes applied include the clip, transform,1447* paint or color, font and composite attributes.1448* @param s The string to be drawn.1449* @param x,y The coordinates where the string should be drawn.1450* @see #setPaint1451* @see java.awt.Graphics#setColor1452* @see java.awt.Graphics#setFont1453* @see #transform1454* @see #setTransform1455* @see #setComposite1456* @see #clip1457* @see #setClip1458*/1459public void drawString(String str,1460float x,1461float y) {14621463if (str.length() == 0) {1464return;1465}1466/* Logical bounds close enough and is used for GlyphVector */1467FontRenderContext frc = getFontRenderContext();1468Rectangle2D bbox = getFont().getStringBounds(str, frc);1469addDrawingRect(bbox, x, y);1470mPrintMetrics.drawText(this);1471}14721473/**1474* Draws a GlyphVector.1475* The rendering attributes applied include the clip, transform,1476* paint or color, and composite attributes. The GlyphVector specifies1477* individual glyphs from a Font.1478* @param g The GlyphVector to be drawn.1479* @param x,y The coordinates where the glyphs should be drawn.1480* @see #setPaint1481* @see java.awt.Graphics#setColor1482* @see #transform1483* @see #setTransform1484* @see #setComposite1485* @see #clip1486* @see #setClip1487*/1488public void drawGlyphVector(GlyphVector g,1489float x,1490float y) {14911492Rectangle2D bbox = g.getLogicalBounds();1493addDrawingRect(bbox, x, y);1494mPrintMetrics.drawText(this);14951496}14971498/**1499* Fills the interior of a Shape using the settings of the current1500* graphics state. The rendering attributes applied include the1501* clip, transform, paint or color, and composite.1502* @see #setPaint1503* @see java.awt.Graphics#setColor1504* @see #transform1505* @see #setTransform1506* @see #setComposite1507* @see #clip1508* @see #setClip1509*/1510public void fill(Shape s) {1511addDrawingRect(s.getBounds());1512mPrintMetrics.fill(this);15131514}151515161517/**1518* Checks to see if the outline of a Shape intersects the specified1519* Rectangle in device space.1520* The rendering attributes taken into account include the1521* clip, transform, and stroke attributes.1522* @param rect The area in device space to check for a hit.1523* @param s The shape to check for a hit.1524* @param onStroke Flag to choose between testing the stroked or1525* the filled shape.1526* @return True if there is a hit, false otherwise.1527* @see #setStroke1528* @see #fill1529* @see #draw1530* @see #transform1531* @see #setTransform1532* @see #clip1533* @see #setClip1534*/1535public boolean hit(Rectangle rect,1536Shape s,1537boolean onStroke) {15381539return mGraphics.hit(rect, s, onStroke);1540}15411542/**1543* Sets the Composite in the current graphics state. Composite is used1544* in all drawing methods such as drawImage, drawString, draw,1545* and fill. It specifies how new pixels are to be combined with1546* the existing pixels on the graphics device in the rendering process.1547* @param comp The Composite object to be used for drawing.1548* @see java.awt.Graphics#setXORMode1549* @see java.awt.Graphics#setPaintMode1550* @see AlphaComposite1551*/1552public void setComposite(Composite comp) {1553mGraphics.setComposite(comp);1554}155515561557/**1558* Sets the Paint in the current graphics state.1559* @param paint The Paint object to be used to generate color in1560* the rendering process.1561* @see java.awt.Graphics#setColor1562* @see GradientPaint1563* @see TexturePaint1564*/1565public void setPaint(Paint paint) {1566mGraphics.setPaint(paint);1567}15681569/**1570* Sets the Stroke in the current graphics state.1571* @param s The Stroke object to be used to stroke a Shape in1572* the rendering process.1573* @see BasicStroke1574*/1575public void setStroke(Stroke s) {1576mGraphics.setStroke(s);1577}15781579/**1580* Sets the preferences for the rendering algorithms.1581* Hint categories include controls for rendering quality and1582* overall time/quality trade-off in the rendering process.1583* @param hintCategory The category of hint to be set.1584* @param hintValue The value indicating preferences for the specified1585* hint category.1586* @see RenderingHints1587*/1588public void setRenderingHint(Key hintCategory, Object hintValue) {1589mGraphics.setRenderingHint(hintCategory, hintValue);1590}15911592/**1593* Returns the preferences for the rendering algorithms.1594* @param hintCategory The category of hint to be set.1595* @return The preferences for rendering algorithms.1596* @see RenderingHings1597*/1598public Object getRenderingHint(Key hintCategory) {1599return mGraphics.getRenderingHint(hintCategory);1600}16011602/**1603* Sets the preferences for the rendering algorithms.1604* Hint categories include controls for rendering quality and1605* overall time/quality trade-off in the rendering process.1606* @param hints The rendering hints to be set1607* @see RenderingHints1608*/1609public void setRenderingHints(Map<?,?> hints) {1610mGraphics.setRenderingHints(hints);1611}16121613/**1614* Adds a number of preferences for the rendering algorithms.1615* Hint categories include controls for rendering quality and1616* overall time/quality trade-off in the rendering process.1617* @param hints The rendering hints to be set1618* @see RenderingHints1619*/1620public void addRenderingHints(Map<?,?> hints) {1621mGraphics.addRenderingHints(hints);1622}16231624/**1625* Gets the preferences for the rendering algorithms.1626* Hint categories include controls for rendering quality and1627* overall time/quality trade-off in the rendering process.1628* @see RenderingHints1629*/1630public RenderingHints getRenderingHints() {1631return mGraphics.getRenderingHints();1632}16331634/**1635* Composes a Transform object with the transform in this1636* Graphics2D according to the rule last-specified-first-applied.1637* If the currrent transform is Cx, the result of composition1638* with Tx is a new transform Cx'. Cx' becomes the current1639* transform for this Graphics2D.1640* Transforming a point p by the updated transform Cx' is1641* equivalent to first transforming p by Tx and then transforming1642* the result by the original transform Cx. In other words,1643* Cx'(p) = Cx(Tx(p)).1644* A copy of the Tx is made, if necessary, so further1645* modifications to Tx do not affect rendering.1646* @param Tx The Transform object to be composed with the current1647* transform.1648* @see #setTransform1649* @see TransformChain1650* @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 TransformChain1661* @see AffineTransform1662*/1663public void setTransform(AffineTransform Tx) {1664mGraphics.setTransform(Tx);1665}16661667/**1668* Returns the current Transform in the Graphics2D state.1669* @see #transform1670* @see #setTransform1671*/1672public AffineTransform getTransform() {1673return mGraphics.getTransform();1674}16751676/**1677* Returns the current Paint in the Graphics2D state.1678* @see #setPaint1679* @see java.awt.Graphics#setColor1680*/1681public Paint getPaint() {1682return mGraphics.getPaint();1683}16841685/**1686* Returns the current Composite in the Graphics2D state.1687* @see #setComposite1688*/1689public Composite getComposite() {1690return mGraphics.getComposite();1691}16921693/**1694* Sets the background color in this context used for clearing a region.1695* When Graphics2D is constructed for a component, the backgroung color is1696* inherited from the component. Setting the background color in the1697* Graphics2D context only affects the subsequent clearRect() calls and1698* not the background color of the component. To change the background1699* of the component, use appropriate methods of the component.1700* @param color The background color that should be used in1701* subsequent calls to clearRect().1702* @see getBackground1703* @see Graphics.clearRect()1704*/1705public void setBackground(Color color) {1706mGraphics.setBackground(color);1707}17081709/**1710* Returns the background color used for clearing a region.1711* @see setBackground1712*/1713public Color getBackground() {1714return mGraphics.getBackground();1715}17161717/**1718* Returns the current Stroke in the Graphics2D state.1719* @see setStroke1720*/1721public Stroke getStroke() {1722return mGraphics.getStroke();1723}17241725/**1726* Intersects the current clip with the interior of the specified Shape1727* and sets the current clip to the resulting intersection.1728* The indicated shape is transformed with the current transform in the1729* Graphics2D state before being intersected with the current clip.1730* This method is used to make the current clip smaller.1731* To make the clip larger, use any setClip method.1732* @param s The Shape to be intersected with the current clip.1733*/1734public void clip(Shape s) {1735mGraphics.clip(s);1736}17371738/**1739* Return true if the Rectangle <code>rect</code>1740* intersects the area into which the application1741* has drawn.1742*/1743public boolean hitsDrawingArea(Rectangle rect) {17441745return mDrawingArea.intersects((float) rect.getMinY(),1746(float) rect.getMaxY());1747}17481749/**1750* Return the object holding the summary of the1751* drawing done by the printing application.1752*/1753public PeekMetrics getMetrics() {1754return mPrintMetrics;1755}17561757/* Support Routines for Calculating the Drawing Area */17581759/**1760* Shift the rectangle 'rect' to the position ('x', 'y')1761* and add the resulting rectangle to the area representing1762* the part of the page which is drawn into.1763*/1764private void addDrawingRect(Rectangle2D rect, float x, float y) {17651766addDrawingRect((float) (rect.getX() + x),1767(float) (rect.getY() + y),1768(float) rect.getWidth(),1769(float) rect.getHeight());17701771}17721773private void addDrawingRect(float x, float y, float width, float height) {17741775Rectangle2D.Float bbox = new Rectangle2D.Float(x, y, width, height);1776addDrawingRect(bbox);1777}17781779/**1780* Add the rectangle 'rect' to the area representing1781* the part of the page which is drawn into.1782*/1783private void addDrawingRect(Rectangle2D rect) {17841785/* For testing purposes the following line can be uncommented.1786When uncommented it causes the entire page to be rasterized1787thus eliminating errors caused by a faulty bounding box1788calculation.1789*/1790//mDrawingArea.addInfinite();1791179217931794AffineTransform matrix = getTransform();17951796Shape transShape = matrix.createTransformedShape(rect);17971798Rectangle2D transRect = transShape.getBounds2D();17991800mDrawingArea.add((float) transRect.getMinY(),1801(float) transRect.getMaxY());180218031804}18051806/**1807* Add the stroked shape to the area representing1808* the part of the page which is drawn into.1809*/1810private void addStrokeShape(Shape s) {1811Shape transShape = getStroke().createStrokedShape(s);1812addDrawingRect(transShape.getBounds2D());1813}18141815/* Image Observer */18161817/**1818* Notify this object when the height or width become available1819* for an image.1820*/1821public synchronized boolean imageUpdate(Image img, int infoFlags,1822int x, int y,1823int width, int height) {18241825boolean gotInfo = false;18261827if((infoFlags & (WIDTH | HEIGHT)) != 0) {1828gotInfo = true;1829notify();1830}18311832return gotInfo;1833}18341835private synchronized int getImageWidth(Image img) {18361837/* Wait for the width the image to1838* become available.1839*/1840while (img.getWidth(this) == -1) {1841try {1842wait();1843} catch (InterruptedException e) {1844}1845}184618471848return img.getWidth(this);1849}18501851private synchronized int getImageHeight(Image img) {18521853/* Wait for the height the image to1854* become available.1855*/1856while (img.getHeight(this) == -1) {1857try {1858wait();1859} catch (InterruptedException e) {1860}1861}186218631864return img.getHeight(this);1865}18661867/**1868* This private class does not return from its constructor1869* until 'img's width and height are available.1870*/1871protected class ImageWaiter implements ImageObserver {18721873private int mWidth;1874private int mHeight;1875private boolean badImage = false;18761877ImageWaiter(Image img) {1878waitForDimensions(img);1879}18801881public int getWidth() {1882return mWidth;1883}18841885public int getHeight() {1886return mHeight;1887}18881889synchronized private void waitForDimensions(Image img) {1890mHeight = img.getHeight(this);1891mWidth = img.getWidth(this);1892while (!badImage && (mWidth < 0 || mHeight < 0)) {1893try {1894Thread.sleep(50);1895} catch(InterruptedException e) {1896// do nothing.1897}1898mHeight = img.getHeight(this);1899mWidth = img.getWidth(this);1900}1901if (badImage) {1902mHeight = 0;1903mWidth = 0;1904}1905}19061907synchronized public boolean imageUpdate(Image image, int flags,1908int x, int y, int w, int h) {19091910boolean dontCallMeAgain = (flags & (HEIGHT | ABORT | ERROR)) != 0;1911badImage = (flags & (ABORT | ERROR)) != 0;19121913return dontCallMeAgain;1914}19151916}1917}191819191920