Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/Graphics.java
38829 views
/*1* Copyright (c) 1995, 2013, 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*/24package java.awt;2526import java.io.*;27import java.lang.*;28import java.util.*;29import java.awt.image.ImageObserver;30import java.text.AttributedCharacterIterator;3132/**33* The <code>Graphics</code> class is the abstract base class for34* all graphics contexts that allow an application to draw onto35* components that are realized on various devices, as well as36* onto off-screen images.37* <p>38* A <code>Graphics</code> object encapsulates state information needed39* for the basic rendering operations that Java supports. This40* state information includes the following properties:41*42* <ul>43* <li>The <code>Component</code> object on which to draw.44* <li>A translation origin for rendering and clipping coordinates.45* <li>The current clip.46* <li>The current color.47* <li>The current font.48* <li>The current logical pixel operation function (XOR or Paint).49* <li>The current XOR alternation color50* (see {@link Graphics#setXORMode}).51* </ul>52* <p>53* Coordinates are infinitely thin and lie between the pixels of the54* output device.55* Operations that draw the outline of a figure operate by traversing56* an infinitely thin path between pixels with a pixel-sized pen that hangs57* down and to the right of the anchor point on the path.58* Operations that fill a figure operate by filling the interior59* of that infinitely thin path.60* Operations that render horizontal text render the ascending61* portion of character glyphs entirely above the baseline coordinate.62* <p>63* The graphics pen hangs down and to the right from the path it traverses.64* This has the following implications:65* <ul>66* <li>If you draw a figure that covers a given rectangle, that67* figure occupies one extra row of pixels on the right and bottom edges68* as compared to filling a figure that is bounded by that same rectangle.69* <li>If you draw a horizontal line along the same <i>y</i> coordinate as70* the baseline of a line of text, that line is drawn entirely below71* the text, except for any descenders.72* </ul><p>73* All coordinates that appear as arguments to the methods of this74* <code>Graphics</code> object are considered relative to the75* translation origin of this <code>Graphics</code> object prior to76* the invocation of the method.77* <p>78* All rendering operations modify only pixels which lie within the79* area bounded by the current clip, which is specified by a {@link Shape}80* in user space and is controlled by the program using the81* <code>Graphics</code> object. This <i>user clip</i>82* is transformed into device space and combined with the83* <i>device clip</i>, which is defined by the visibility of windows and84* device extents. The combination of the user clip and device clip85* defines the <i>composite clip</i>, which determines the final clipping86* region. The user clip cannot be modified by the rendering87* system to reflect the resulting composite clip. The user clip can only88* be changed through the <code>setClip</code> or <code>clipRect</code>89* methods.90* All drawing or writing is done in the current color,91* using the current paint mode, and in the current font.92*93* @author Sami Shaio94* @author Arthur van Hoff95* @see java.awt.Component96* @see java.awt.Graphics#clipRect(int, int, int, int)97* @see java.awt.Graphics#setColor(java.awt.Color)98* @see java.awt.Graphics#setPaintMode()99* @see java.awt.Graphics#setXORMode(java.awt.Color)100* @see java.awt.Graphics#setFont(java.awt.Font)101* @since JDK1.0102*/103public abstract class Graphics {104105/**106* Constructs a new <code>Graphics</code> object.107* This constructor is the default constructor for a graphics108* context.109* <p>110* Since <code>Graphics</code> is an abstract class, applications111* cannot call this constructor directly. Graphics contexts are112* obtained from other graphics contexts or are created by calling113* <code>getGraphics</code> on a component.114* @see java.awt.Graphics#create()115* @see java.awt.Component#getGraphics116*/117protected Graphics() {118}119120/**121* Creates a new <code>Graphics</code> object that is122* a copy of this <code>Graphics</code> object.123* @return a new graphics context that is a copy of124* this graphics context.125*/126public abstract Graphics create();127128/**129* Creates a new <code>Graphics</code> object based on this130* <code>Graphics</code> object, but with a new translation and clip area.131* The new <code>Graphics</code> object has its origin132* translated to the specified point (<i>x</i>, <i>y</i>).133* Its clip area is determined by the intersection of the original134* clip area with the specified rectangle. The arguments are all135* interpreted in the coordinate system of the original136* <code>Graphics</code> object. The new graphics context is137* identical to the original, except in two respects:138*139* <ul>140* <li>141* The new graphics context is translated by (<i>x</i>, <i>y</i>).142* That is to say, the point (<code>0</code>, <code>0</code>) in the143* new graphics context is the same as (<i>x</i>, <i>y</i>) in144* the original graphics context.145* <li>146* The new graphics context has an additional clipping rectangle, in147* addition to whatever (translated) clipping rectangle it inherited148* from the original graphics context. The origin of the new clipping149* rectangle is at (<code>0</code>, <code>0</code>), and its size150* is specified by the <code>width</code> and <code>height</code>151* arguments.152* </ul>153* <p>154* @param x the <i>x</i> coordinate.155* @param y the <i>y</i> coordinate.156* @param width the width of the clipping rectangle.157* @param height the height of the clipping rectangle.158* @return a new graphics context.159* @see java.awt.Graphics#translate160* @see java.awt.Graphics#clipRect161*/162public Graphics create(int x, int y, int width, int height) {163Graphics g = create();164if (g == null) return null;165g.translate(x, y);166g.clipRect(0, 0, width, height);167return g;168}169170/**171* Translates the origin of the graphics context to the point172* (<i>x</i>, <i>y</i>) in the current coordinate system.173* Modifies this graphics context so that its new origin corresponds174* to the point (<i>x</i>, <i>y</i>) in this graphics context's175* original coordinate system. All coordinates used in subsequent176* rendering operations on this graphics context will be relative177* to this new origin.178* @param x the <i>x</i> coordinate.179* @param y the <i>y</i> coordinate.180*/181public abstract void translate(int x, int y);182183/**184* Gets this graphics context's current color.185* @return this graphics context's current color.186* @see java.awt.Color187* @see java.awt.Graphics#setColor(Color)188*/189public abstract Color getColor();190191/**192* Sets this graphics context's current color to the specified193* color. All subsequent graphics operations using this graphics194* context use this specified color.195* @param c the new rendering color.196* @see java.awt.Color197* @see java.awt.Graphics#getColor198*/199public abstract void setColor(Color c);200201/**202* Sets the paint mode of this graphics context to overwrite the203* destination with this graphics context's current color.204* This sets the logical pixel operation function to the paint or205* overwrite mode. All subsequent rendering operations will206* overwrite the destination with the current color.207*/208public abstract void setPaintMode();209210/**211* Sets the paint mode of this graphics context to alternate between212* this graphics context's current color and the new specified color.213* This specifies that logical pixel operations are performed in the214* XOR mode, which alternates pixels between the current color and215* a specified XOR color.216* <p>217* When drawing operations are performed, pixels which are the218* current color are changed to the specified color, and vice versa.219* <p>220* Pixels that are of colors other than those two colors are changed221* in an unpredictable but reversible manner; if the same figure is222* drawn twice, then all pixels are restored to their original values.223* @param c1 the XOR alternation color224*/225public abstract void setXORMode(Color c1);226227/**228* Gets the current font.229* @return this graphics context's current font.230* @see java.awt.Font231* @see java.awt.Graphics#setFont(Font)232*/233public abstract Font getFont();234235/**236* Sets this graphics context's font to the specified font.237* All subsequent text operations using this graphics context238* use this font. A null argument is silently ignored.239* @param font the font.240* @see java.awt.Graphics#getFont241* @see java.awt.Graphics#drawString(java.lang.String, int, int)242* @see java.awt.Graphics#drawBytes(byte[], int, int, int, int)243* @see java.awt.Graphics#drawChars(char[], int, int, int, int)244*/245public abstract void setFont(Font font);246247/**248* Gets the font metrics of the current font.249* @return the font metrics of this graphics250* context's current font.251* @see java.awt.Graphics#getFont252* @see java.awt.FontMetrics253* @see java.awt.Graphics#getFontMetrics(Font)254*/255public FontMetrics getFontMetrics() {256return getFontMetrics(getFont());257}258259/**260* Gets the font metrics for the specified font.261* @return the font metrics for the specified font.262* @param f the specified font263* @see java.awt.Graphics#getFont264* @see java.awt.FontMetrics265* @see java.awt.Graphics#getFontMetrics()266*/267public abstract FontMetrics getFontMetrics(Font f);268269270/**271* Returns the bounding rectangle of the current clipping area.272* This method refers to the user clip, which is independent of the273* clipping associated with device bounds and window visibility.274* If no clip has previously been set, or if the clip has been275* cleared using <code>setClip(null)</code>, this method returns276* <code>null</code>.277* The coordinates in the rectangle are relative to the coordinate278* system origin of this graphics context.279* @return the bounding rectangle of the current clipping area,280* or <code>null</code> if no clip is set.281* @see java.awt.Graphics#getClip282* @see java.awt.Graphics#clipRect283* @see java.awt.Graphics#setClip(int, int, int, int)284* @see java.awt.Graphics#setClip(Shape)285* @since JDK1.1286*/287public abstract Rectangle getClipBounds();288289/**290* Intersects the current clip with the specified rectangle.291* The resulting clipping area is the intersection of the current292* clipping area and the specified rectangle. If there is no293* current clipping area, either because the clip has never been294* set, or the clip has been cleared using <code>setClip(null)</code>,295* the specified rectangle becomes the new clip.296* This method sets the user clip, which is independent of the297* clipping associated with device bounds and window visibility.298* This method can only be used to make the current clip smaller.299* To set the current clip larger, use any of the setClip methods.300* Rendering operations have no effect outside of the clipping area.301* @param x the x coordinate of the rectangle to intersect the clip with302* @param y the y coordinate of the rectangle to intersect the clip with303* @param width the width of the rectangle to intersect the clip with304* @param height the height of the rectangle to intersect the clip with305* @see #setClip(int, int, int, int)306* @see #setClip(Shape)307*/308public abstract void clipRect(int x, int y, int width, int height);309310/**311* Sets the current clip to the rectangle specified by the given312* coordinates. This method sets the user clip, which is313* independent of the clipping associated with device bounds314* and window visibility.315* Rendering operations have no effect outside of the clipping area.316* @param x the <i>x</i> coordinate of the new clip rectangle.317* @param y the <i>y</i> coordinate of the new clip rectangle.318* @param width the width of the new clip rectangle.319* @param height the height of the new clip rectangle.320* @see java.awt.Graphics#clipRect321* @see java.awt.Graphics#setClip(Shape)322* @see java.awt.Graphics#getClip323* @since JDK1.1324*/325public abstract void setClip(int x, int y, int width, int height);326327/**328* Gets the current clipping area.329* This method returns the user clip, which is independent of the330* clipping associated with device bounds and window visibility.331* If no clip has previously been set, or if the clip has been332* cleared using <code>setClip(null)</code>, this method returns333* <code>null</code>.334* @return a <code>Shape</code> object representing the335* current clipping area, or <code>null</code> if336* no clip is set.337* @see java.awt.Graphics#getClipBounds338* @see java.awt.Graphics#clipRect339* @see java.awt.Graphics#setClip(int, int, int, int)340* @see java.awt.Graphics#setClip(Shape)341* @since JDK1.1342*/343public abstract Shape getClip();344345/**346* Sets the current clipping area to an arbitrary clip shape.347* Not all objects that implement the <code>Shape</code>348* interface can be used to set the clip. The only349* <code>Shape</code> objects that are guaranteed to be350* supported are <code>Shape</code> objects that are351* obtained via the <code>getClip</code> method and via352* <code>Rectangle</code> objects. This method sets the353* user clip, which is independent of the clipping associated354* with device bounds and window visibility.355* @param clip the <code>Shape</code> to use to set the clip356* @see java.awt.Graphics#getClip()357* @see java.awt.Graphics#clipRect358* @see java.awt.Graphics#setClip(int, int, int, int)359* @since JDK1.1360*/361public abstract void setClip(Shape clip);362363/**364* Copies an area of the component by a distance specified by365* <code>dx</code> and <code>dy</code>. From the point specified366* by <code>x</code> and <code>y</code>, this method367* copies downwards and to the right. To copy an area of the368* component to the left or upwards, specify a negative value for369* <code>dx</code> or <code>dy</code>.370* If a portion of the source rectangle lies outside the bounds371* of the component, or is obscured by another window or component,372* <code>copyArea</code> will be unable to copy the associated373* pixels. The area that is omitted can be refreshed by calling374* the component's <code>paint</code> method.375* @param x the <i>x</i> coordinate of the source rectangle.376* @param y the <i>y</i> coordinate of the source rectangle.377* @param width the width of the source rectangle.378* @param height the height of the source rectangle.379* @param dx the horizontal distance to copy the pixels.380* @param dy the vertical distance to copy the pixels.381*/382public abstract void copyArea(int x, int y, int width, int height,383int dx, int dy);384385/**386* Draws a line, using the current color, between the points387* <code>(x1, y1)</code> and <code>(x2, y2)</code>388* in this graphics context's coordinate system.389* @param x1 the first point's <i>x</i> coordinate.390* @param y1 the first point's <i>y</i> coordinate.391* @param x2 the second point's <i>x</i> coordinate.392* @param y2 the second point's <i>y</i> coordinate.393*/394public abstract void drawLine(int x1, int y1, int x2, int y2);395396/**397* Fills the specified rectangle.398* The left and right edges of the rectangle are at399* <code>x</code> and <code>x + width - 1</code>.400* The top and bottom edges are at401* <code>y</code> and <code>y + height - 1</code>.402* The resulting rectangle covers an area403* <code>width</code> pixels wide by404* <code>height</code> pixels tall.405* The rectangle is filled using the graphics context's current color.406* @param x the <i>x</i> coordinate407* of the rectangle to be filled.408* @param y the <i>y</i> coordinate409* of the rectangle to be filled.410* @param width the width of the rectangle to be filled.411* @param height the height of the rectangle to be filled.412* @see java.awt.Graphics#clearRect413* @see java.awt.Graphics#drawRect414*/415public abstract void fillRect(int x, int y, int width, int height);416417/**418* Draws the outline of the specified rectangle.419* The left and right edges of the rectangle are at420* <code>x</code> and <code>x + width</code>.421* The top and bottom edges are at422* <code>y</code> and <code>y + height</code>.423* The rectangle is drawn using the graphics context's current color.424* @param x the <i>x</i> coordinate425* of the rectangle to be drawn.426* @param y the <i>y</i> coordinate427* of the rectangle to be drawn.428* @param width the width of the rectangle to be drawn.429* @param height the height of the rectangle to be drawn.430* @see java.awt.Graphics#fillRect431* @see java.awt.Graphics#clearRect432*/433public void drawRect(int x, int y, int width, int height) {434if ((width < 0) || (height < 0)) {435return;436}437438if (height == 0 || width == 0) {439drawLine(x, y, x + width, y + height);440} else {441drawLine(x, y, x + width - 1, y);442drawLine(x + width, y, x + width, y + height - 1);443drawLine(x + width, y + height, x + 1, y + height);444drawLine(x, y + height, x, y + 1);445}446}447448/**449* Clears the specified rectangle by filling it with the background450* color of the current drawing surface. This operation does not451* use the current paint mode.452* <p>453* Beginning with Java 1.1, the background color454* of offscreen images may be system dependent. Applications should455* use <code>setColor</code> followed by <code>fillRect</code> to456* ensure that an offscreen image is cleared to a specific color.457* @param x the <i>x</i> coordinate of the rectangle to clear.458* @param y the <i>y</i> coordinate of the rectangle to clear.459* @param width the width of the rectangle to clear.460* @param height the height of the rectangle to clear.461* @see java.awt.Graphics#fillRect(int, int, int, int)462* @see java.awt.Graphics#drawRect463* @see java.awt.Graphics#setColor(java.awt.Color)464* @see java.awt.Graphics#setPaintMode465* @see java.awt.Graphics#setXORMode(java.awt.Color)466*/467public abstract void clearRect(int x, int y, int width, int height);468469/**470* Draws an outlined round-cornered rectangle using this graphics471* context's current color. The left and right edges of the rectangle472* are at <code>x</code> and <code>x + width</code>,473* respectively. The top and bottom edges of the rectangle are at474* <code>y</code> and <code>y + height</code>.475* @param x the <i>x</i> coordinate of the rectangle to be drawn.476* @param y the <i>y</i> coordinate of the rectangle to be drawn.477* @param width the width of the rectangle to be drawn.478* @param height the height of the rectangle to be drawn.479* @param arcWidth the horizontal diameter of the arc480* at the four corners.481* @param arcHeight the vertical diameter of the arc482* at the four corners.483* @see java.awt.Graphics#fillRoundRect484*/485public abstract void drawRoundRect(int x, int y, int width, int height,486int arcWidth, int arcHeight);487488/**489* Fills the specified rounded corner rectangle with the current color.490* The left and right edges of the rectangle491* are at <code>x</code> and <code>x + width - 1</code>,492* respectively. The top and bottom edges of the rectangle are at493* <code>y</code> and <code>y + height - 1</code>.494* @param x the <i>x</i> coordinate of the rectangle to be filled.495* @param y the <i>y</i> coordinate of the rectangle to be filled.496* @param width the width of the rectangle to be filled.497* @param height the height of the rectangle to be filled.498* @param arcWidth the horizontal diameter499* of the arc at the four corners.500* @param arcHeight the vertical diameter501* of the arc at the four corners.502* @see java.awt.Graphics#drawRoundRect503*/504public abstract void fillRoundRect(int x, int y, int width, int height,505int arcWidth, int arcHeight);506507/**508* Draws a 3-D highlighted outline of the specified rectangle.509* The edges of the rectangle are highlighted so that they510* appear to be beveled and lit from the upper left corner.511* <p>512* The colors used for the highlighting effect are determined513* based on the current color.514* The resulting rectangle covers an area that is515* <code>width + 1</code> pixels wide516* by <code>height + 1</code> pixels tall.517* @param x the <i>x</i> coordinate of the rectangle to be drawn.518* @param y the <i>y</i> coordinate of the rectangle to be drawn.519* @param width the width of the rectangle to be drawn.520* @param height the height of the rectangle to be drawn.521* @param raised a boolean that determines whether the rectangle522* appears to be raised above the surface523* or sunk into the surface.524* @see java.awt.Graphics#fill3DRect525*/526public void draw3DRect(int x, int y, int width, int height,527boolean raised) {528Color c = getColor();529Color brighter = c.brighter();530Color darker = c.darker();531532setColor(raised ? brighter : darker);533drawLine(x, y, x, y + height);534drawLine(x + 1, y, x + width - 1, y);535setColor(raised ? darker : brighter);536drawLine(x + 1, y + height, x + width, y + height);537drawLine(x + width, y, x + width, y + height - 1);538setColor(c);539}540541/**542* Paints a 3-D highlighted rectangle filled with the current color.543* The edges of the rectangle will be highlighted so that it appears544* as if the edges were beveled and lit from the upper left corner.545* The colors used for the highlighting effect will be determined from546* the current color.547* @param x the <i>x</i> coordinate of the rectangle to be filled.548* @param y the <i>y</i> coordinate of the rectangle to be filled.549* @param width the width of the rectangle to be filled.550* @param height the height of the rectangle to be filled.551* @param raised a boolean value that determines whether the552* rectangle appears to be raised above the surface553* or etched into the surface.554* @see java.awt.Graphics#draw3DRect555*/556public void fill3DRect(int x, int y, int width, int height,557boolean raised) {558Color c = getColor();559Color brighter = c.brighter();560Color darker = c.darker();561562if (!raised) {563setColor(darker);564}565fillRect(x+1, y+1, width-2, height-2);566setColor(raised ? brighter : darker);567drawLine(x, y, x, y + height - 1);568drawLine(x + 1, y, x + width - 2, y);569setColor(raised ? darker : brighter);570drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);571drawLine(x + width - 1, y, x + width - 1, y + height - 2);572setColor(c);573}574575/**576* Draws the outline of an oval.577* The result is a circle or ellipse that fits within the578* rectangle specified by the <code>x</code>, <code>y</code>,579* <code>width</code>, and <code>height</code> arguments.580* <p>581* The oval covers an area that is582* <code>width + 1</code> pixels wide583* and <code>height + 1</code> pixels tall.584* @param x the <i>x</i> coordinate of the upper left585* corner of the oval to be drawn.586* @param y the <i>y</i> coordinate of the upper left587* corner of the oval to be drawn.588* @param width the width of the oval to be drawn.589* @param height the height of the oval to be drawn.590* @see java.awt.Graphics#fillOval591*/592public abstract void drawOval(int x, int y, int width, int height);593594/**595* Fills an oval bounded by the specified rectangle with the596* current color.597* @param x the <i>x</i> coordinate of the upper left corner598* of the oval to be filled.599* @param y the <i>y</i> coordinate of the upper left corner600* of the oval to be filled.601* @param width the width of the oval to be filled.602* @param height the height of the oval to be filled.603* @see java.awt.Graphics#drawOval604*/605public abstract void fillOval(int x, int y, int width, int height);606607/**608* Draws the outline of a circular or elliptical arc609* covering the specified rectangle.610* <p>611* The resulting arc begins at <code>startAngle</code> and extends612* for <code>arcAngle</code> degrees, using the current color.613* Angles are interpreted such that 0 degrees614* is at the 3 o'clock position.615* A positive value indicates a counter-clockwise rotation616* while a negative value indicates a clockwise rotation.617* <p>618* The center of the arc is the center of the rectangle whose origin619* is (<i>x</i>, <i>y</i>) and whose size is specified by the620* <code>width</code> and <code>height</code> arguments.621* <p>622* The resulting arc covers an area623* <code>width + 1</code> pixels wide624* by <code>height + 1</code> pixels tall.625* <p>626* The angles are specified relative to the non-square extents of627* the bounding rectangle such that 45 degrees always falls on the628* line from the center of the ellipse to the upper right corner of629* the bounding rectangle. As a result, if the bounding rectangle is630* noticeably longer in one axis than the other, the angles to the631* start and end of the arc segment will be skewed farther along the632* longer axis of the bounds.633* @param x the <i>x</i> coordinate of the634* upper-left corner of the arc to be drawn.635* @param y the <i>y</i> coordinate of the636* upper-left corner of the arc to be drawn.637* @param width the width of the arc to be drawn.638* @param height the height of the arc to be drawn.639* @param startAngle the beginning angle.640* @param arcAngle the angular extent of the arc,641* relative to the start angle.642* @see java.awt.Graphics#fillArc643*/644public abstract void drawArc(int x, int y, int width, int height,645int startAngle, int arcAngle);646647/**648* Fills a circular or elliptical arc covering the specified rectangle.649* <p>650* The resulting arc begins at <code>startAngle</code> and extends651* for <code>arcAngle</code> degrees.652* Angles are interpreted such that 0 degrees653* is at the 3 o'clock position.654* A positive value indicates a counter-clockwise rotation655* while a negative value indicates a clockwise rotation.656* <p>657* The center of the arc is the center of the rectangle whose origin658* is (<i>x</i>, <i>y</i>) and whose size is specified by the659* <code>width</code> and <code>height</code> arguments.660* <p>661* The resulting arc covers an area662* <code>width + 1</code> pixels wide663* by <code>height + 1</code> pixels tall.664* <p>665* The angles are specified relative to the non-square extents of666* the bounding rectangle such that 45 degrees always falls on the667* line from the center of the ellipse to the upper right corner of668* the bounding rectangle. As a result, if the bounding rectangle is669* noticeably longer in one axis than the other, the angles to the670* start and end of the arc segment will be skewed farther along the671* longer axis of the bounds.672* @param x the <i>x</i> coordinate of the673* upper-left corner of the arc to be filled.674* @param y the <i>y</i> coordinate of the675* upper-left corner of the arc to be filled.676* @param width the width of the arc to be filled.677* @param height the height of the arc to be filled.678* @param startAngle the beginning angle.679* @param arcAngle the angular extent of the arc,680* relative to the start angle.681* @see java.awt.Graphics#drawArc682*/683public abstract void fillArc(int x, int y, int width, int height,684int startAngle, int arcAngle);685686/**687* Draws a sequence of connected lines defined by688* arrays of <i>x</i> and <i>y</i> coordinates.689* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.690* The figure is not closed if the first point691* differs from the last point.692* @param xPoints an array of <i>x</i> points693* @param yPoints an array of <i>y</i> points694* @param nPoints the total number of points695* @see java.awt.Graphics#drawPolygon(int[], int[], int)696* @since JDK1.1697*/698public abstract void drawPolyline(int xPoints[], int yPoints[],699int nPoints);700701/**702* Draws a closed polygon defined by703* arrays of <i>x</i> and <i>y</i> coordinates.704* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.705* <p>706* This method draws the polygon defined by <code>nPoint</code> line707* segments, where the first <code>nPoint - 1</code>708* line segments are line segments from709* <code>(xPoints[i - 1], yPoints[i - 1])</code>710* to <code>(xPoints[i], yPoints[i])</code>, for711* 1 ≤ <i>i</i> ≤ <code>nPoints</code>.712* The figure is automatically closed by drawing a line connecting713* the final point to the first point, if those points are different.714* @param xPoints a an array of <code>x</code> coordinates.715* @param yPoints a an array of <code>y</code> coordinates.716* @param nPoints a the total number of points.717* @see java.awt.Graphics#fillPolygon718* @see java.awt.Graphics#drawPolyline719*/720public abstract void drawPolygon(int xPoints[], int yPoints[],721int nPoints);722723/**724* Draws the outline of a polygon defined by the specified725* <code>Polygon</code> object.726* @param p the polygon to draw.727* @see java.awt.Graphics#fillPolygon728* @see java.awt.Graphics#drawPolyline729*/730public void drawPolygon(Polygon p) {731drawPolygon(p.xpoints, p.ypoints, p.npoints);732}733734/**735* Fills a closed polygon defined by736* arrays of <i>x</i> and <i>y</i> coordinates.737* <p>738* This method draws the polygon defined by <code>nPoint</code> line739* segments, where the first <code>nPoint - 1</code>740* line segments are line segments from741* <code>(xPoints[i - 1], yPoints[i - 1])</code>742* to <code>(xPoints[i], yPoints[i])</code>, for743* 1 ≤ <i>i</i> ≤ <code>nPoints</code>.744* The figure is automatically closed by drawing a line connecting745* the final point to the first point, if those points are different.746* <p>747* The area inside the polygon is defined using an748* even-odd fill rule, also known as the alternating rule.749* @param xPoints a an array of <code>x</code> coordinates.750* @param yPoints a an array of <code>y</code> coordinates.751* @param nPoints a the total number of points.752* @see java.awt.Graphics#drawPolygon(int[], int[], int)753*/754public abstract void fillPolygon(int xPoints[], int yPoints[],755int nPoints);756757/**758* Fills the polygon defined by the specified Polygon object with759* the graphics context's current color.760* <p>761* The area inside the polygon is defined using an762* even-odd fill rule, also known as the alternating rule.763* @param p the polygon to fill.764* @see java.awt.Graphics#drawPolygon(int[], int[], int)765*/766public void fillPolygon(Polygon p) {767fillPolygon(p.xpoints, p.ypoints, p.npoints);768}769770/**771* Draws the text given by the specified string, using this772* graphics context's current font and color. The baseline of the773* leftmost character is at position (<i>x</i>, <i>y</i>) in this774* graphics context's coordinate system.775* @param str the string to be drawn.776* @param x the <i>x</i> coordinate.777* @param y the <i>y</i> coordinate.778* @throws NullPointerException if <code>str</code> is <code>null</code>.779* @see java.awt.Graphics#drawBytes780* @see java.awt.Graphics#drawChars781*/782public abstract void drawString(String str, int x, int y);783784/**785* Renders the text of the specified iterator applying its attributes786* in accordance with the specification of the787* {@link java.awt.font.TextAttribute TextAttribute} class.788* <p>789* The baseline of the leftmost character is at position790* (<i>x</i>, <i>y</i>) in this graphics context's coordinate system.791* @param iterator the iterator whose text is to be drawn792* @param x the <i>x</i> coordinate.793* @param y the <i>y</i> coordinate.794* @throws NullPointerException if <code>iterator</code> is795* <code>null</code>.796* @see java.awt.Graphics#drawBytes797* @see java.awt.Graphics#drawChars798*/799public abstract void drawString(AttributedCharacterIterator iterator,800int x, int y);801802/**803* Draws the text given by the specified character array, using this804* graphics context's current font and color. The baseline of the805* first character is at position (<i>x</i>, <i>y</i>) in this806* graphics context's coordinate system.807* @param data the array of characters to be drawn808* @param offset the start offset in the data809* @param length the number of characters to be drawn810* @param x the <i>x</i> coordinate of the baseline of the text811* @param y the <i>y</i> coordinate of the baseline of the text812* @throws NullPointerException if <code>data</code> is <code>null</code>.813* @throws IndexOutOfBoundsException if <code>offset</code> or814* <code>length</code>is less than zero, or815* <code>offset+length</code> is greater than the length of the816* <code>data</code> array.817* @see java.awt.Graphics#drawBytes818* @see java.awt.Graphics#drawString819*/820public void drawChars(char data[], int offset, int length, int x, int y) {821drawString(new String(data, offset, length), x, y);822}823824/**825* Draws the text given by the specified byte array, using this826* graphics context's current font and color. The baseline of the827* first character is at position (<i>x</i>, <i>y</i>) in this828* graphics context's coordinate system.829* <p>830* Use of this method is not recommended as each byte is interpreted831* as a Unicode code point in the range 0 to 255, and so can only be832* used to draw Latin characters in that range.833* @param data the data to be drawn834* @param offset the start offset in the data835* @param length the number of bytes that are drawn836* @param x the <i>x</i> coordinate of the baseline of the text837* @param y the <i>y</i> coordinate of the baseline of the text838* @throws NullPointerException if <code>data</code> is <code>null</code>.839* @throws IndexOutOfBoundsException if <code>offset</code> or840* <code>length</code>is less than zero, or <code>offset+length</code>841* is greater than the length of the <code>data</code> array.842* @see java.awt.Graphics#drawChars843* @see java.awt.Graphics#drawString844*/845public void drawBytes(byte data[], int offset, int length, int x, int y) {846drawString(new String(data, 0, offset, length), x, y);847}848849/**850* Draws as much of the specified image as is currently available.851* The image is drawn with its top-left corner at852* (<i>x</i>, <i>y</i>) in this graphics context's coordinate853* space. Transparent pixels in the image do not affect whatever854* pixels are already there.855* <p>856* This method returns immediately in all cases, even if the857* complete image has not yet been loaded, and it has not been dithered858* and converted for the current output device.859* <p>860* If the image has completely loaded and its pixels are861* no longer being changed, then862* <code>drawImage</code> returns <code>true</code>.863* Otherwise, <code>drawImage</code> returns <code>false</code>864* and as more of865* the image becomes available866* or it is time to draw another frame of animation,867* the process that loads the image notifies868* the specified image observer.869* @param img the specified image to be drawn. This method does870* nothing if <code>img</code> is null.871* @param x the <i>x</i> coordinate.872* @param y the <i>y</i> coordinate.873* @param observer object to be notified as more of874* the image is converted.875* @return <code>false</code> if the image pixels are still changing;876* <code>true</code> otherwise.877* @see java.awt.Image878* @see java.awt.image.ImageObserver879* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)880*/881public abstract boolean drawImage(Image img, int x, int y,882ImageObserver observer);883884/**885* Draws as much of the specified image as has already been scaled886* to fit inside the specified rectangle.887* <p>888* The image is drawn inside the specified rectangle of this889* graphics context's coordinate space, and is scaled if890* necessary. Transparent pixels do not affect whatever pixels891* are already there.892* <p>893* This method returns immediately in all cases, even if the894* entire image has not yet been scaled, dithered, and converted895* for the current output device.896* If the current output representation is not yet complete, then897* <code>drawImage</code> returns <code>false</code>. As more of898* the image becomes available, the process that loads the image notifies899* the image observer by calling its <code>imageUpdate</code> method.900* <p>901* A scaled version of an image will not necessarily be902* available immediately just because an unscaled version of the903* image has been constructed for this output device. Each size of904* the image may be cached separately and generated from the original905* data in a separate image production sequence.906* @param img the specified image to be drawn. This method does907* nothing if <code>img</code> is null.908* @param x the <i>x</i> coordinate.909* @param y the <i>y</i> coordinate.910* @param width the width of the rectangle.911* @param height the height of the rectangle.912* @param observer object to be notified as more of913* the image is converted.914* @return <code>false</code> if the image pixels are still changing;915* <code>true</code> otherwise.916* @see java.awt.Image917* @see java.awt.image.ImageObserver918* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)919*/920public abstract boolean drawImage(Image img, int x, int y,921int width, int height,922ImageObserver observer);923924/**925* Draws as much of the specified image as is currently available.926* The image is drawn with its top-left corner at927* (<i>x</i>, <i>y</i>) in this graphics context's coordinate928* space. Transparent pixels are drawn in the specified929* background color.930* <p>931* This operation is equivalent to filling a rectangle of the932* width and height of the specified image with the given color and then933* drawing the image on top of it, but possibly more efficient.934* <p>935* This method returns immediately in all cases, even if the936* complete image has not yet been loaded, and it has not been dithered937* and converted for the current output device.938* <p>939* If the image has completely loaded and its pixels are940* no longer being changed, then941* <code>drawImage</code> returns <code>true</code>.942* Otherwise, <code>drawImage</code> returns <code>false</code>943* and as more of944* the image becomes available945* or it is time to draw another frame of animation,946* the process that loads the image notifies947* the specified image observer.948* @param img the specified image to be drawn. This method does949* nothing if <code>img</code> is null.950* @param x the <i>x</i> coordinate.951* @param y the <i>y</i> coordinate.952* @param bgcolor the background color to paint under the953* non-opaque portions of the image.954* @param observer object to be notified as more of955* the image is converted.956* @return <code>false</code> if the image pixels are still changing;957* <code>true</code> otherwise.958* @see java.awt.Image959* @see java.awt.image.ImageObserver960* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)961*/962public abstract boolean drawImage(Image img, int x, int y,963Color bgcolor,964ImageObserver observer);965966/**967* Draws as much of the specified image as has already been scaled968* to fit inside the specified rectangle.969* <p>970* The image is drawn inside the specified rectangle of this971* graphics context's coordinate space, and is scaled if972* necessary. Transparent pixels are drawn in the specified973* background color.974* This operation is equivalent to filling a rectangle of the975* width and height of the specified image with the given color and then976* drawing the image on top of it, but possibly more efficient.977* <p>978* This method returns immediately in all cases, even if the979* entire image has not yet been scaled, dithered, and converted980* for the current output device.981* If the current output representation is not yet complete then982* <code>drawImage</code> returns <code>false</code>. As more of983* the image becomes available, the process that loads the image notifies984* the specified image observer.985* <p>986* A scaled version of an image will not necessarily be987* available immediately just because an unscaled version of the988* image has been constructed for this output device. Each size of989* the image may be cached separately and generated from the original990* data in a separate image production sequence.991* @param img the specified image to be drawn. This method does992* nothing if <code>img</code> is null.993* @param x the <i>x</i> coordinate.994* @param y the <i>y</i> coordinate.995* @param width the width of the rectangle.996* @param height the height of the rectangle.997* @param bgcolor the background color to paint under the998* non-opaque portions of the image.999* @param observer object to be notified as more of1000* the image is converted.1001* @return <code>false</code> if the image pixels are still changing;1002* <code>true</code> otherwise.1003* @see java.awt.Image1004* @see java.awt.image.ImageObserver1005* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1006*/1007public abstract boolean drawImage(Image img, int x, int y,1008int width, int height,1009Color bgcolor,1010ImageObserver observer);10111012/**1013* Draws as much of the specified area of the specified image as is1014* currently available, scaling it on the fly to fit inside the1015* specified area of the destination drawable surface. Transparent pixels1016* do not affect whatever pixels are already there.1017* <p>1018* This method returns immediately in all cases, even if the1019* image area to be drawn has not yet been scaled, dithered, and converted1020* for the current output device.1021* If the current output representation is not yet complete then1022* <code>drawImage</code> returns <code>false</code>. As more of1023* the image becomes available, the process that loads the image notifies1024* the specified image observer.1025* <p>1026* This method always uses the unscaled version of the image1027* to render the scaled rectangle and performs the required1028* scaling on the fly. It does not use a cached, scaled version1029* of the image for this operation. Scaling of the image from source1030* to destination is performed such that the first coordinate1031* of the source rectangle is mapped to the first coordinate of1032* the destination rectangle, and the second source coordinate is1033* mapped to the second destination coordinate. The subimage is1034* scaled and flipped as needed to preserve those mappings.1035* @param img the specified image to be drawn. This method does1036* nothing if <code>img</code> is null.1037* @param dx1 the <i>x</i> coordinate of the first corner of the1038* destination rectangle.1039* @param dy1 the <i>y</i> coordinate of the first corner of the1040* destination rectangle.1041* @param dx2 the <i>x</i> coordinate of the second corner of the1042* destination rectangle.1043* @param dy2 the <i>y</i> coordinate of the second corner of the1044* destination rectangle.1045* @param sx1 the <i>x</i> coordinate of the first corner of the1046* source rectangle.1047* @param sy1 the <i>y</i> coordinate of the first corner of the1048* source rectangle.1049* @param sx2 the <i>x</i> coordinate of the second corner of the1050* source rectangle.1051* @param sy2 the <i>y</i> coordinate of the second corner of the1052* source rectangle.1053* @param observer object to be notified as more of the image is1054* scaled and converted.1055* @return <code>false</code> if the image pixels are still changing;1056* <code>true</code> otherwise.1057* @see java.awt.Image1058* @see java.awt.image.ImageObserver1059* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1060* @since JDK1.11061*/1062public abstract boolean drawImage(Image img,1063int dx1, int dy1, int dx2, int dy2,1064int sx1, int sy1, int sx2, int sy2,1065ImageObserver observer);10661067/**1068* Draws as much of the specified area of the specified image as is1069* currently available, scaling it on the fly to fit inside the1070* specified area of the destination drawable surface.1071* <p>1072* Transparent pixels are drawn in the specified background color.1073* This operation is equivalent to filling a rectangle of the1074* width and height of the specified image with the given color and then1075* drawing the image on top of it, but possibly more efficient.1076* <p>1077* This method returns immediately in all cases, even if the1078* image area to be drawn has not yet been scaled, dithered, and converted1079* for the current output device.1080* If the current output representation is not yet complete then1081* <code>drawImage</code> returns <code>false</code>. As more of1082* the image becomes available, the process that loads the image notifies1083* the specified image observer.1084* <p>1085* This method always uses the unscaled version of the image1086* to render the scaled rectangle and performs the required1087* scaling on the fly. It does not use a cached, scaled version1088* of the image for this operation. Scaling of the image from source1089* to destination is performed such that the first coordinate1090* of the source rectangle is mapped to the first coordinate of1091* the destination rectangle, and the second source coordinate is1092* mapped to the second destination coordinate. The subimage is1093* scaled and flipped as needed to preserve those mappings.1094* @param img the specified image to be drawn. This method does1095* nothing if <code>img</code> is null.1096* @param dx1 the <i>x</i> coordinate of the first corner of the1097* destination rectangle.1098* @param dy1 the <i>y</i> coordinate of the first corner of the1099* destination rectangle.1100* @param dx2 the <i>x</i> coordinate of the second corner of the1101* destination rectangle.1102* @param dy2 the <i>y</i> coordinate of the second corner of the1103* destination rectangle.1104* @param sx1 the <i>x</i> coordinate of the first corner of the1105* source rectangle.1106* @param sy1 the <i>y</i> coordinate of the first corner of the1107* source rectangle.1108* @param sx2 the <i>x</i> coordinate of the second corner of the1109* source rectangle.1110* @param sy2 the <i>y</i> coordinate of the second corner of the1111* source rectangle.1112* @param bgcolor the background color to paint under the1113* non-opaque portions of the image.1114* @param observer object to be notified as more of the image is1115* scaled and converted.1116* @return <code>false</code> if the image pixels are still changing;1117* <code>true</code> otherwise.1118* @see java.awt.Image1119* @see java.awt.image.ImageObserver1120* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1121* @since JDK1.11122*/1123public abstract boolean drawImage(Image img,1124int dx1, int dy1, int dx2, int dy2,1125int sx1, int sy1, int sx2, int sy2,1126Color bgcolor,1127ImageObserver observer);11281129/**1130* Disposes of this graphics context and releases1131* any system resources that it is using.1132* A <code>Graphics</code> object cannot be used after1133* <code>dispose</code>has been called.1134* <p>1135* When a Java program runs, a large number of <code>Graphics</code>1136* objects can be created within a short time frame.1137* Although the finalization process of the garbage collector1138* also disposes of the same system resources, it is preferable1139* to manually free the associated resources by calling this1140* method rather than to rely on a finalization process which1141* may not run to completion for a long period of time.1142* <p>1143* Graphics objects which are provided as arguments to the1144* <code>paint</code> and <code>update</code> methods1145* of components are automatically released by the system when1146* those methods return. For efficiency, programmers should1147* call <code>dispose</code> when finished using1148* a <code>Graphics</code> object only if it was created1149* directly from a component or another <code>Graphics</code> object.1150* @see java.awt.Graphics#finalize1151* @see java.awt.Component#paint1152* @see java.awt.Component#update1153* @see java.awt.Component#getGraphics1154* @see java.awt.Graphics#create1155*/1156public abstract void dispose();11571158/**1159* Disposes of this graphics context once it is no longer referenced.1160* @see #dispose1161*/1162public void finalize() {1163dispose();1164}11651166/**1167* Returns a <code>String</code> object representing this1168* <code>Graphics</code> object's value.1169* @return a string representation of this graphics context.1170*/1171public String toString() {1172return getClass().getName() + "[font=" + getFont() + ",color=" + getColor() + "]";1173}11741175/**1176* Returns the bounding rectangle of the current clipping area.1177* @return the bounding rectangle of the current clipping area1178* or <code>null</code> if no clip is set.1179* @deprecated As of JDK version 1.1,1180* replaced by <code>getClipBounds()</code>.1181*/1182@Deprecated1183public Rectangle getClipRect() {1184return getClipBounds();1185}11861187/**1188* Returns true if the specified rectangular area might intersect1189* the current clipping area.1190* The coordinates of the specified rectangular area are in the1191* user coordinate space and are relative to the coordinate1192* system origin of this graphics context.1193* This method may use an algorithm that calculates a result quickly1194* but which sometimes might return true even if the specified1195* rectangular area does not intersect the clipping area.1196* The specific algorithm employed may thus trade off accuracy for1197* speed, but it will never return false unless it can guarantee1198* that the specified rectangular area does not intersect the1199* current clipping area.1200* The clipping area used by this method can represent the1201* intersection of the user clip as specified through the clip1202* methods of this graphics context as well as the clipping1203* associated with the device or image bounds and window visibility.1204*1205* @param x the x coordinate of the rectangle to test against the clip1206* @param y the y coordinate of the rectangle to test against the clip1207* @param width the width of the rectangle to test against the clip1208* @param height the height of the rectangle to test against the clip1209* @return <code>true</code> if the specified rectangle intersects1210* the bounds of the current clip; <code>false</code>1211* otherwise.1212*/1213public boolean hitClip(int x, int y, int width, int height) {1214// Note, this implementation is not very efficient.1215// Subclasses should override this method and calculate1216// the results more directly.1217Rectangle clipRect = getClipBounds();1218if (clipRect == null) {1219return true;1220}1221return clipRect.intersects(x, y, width, height);1222}12231224/**1225* Returns the bounding rectangle of the current clipping area.1226* The coordinates in the rectangle are relative to the coordinate1227* system origin of this graphics context. This method differs1228* from {@link #getClipBounds() getClipBounds} in that an existing1229* rectangle is used instead of allocating a new one.1230* This method refers to the user clip, which is independent of the1231* clipping associated with device bounds and window visibility.1232* If no clip has previously been set, or if the clip has been1233* cleared using <code>setClip(null)</code>, this method returns the1234* specified <code>Rectangle</code>.1235* @param r the rectangle where the current clipping area is1236* copied to. Any current values in this rectangle are1237* overwritten.1238* @return the bounding rectangle of the current clipping area.1239*/1240public Rectangle getClipBounds(Rectangle r) {1241// Note, this implementation is not very efficient.1242// Subclasses should override this method and avoid1243// the allocation overhead of getClipBounds().1244Rectangle clipRect = getClipBounds();1245if (clipRect != null) {1246r.x = clipRect.x;1247r.y = clipRect.y;1248r.width = clipRect.width;1249r.height = clipRect.height;1250} else if (r == null) {1251throw new NullPointerException("null rectangle parameter");1252}1253return r;1254}1255}125612571258