Path: blob/master/src/java.desktop/share/classes/java/awt/Graphics.java
41152 views
/*1* Copyright (c) 1995, 2021, 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} 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} 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} 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} object are considered relative to the75* translation origin of this {@code Graphics} 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} 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} or {@code clipRect}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 1.0102*/103public abstract class Graphics {104105/**106* Constructs a new {@code Graphics} object.107* This constructor is the default constructor for a graphics108* context.109* <p>110* Since {@code Graphics} 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} 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} object that is122* a copy of this {@code Graphics} 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} object based on this130* {@code Graphics} object, but with a new translation and clip area.131* The new {@code Graphics} 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} 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 0}) 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 0}), and its size150* is specified by the {@code width} and {@code height}151* arguments.152* </ul>153*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* A null argument is silently ignored.196* @param c the new rendering color.197* @see java.awt.Color198* @see java.awt.Graphics#getColor199*/200public abstract void setColor(Color c);201202/**203* Sets the paint mode of this graphics context to overwrite the204* destination with this graphics context's current color.205* This sets the logical pixel operation function to the paint or206* overwrite mode. All subsequent rendering operations will207* overwrite the destination with the current color.208*/209public abstract void setPaintMode();210211/**212* Sets the paint mode of this graphics context to alternate between213* this graphics context's current color and the new specified color.214* This specifies that logical pixel operations are performed in the215* XOR mode, which alternates pixels between the current color and216* a specified XOR color.217* <p>218* When drawing operations are performed, pixels which are the219* current color are changed to the specified color, and vice versa.220* <p>221* Pixels that are of colors other than those two colors are changed222* in an unpredictable but reversible manner; if the same figure is223* drawn twice, then all pixels are restored to their original values.224* @param c1 the XOR alternation color225*/226public abstract void setXORMode(Color c1);227228/**229* Gets the current font.230* @return this graphics context's current font.231* @see java.awt.Font232* @see java.awt.Graphics#setFont(Font)233*/234public abstract Font getFont();235236/**237* Sets this graphics context's font to the specified font.238* All subsequent text operations using this graphics context239* use this font. A null argument is silently ignored.240* @param font the font.241* @see java.awt.Graphics#getFont242* @see java.awt.Graphics#drawString(java.lang.String, int, int)243* @see java.awt.Graphics#drawBytes(byte[], int, int, int, int)244* @see java.awt.Graphics#drawChars(char[], int, int, int, int)245*/246public abstract void setFont(Font font);247248/**249* Gets the font metrics of the current font.250* @return the font metrics of this graphics251* context's current font.252* @see java.awt.Graphics#getFont253* @see java.awt.FontMetrics254* @see java.awt.Graphics#getFontMetrics(Font)255*/256public FontMetrics getFontMetrics() {257return getFontMetrics(getFont());258}259260/**261* Gets the font metrics for the specified font.262* @return the font metrics for the specified font.263* @param f the specified font264* @see java.awt.Graphics#getFont265* @see java.awt.FontMetrics266* @see java.awt.Graphics#getFontMetrics()267*/268public abstract FontMetrics getFontMetrics(Font f);269270271/**272* Returns the bounding rectangle of the current clipping area.273* This method refers to the user clip, which is independent of the274* clipping associated with device bounds and window visibility.275* If no clip has previously been set, or if the clip has been276* cleared using {@code setClip(null)}, this method returns277* {@code null}.278* The coordinates in the rectangle are relative to the coordinate279* system origin of this graphics context.280* @return the bounding rectangle of the current clipping area,281* or {@code null} if no clip is set.282* @see java.awt.Graphics#getClip283* @see java.awt.Graphics#clipRect284* @see java.awt.Graphics#setClip(int, int, int, int)285* @see java.awt.Graphics#setClip(Shape)286* @since 1.1287*/288public abstract Rectangle getClipBounds();289290/**291* Intersects the current clip with the specified rectangle.292* The resulting clipping area is the intersection of the current293* clipping area and the specified rectangle. If there is no294* current clipping area, either because the clip has never been295* set, or the clip has been cleared using {@code setClip(null)},296* the specified rectangle becomes the new clip.297* This method sets the user clip, which is independent of the298* clipping associated with device bounds and window visibility.299* This method can only be used to make the current clip smaller.300* To set the current clip larger, use any of the setClip methods.301* Rendering operations have no effect outside of the clipping area.302* @param x the x coordinate of the rectangle to intersect the clip with303* @param y the y coordinate of the rectangle to intersect the clip with304* @param width the width of the rectangle to intersect the clip with305* @param height the height of the rectangle to intersect the clip with306* @see #setClip(int, int, int, int)307* @see #setClip(Shape)308*/309public abstract void clipRect(int x, int y, int width, int height);310311/**312* Sets the current clip to the rectangle specified by the given313* coordinates. This method sets the user clip, which is314* independent of the clipping associated with device bounds315* and window visibility.316* Rendering operations have no effect outside of the clipping area.317* @param x the <i>x</i> coordinate of the new clip rectangle.318* @param y the <i>y</i> coordinate of the new clip rectangle.319* @param width the width of the new clip rectangle.320* @param height the height of the new clip rectangle.321* @see java.awt.Graphics#clipRect322* @see java.awt.Graphics#setClip(Shape)323* @see java.awt.Graphics#getClip324* @since 1.1325*/326public abstract void setClip(int x, int y, int width, int height);327328/**329* Gets the current clipping area.330* This method returns the user clip, which is independent of the331* clipping associated with device bounds and window visibility.332* If no clip has previously been set, or if the clip has been333* cleared using {@code setClip(null)}, this method returns334* {@code null}.335* @return a {@code Shape} object representing the336* current clipping area, or {@code null} if337* no clip is set.338* @see java.awt.Graphics#getClipBounds339* @see java.awt.Graphics#clipRect340* @see java.awt.Graphics#setClip(int, int, int, int)341* @see java.awt.Graphics#setClip(Shape)342* @since 1.1343*/344public abstract Shape getClip();345346/**347* Sets the current clipping area to an arbitrary clip shape.348* Not all objects that implement the {@code Shape}349* interface can be used to set the clip. The only350* {@code Shape} objects that are guaranteed to be351* supported are {@code Shape} objects that are352* obtained via the {@code getClip} method and via353* {@code Rectangle} objects. This method sets the354* user clip, which is independent of the clipping associated355* with device bounds and window visibility.356* @param clip the {@code Shape} to use to set the clip.357* Passing {@code null} clears the current {@code clip}.358* @see java.awt.Graphics#getClip()359* @see java.awt.Graphics#clipRect360* @see java.awt.Graphics#setClip(int, int, int, int)361* @since 1.1362*/363public abstract void setClip(Shape clip);364365/**366* Copies an area of the component by a distance specified by367* {@code dx} and {@code dy}. From the point specified368* by {@code x} and {@code y}, this method369* copies downwards and to the right. To copy an area of the370* component to the left or upwards, specify a negative value for371* {@code dx} or {@code dy}.372* If a portion of the source rectangle lies outside the bounds373* of the component, or is obscured by another window or component,374* {@code copyArea} will be unable to copy the associated375* pixels. The area that is omitted can be refreshed by calling376* the component's {@code paint} method.377* @param x the <i>x</i> coordinate of the source rectangle.378* @param y the <i>y</i> coordinate of the source rectangle.379* @param width the width of the source rectangle.380* @param height the height of the source rectangle.381* @param dx the horizontal distance to copy the pixels.382* @param dy the vertical distance to copy the pixels.383*/384public abstract void copyArea(int x, int y, int width, int height,385int dx, int dy);386387/**388* Draws a line, using the current color, between the points389* <code>(x1, y1)</code> and <code>(x2, y2)</code>390* in this graphics context's coordinate system.391* @param x1 the first point's <i>x</i> coordinate.392* @param y1 the first point's <i>y</i> coordinate.393* @param x2 the second point's <i>x</i> coordinate.394* @param y2 the second point's <i>y</i> coordinate.395*/396public abstract void drawLine(int x1, int y1, int x2, int y2);397398/**399* Fills the specified rectangle.400* The left and right edges of the rectangle are at401* {@code x} and <code>x + width - 1</code>.402* The top and bottom edges are at403* {@code y} and <code>y + height - 1</code>.404* The resulting rectangle covers an area405* {@code width} pixels wide by406* {@code height} pixels tall.407* The rectangle is filled using the graphics context's current color.408* @param x the <i>x</i> coordinate409* of the rectangle to be filled.410* @param y the <i>y</i> coordinate411* of the rectangle to be filled.412* @param width the width of the rectangle to be filled.413* @param height the height of the rectangle to be filled.414* @see java.awt.Graphics#clearRect415* @see java.awt.Graphics#drawRect416*/417public abstract void fillRect(int x, int y, int width, int height);418419/**420* Draws the outline of the specified rectangle.421* The left and right edges of the rectangle are at422* {@code x} and <code>x + width</code>.423* The top and bottom edges are at424* {@code y} and <code>y + height</code>.425* The rectangle is drawn using the graphics context's current color.426* @param x the <i>x</i> coordinate427* of the rectangle to be drawn.428* @param y the <i>y</i> coordinate429* of the rectangle to be drawn.430* @param width the width of the rectangle to be drawn.431* @param height the height of the rectangle to be drawn.432* @see java.awt.Graphics#fillRect433* @see java.awt.Graphics#clearRect434*/435public void drawRect(int x, int y, int width, int height) {436if ((width < 0) || (height < 0)) {437return;438}439440if (height == 0 || width == 0) {441drawLine(x, y, x + width, y + height);442} else {443drawLine(x, y, x + width - 1, y);444drawLine(x + width, y, x + width, y + height - 1);445drawLine(x + width, y + height, x + 1, y + height);446drawLine(x, y + height, x, y + 1);447}448}449450/**451* Clears the specified rectangle by filling it with the background452* color of the current drawing surface. This operation does not453* use the current paint mode.454* <p>455* Beginning with Java 1.1, the background color456* of offscreen images may be system dependent. Applications should457* use {@code setColor} followed by {@code fillRect} to458* ensure that an offscreen image is cleared to a specific color.459* @param x the <i>x</i> coordinate of the rectangle to clear.460* @param y the <i>y</i> coordinate of the rectangle to clear.461* @param width the width of the rectangle to clear.462* @param height the height of the rectangle to clear.463* @see java.awt.Graphics#fillRect(int, int, int, int)464* @see java.awt.Graphics#drawRect465* @see java.awt.Graphics#setColor(java.awt.Color)466* @see java.awt.Graphics#setPaintMode467* @see java.awt.Graphics#setXORMode(java.awt.Color)468*/469public abstract void clearRect(int x, int y, int width, int height);470471/**472* Draws an outlined round-cornered rectangle using this graphics473* context's current color. The left and right edges of the rectangle474* are at {@code x} and <code>x + width</code>,475* respectively. The top and bottom edges of the rectangle are at476* {@code y} and <code>y + height</code>.477* @param x the <i>x</i> coordinate of the rectangle to be drawn.478* @param y the <i>y</i> coordinate of the rectangle to be drawn.479* @param width the width of the rectangle to be drawn.480* @param height the height of the rectangle to be drawn.481* @param arcWidth the horizontal diameter of the arc482* at the four corners.483* @param arcHeight the vertical diameter of the arc484* at the four corners.485* @see java.awt.Graphics#fillRoundRect486*/487public abstract void drawRoundRect(int x, int y, int width, int height,488int arcWidth, int arcHeight);489490/**491* Fills the specified rounded corner rectangle with the current color.492* The left and right edges of the rectangle493* are at {@code x} and <code>x + width - 1</code>,494* respectively. The top and bottom edges of the rectangle are at495* {@code y} and <code>y + height - 1</code>.496* @param x the <i>x</i> coordinate of the rectangle to be filled.497* @param y the <i>y</i> coordinate of the rectangle to be filled.498* @param width the width of the rectangle to be filled.499* @param height the height of the rectangle to be filled.500* @param arcWidth the horizontal diameter501* of the arc at the four corners.502* @param arcHeight the vertical diameter503* of the arc at the four corners.504* @see java.awt.Graphics#drawRoundRect505*/506public abstract void fillRoundRect(int x, int y, int width, int height,507int arcWidth, int arcHeight);508509/**510* Draws a 3-D highlighted outline of the specified rectangle.511* The edges of the rectangle are highlighted so that they512* appear to be beveled and lit from the upper left corner.513* <p>514* The colors used for the highlighting effect are determined515* based on the current color.516* The resulting rectangle covers an area that is517* <code>width + 1</code> pixels wide518* by <code>height + 1</code> pixels tall.519* @param x the <i>x</i> coordinate of the rectangle to be drawn.520* @param y the <i>y</i> coordinate of the rectangle to be drawn.521* @param width the width of the rectangle to be drawn.522* @param height the height of the rectangle to be drawn.523* @param raised a boolean that determines whether the rectangle524* appears to be raised above the surface525* or sunk into the surface.526* @see java.awt.Graphics#fill3DRect527*/528public void draw3DRect(int x, int y, int width, int height,529boolean raised) {530Color c = getColor();531Color brighter = c.brighter();532Color darker = c.darker();533534setColor(raised ? brighter : darker);535drawLine(x, y, x, y + height);536drawLine(x + 1, y, x + width - 1, y);537setColor(raised ? darker : brighter);538drawLine(x + 1, y + height, x + width, y + height);539drawLine(x + width, y, x + width, y + height - 1);540setColor(c);541}542543/**544* Paints a 3-D highlighted rectangle filled with the current color.545* The edges of the rectangle will be highlighted so that it appears546* as if the edges were beveled and lit from the upper left corner.547* The colors used for the highlighting effect will be determined from548* the current color.549* @param x the <i>x</i> coordinate of the rectangle to be filled.550* @param y the <i>y</i> coordinate of the rectangle to be filled.551* @param width the width of the rectangle to be filled.552* @param height the height of the rectangle to be filled.553* @param raised a boolean value that determines whether the554* rectangle appears to be raised above the surface555* or etched into the surface.556* @see java.awt.Graphics#draw3DRect557*/558public void fill3DRect(int x, int y, int width, int height,559boolean raised) {560Color c = getColor();561Color brighter = c.brighter();562Color darker = c.darker();563564if (!raised) {565setColor(darker);566}567fillRect(x+1, y+1, width-2, height-2);568setColor(raised ? brighter : darker);569drawLine(x, y, x, y + height - 1);570drawLine(x + 1, y, x + width - 2, y);571setColor(raised ? darker : brighter);572drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);573drawLine(x + width - 1, y, x + width - 1, y + height - 2);574setColor(c);575}576577/**578* Draws the outline of an oval.579* The result is a circle or ellipse that fits within the580* rectangle specified by the {@code x}, {@code y},581* {@code width}, and {@code height} arguments.582* <p>583* The oval covers an area that is584* <code>width + 1</code> pixels wide585* and <code>height + 1</code> pixels tall.586* @param x the <i>x</i> coordinate of the upper left587* corner of the oval to be drawn.588* @param y the <i>y</i> coordinate of the upper left589* corner of the oval to be drawn.590* @param width the width of the oval to be drawn.591* @param height the height of the oval to be drawn.592* @see java.awt.Graphics#fillOval593*/594public abstract void drawOval(int x, int y, int width, int height);595596/**597* Fills an oval bounded by the specified rectangle with the598* current color.599* @param x the <i>x</i> coordinate of the upper left corner600* of the oval to be filled.601* @param y the <i>y</i> coordinate of the upper left corner602* of the oval to be filled.603* @param width the width of the oval to be filled.604* @param height the height of the oval to be filled.605* @see java.awt.Graphics#drawOval606*/607public abstract void fillOval(int x, int y, int width, int height);608609/**610* Draws the outline of a circular or elliptical arc611* covering the specified rectangle.612* <p>613* The resulting arc begins at {@code startAngle} and extends614* for {@code arcAngle} degrees, using the current color.615* Angles are interpreted such that 0 degrees616* is at the 3 o'clock position.617* A positive value indicates a counter-clockwise rotation618* while a negative value indicates a clockwise rotation.619* <p>620* The center of the arc is the center of the rectangle whose origin621* is (<i>x</i>, <i>y</i>) and whose size is specified by the622* {@code width} and {@code height} arguments.623* <p>624* The resulting arc covers an area625* <code>width + 1</code> pixels wide626* by <code>height + 1</code> pixels tall.627* <p>628* The angles are specified relative to the non-square extents of629* the bounding rectangle such that 45 degrees always falls on the630* line from the center of the ellipse to the upper right corner of631* the bounding rectangle. As a result, if the bounding rectangle is632* noticeably longer in one axis than the other, the angles to the633* start and end of the arc segment will be skewed farther along the634* longer axis of the bounds.635* @param x the <i>x</i> coordinate of the636* upper-left corner of the arc to be drawn.637* @param y the <i>y</i> coordinate of the638* upper-left corner of the arc to be drawn.639* @param width the width of the arc to be drawn.640* @param height the height of the arc to be drawn.641* @param startAngle the beginning angle.642* @param arcAngle the angular extent of the arc,643* relative to the start angle.644* @see java.awt.Graphics#fillArc645*/646public abstract void drawArc(int x, int y, int width, int height,647int startAngle, int arcAngle);648649/**650* Fills a circular or elliptical arc covering the specified rectangle.651* <p>652* The resulting arc begins at {@code startAngle} and extends653* for {@code arcAngle} degrees.654* Angles are interpreted such that 0 degrees655* is at the 3 o'clock position.656* A positive value indicates a counter-clockwise rotation657* while a negative value indicates a clockwise rotation.658* <p>659* The center of the arc is the center of the rectangle whose origin660* is (<i>x</i>, <i>y</i>) and whose size is specified by the661* {@code width} and {@code height} arguments.662* <p>663* The resulting arc covers an area664* <code>width + 1</code> pixels wide665* by <code>height + 1</code> pixels tall.666* <p>667* The angles are specified relative to the non-square extents of668* the bounding rectangle such that 45 degrees always falls on the669* line from the center of the ellipse to the upper right corner of670* the bounding rectangle. As a result, if the bounding rectangle is671* noticeably longer in one axis than the other, the angles to the672* start and end of the arc segment will be skewed farther along the673* longer axis of the bounds.674* @param x the <i>x</i> coordinate of the675* upper-left corner of the arc to be filled.676* @param y the <i>y</i> coordinate of the677* upper-left corner of the arc to be filled.678* @param width the width of the arc to be filled.679* @param height the height of the arc to be filled.680* @param startAngle the beginning angle.681* @param arcAngle the angular extent of the arc,682* relative to the start angle.683* @see java.awt.Graphics#drawArc684*/685public abstract void fillArc(int x, int y, int width, int height,686int startAngle, int arcAngle);687688/**689* Draws a sequence of connected lines defined by690* arrays of <i>x</i> and <i>y</i> coordinates.691* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.692* The figure is not closed if the first point693* differs from the last point.694* @param xPoints an array of <i>x</i> points695* @param yPoints an array of <i>y</i> points696* @param nPoints the total number of points697* @see java.awt.Graphics#drawPolygon(int[], int[], int)698* @since 1.1699*/700public abstract void drawPolyline(int[] xPoints, int[] yPoints,701int nPoints);702703/**704* Draws a closed polygon defined by705* arrays of <i>x</i> and <i>y</i> coordinates.706* Each pair of (<i>x</i>, <i>y</i>) coordinates defines a point.707* <p>708* This method draws the polygon defined by {@code nPoint} line709* segments, where the first <code>nPoint - 1</code>710* line segments are line segments from711* <code>(xPoints[i - 1], yPoints[i - 1])</code>712* to <code>(xPoints[i], yPoints[i])</code>, for713* 1 ≤ <i>i</i> ≤ {@code nPoints}.714* The figure is automatically closed by drawing a line connecting715* the final point to the first point, if those points are different.716* @param xPoints a an array of {@code x} coordinates.717* @param yPoints a an array of {@code y} coordinates.718* @param nPoints a the total number of points.719* @see java.awt.Graphics#fillPolygon720* @see java.awt.Graphics#drawPolyline721*/722public abstract void drawPolygon(int[] xPoints, int[] yPoints,723int nPoints);724725/**726* Draws the outline of a polygon defined by the specified727* {@code Polygon} object.728* @param p the polygon to draw.729* @see java.awt.Graphics#fillPolygon730* @see java.awt.Graphics#drawPolyline731*/732public void drawPolygon(Polygon p) {733drawPolygon(p.xpoints, p.ypoints, p.npoints);734}735736/**737* Fills a closed polygon defined by738* arrays of <i>x</i> and <i>y</i> coordinates.739* <p>740* This method draws the polygon defined by {@code nPoint} line741* segments, where the first <code>nPoint - 1</code>742* line segments are line segments from743* <code>(xPoints[i - 1], yPoints[i - 1])</code>744* to <code>(xPoints[i], yPoints[i])</code>, for745* 1 ≤ <i>i</i> ≤ {@code nPoints}.746* The figure is automatically closed by drawing a line connecting747* the final point to the first point, if those points are different.748* <p>749* The area inside the polygon is defined using an750* even-odd fill rule, also known as the alternating rule.751* @param xPoints a an array of {@code x} coordinates.752* @param yPoints a an array of {@code y} coordinates.753* @param nPoints a the total number of points.754* @see java.awt.Graphics#drawPolygon(int[], int[], int)755*/756public abstract void fillPolygon(int[] xPoints, int[] yPoints,757int nPoints);758759/**760* Fills the polygon defined by the specified Polygon object with761* the graphics context's current color.762* <p>763* The area inside the polygon is defined using an764* even-odd fill rule, also known as the alternating rule.765* @param p the polygon to fill.766* @see java.awt.Graphics#drawPolygon(int[], int[], int)767*/768public void fillPolygon(Polygon p) {769fillPolygon(p.xpoints, p.ypoints, p.npoints);770}771772/**773* Draws the text given by the specified string, using this774* graphics context's current font and color. The baseline of the775* leftmost character is at position (<i>x</i>, <i>y</i>) in this776* graphics context's coordinate system.777* @param str the string to be drawn.778* @param x the <i>x</i> coordinate.779* @param y the <i>y</i> coordinate.780* @throws NullPointerException if {@code str} is {@code null}.781* @see java.awt.Graphics#drawBytes782* @see java.awt.Graphics#drawChars783*/784public abstract void drawString(String str, int x, int y);785786/**787* Renders the text of the specified iterator applying its attributes788* in accordance with the specification of the789* {@link java.awt.font.TextAttribute TextAttribute} class.790* <p>791* The baseline of the leftmost character is at position792* (<i>x</i>, <i>y</i>) in this graphics context's coordinate system.793* @param iterator the iterator whose text is to be drawn794* @param x the <i>x</i> coordinate.795* @param y the <i>y</i> coordinate.796* @throws NullPointerException if {@code iterator} is797* {@code null}.798* @see java.awt.Graphics#drawBytes799* @see java.awt.Graphics#drawChars800*/801public abstract void drawString(AttributedCharacterIterator iterator,802int x, int y);803804/**805* Draws the text given by the specified character array, using this806* graphics context's current font and color. The baseline of the807* first character is at position (<i>x</i>, <i>y</i>) in this808* graphics context's coordinate system.809* @param data the array of characters to be drawn810* @param offset the start offset in the data811* @param length the number of characters to be drawn812* @param x the <i>x</i> coordinate of the baseline of the text813* @param y the <i>y</i> coordinate of the baseline of the text814* @throws NullPointerException if {@code data} is {@code null}.815* @throws IndexOutOfBoundsException if {@code offset} or816* {@code length} is less than zero, or817* {@code offset+length} is greater than the length of the818* {@code data} array.819* @see java.awt.Graphics#drawBytes820* @see java.awt.Graphics#drawString821*/822public void drawChars(char[] data, int offset, int length, int x, int y) {823drawString(new String(data, offset, length), x, y);824}825826/**827* Draws the text given by the specified byte array, using this828* graphics context's current font and color. The baseline of the829* first character is at position (<i>x</i>, <i>y</i>) in this830* graphics context's coordinate system.831* <p>832* Use of this method is not recommended as each byte is interpreted833* as a Unicode code point in the range 0 to 255, and so can only be834* used to draw Latin characters in that range.835* @param data the data to be drawn836* @param offset the start offset in the data837* @param length the number of bytes that are drawn838* @param x the <i>x</i> coordinate of the baseline of the text839* @param y the <i>y</i> coordinate of the baseline of the text840* @throws NullPointerException if {@code data} is {@code null}.841* @throws IndexOutOfBoundsException if {@code offset} or842* {@code length} is less than zero, or {@code offset+length}843* is greater than the length of the {@code data} array.844* @see java.awt.Graphics#drawChars845* @see java.awt.Graphics#drawString846*/847@SuppressWarnings("deprecation")848public void drawBytes(byte[] data, int offset, int length, int x, int y) {849drawString(new String(data, 0, offset, length), x, y);850}851852/**853* Draws as much of the specified image as is currently available.854* The image is drawn with its top-left corner at855* (<i>x</i>, <i>y</i>) in this graphics context's coordinate856* space. Transparent pixels in the image do not affect whatever857* pixels are already there.858* <p>859* This method returns immediately in all cases, even if the860* complete image has not yet been loaded, and it has not been dithered861* and converted for the current output device.862* <p>863* If the image has completely loaded and its pixels are864* no longer being changed, then865* {@code drawImage} returns {@code true}.866* Otherwise, {@code drawImage} returns {@code false}867* and as more of868* the image becomes available869* or it is time to draw another frame of animation,870* the process that loads the image notifies871* the specified image observer.872* @param img the specified image to be drawn. This method does873* nothing if {@code img} is null.874* @param x the <i>x</i> coordinate.875* @param y the <i>y</i> coordinate.876* @param observer object to be notified as more of877* the image is converted.878* @return {@code false} if the image pixels are still changing;879* {@code true} otherwise.880* @see java.awt.Image881* @see java.awt.image.ImageObserver882* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)883*/884public abstract boolean drawImage(Image img, int x, int y,885ImageObserver observer);886887/**888* Draws as much of the specified image as has already been scaled889* to fit inside the specified rectangle.890* <p>891* The image is drawn inside the specified rectangle of this892* graphics context's coordinate space, and is scaled if893* necessary. Transparent pixels do not affect whatever pixels894* are already there.895* <p>896* This method returns immediately in all cases, even if the897* entire image has not yet been scaled, dithered, and converted898* for the current output device.899* If the current output representation is not yet complete, then900* {@code drawImage} returns {@code false}. As more of901* the image becomes available, the process that loads the image notifies902* the image observer by calling its {@code imageUpdate} method.903* <p>904* A scaled version of an image will not necessarily be905* available immediately just because an unscaled version of the906* image has been constructed for this output device. Each size of907* the image may be cached separately and generated from the original908* data in a separate image production sequence.909* @param img the specified image to be drawn. This method does910* nothing if {@code img} is null.911* @param x the <i>x</i> coordinate.912* @param y the <i>y</i> coordinate.913* @param width the width of the rectangle.914* @param height the height of the rectangle.915* @param observer object to be notified as more of916* the image is converted.917* @return {@code false} if the image pixels are still changing;918* {@code true} otherwise.919* @see java.awt.Image920* @see java.awt.image.ImageObserver921* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)922*/923public abstract boolean drawImage(Image img, int x, int y,924int width, int height,925ImageObserver observer);926927/**928* Draws as much of the specified image as is currently available.929* The image is drawn with its top-left corner at930* (<i>x</i>, <i>y</i>) in this graphics context's coordinate931* space. Transparent pixels are drawn in the specified932* background color.933* <p>934* This operation is equivalent to filling a rectangle of the935* width and height of the specified image with the given color and then936* drawing the image on top of it, but possibly more efficient.937* <p>938* This method returns immediately in all cases, even if the939* complete image has not yet been loaded, and it has not been dithered940* and converted for the current output device.941* <p>942* If the image has completely loaded and its pixels are943* no longer being changed, then944* {@code drawImage} returns {@code true}.945* Otherwise, {@code drawImage} returns {@code false}946* and as more of947* the image becomes available948* or it is time to draw another frame of animation,949* the process that loads the image notifies950* the specified image observer.951* @param img the specified image to be drawn. This method does952* nothing if {@code img} is null.953* @param x the <i>x</i> coordinate.954* @param y the <i>y</i> coordinate.955* @param bgcolor the background color to paint under the956* non-opaque portions of the image.957* @param observer object to be notified as more of958* the image is converted.959* @return {@code false} if the image pixels are still changing;960* {@code true} otherwise.961* @see java.awt.Image962* @see java.awt.image.ImageObserver963* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)964*/965public abstract boolean drawImage(Image img, int x, int y,966Color bgcolor,967ImageObserver observer);968969/**970* Draws as much of the specified image as has already been scaled971* to fit inside the specified rectangle.972* <p>973* The image is drawn inside the specified rectangle of this974* graphics context's coordinate space, and is scaled if975* necessary. Transparent pixels are drawn in the specified976* background color.977* This operation is equivalent to filling a rectangle of the978* width and height of the specified image with the given color and then979* drawing the image on top of it, but possibly more efficient.980* <p>981* This method returns immediately in all cases, even if the982* entire image has not yet been scaled, dithered, and converted983* for the current output device.984* If the current output representation is not yet complete then985* {@code drawImage} returns {@code false}. As more of986* the image becomes available, the process that loads the image notifies987* the specified image observer.988* <p>989* A scaled version of an image will not necessarily be990* available immediately just because an unscaled version of the991* image has been constructed for this output device. Each size of992* the image may be cached separately and generated from the original993* data in a separate image production sequence.994* @param img the specified image to be drawn. This method does995* nothing if {@code img} is null.996* @param x the <i>x</i> coordinate.997* @param y the <i>y</i> coordinate.998* @param width the width of the rectangle.999* @param height the height of the rectangle.1000* @param bgcolor the background color to paint under the1001* non-opaque portions of the image.1002* @param observer object to be notified as more of1003* the image is converted.1004* @return {@code false} if the image pixels are still changing;1005* {@code true} otherwise.1006* @see java.awt.Image1007* @see java.awt.image.ImageObserver1008* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1009*/1010public abstract boolean drawImage(Image img, int x, int y,1011int width, int height,1012Color bgcolor,1013ImageObserver observer);10141015/**1016* Draws as much of the specified area of the specified image as is1017* currently available, scaling it on the fly to fit inside the1018* specified area of the destination drawable surface. Transparent pixels1019* do not affect whatever pixels are already there.1020* <p>1021* This method returns immediately in all cases, even if the1022* image area to be drawn has not yet been scaled, dithered, and converted1023* for the current output device.1024* If the current output representation is not yet complete then1025* {@code drawImage} returns {@code false}. As more of1026* the image becomes available, the process that loads the image notifies1027* the specified image observer.1028* <p>1029* This method always uses the unscaled version of the image1030* to render the scaled rectangle and performs the required1031* scaling on the fly. It does not use a cached, scaled version1032* of the image for this operation. Scaling of the image from source1033* to destination is performed such that the first coordinate1034* of the source rectangle is mapped to the first coordinate of1035* the destination rectangle, and the second source coordinate is1036* mapped to the second destination coordinate. The subimage is1037* scaled and flipped as needed to preserve those mappings.1038* @param img the specified image to be drawn. This method does1039* nothing if {@code img} is null.1040* @param dx1 the <i>x</i> coordinate of the first corner of the1041* destination rectangle.1042* @param dy1 the <i>y</i> coordinate of the first corner of the1043* destination rectangle.1044* @param dx2 the <i>x</i> coordinate of the second corner of the1045* destination rectangle.1046* @param dy2 the <i>y</i> coordinate of the second corner of the1047* destination rectangle.1048* @param sx1 the <i>x</i> coordinate of the first corner of the1049* source rectangle.1050* @param sy1 the <i>y</i> coordinate of the first corner of the1051* source rectangle.1052* @param sx2 the <i>x</i> coordinate of the second corner of the1053* source rectangle.1054* @param sy2 the <i>y</i> coordinate of the second corner of the1055* source rectangle.1056* @param observer object to be notified as more of the image is1057* scaled and converted.1058* @return {@code false} if the image pixels are still changing;1059* {@code true} otherwise.1060* @see java.awt.Image1061* @see java.awt.image.ImageObserver1062* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1063* @since 1.11064*/1065public abstract boolean drawImage(Image img,1066int dx1, int dy1, int dx2, int dy2,1067int sx1, int sy1, int sx2, int sy2,1068ImageObserver observer);10691070/**1071* Draws as much of the specified area of the specified image as is1072* currently available, scaling it on the fly to fit inside the1073* specified area of the destination drawable surface.1074* <p>1075* Transparent pixels are drawn in the specified background color.1076* This operation is equivalent to filling a rectangle of the1077* width and height of the specified image with the given color and then1078* drawing the image on top of it, but possibly more efficient.1079* <p>1080* This method returns immediately in all cases, even if the1081* image area to be drawn has not yet been scaled, dithered, and converted1082* for the current output device.1083* If the current output representation is not yet complete then1084* {@code drawImage} returns {@code false}. As more of1085* the image becomes available, the process that loads the image notifies1086* the specified image observer.1087* <p>1088* This method always uses the unscaled version of the image1089* to render the scaled rectangle and performs the required1090* scaling on the fly. It does not use a cached, scaled version1091* of the image for this operation. Scaling of the image from source1092* to destination is performed such that the first coordinate1093* of the source rectangle is mapped to the first coordinate of1094* the destination rectangle, and the second source coordinate is1095* mapped to the second destination coordinate. The subimage is1096* scaled and flipped as needed to preserve those mappings.1097* @param img the specified image to be drawn. This method does1098* nothing if {@code img} is null.1099* @param dx1 the <i>x</i> coordinate of the first corner of the1100* destination rectangle.1101* @param dy1 the <i>y</i> coordinate of the first corner of the1102* destination rectangle.1103* @param dx2 the <i>x</i> coordinate of the second corner of the1104* destination rectangle.1105* @param dy2 the <i>y</i> coordinate of the second corner of the1106* destination rectangle.1107* @param sx1 the <i>x</i> coordinate of the first corner of the1108* source rectangle.1109* @param sy1 the <i>y</i> coordinate of the first corner of the1110* source rectangle.1111* @param sx2 the <i>x</i> coordinate of the second corner of the1112* source rectangle.1113* @param sy2 the <i>y</i> coordinate of the second corner of the1114* source rectangle.1115* @param bgcolor the background color to paint under the1116* non-opaque portions of the image.1117* @param observer object to be notified as more of the image is1118* scaled and converted.1119* @return {@code false} if the image pixels are still changing;1120* {@code true} otherwise.1121* @see java.awt.Image1122* @see java.awt.image.ImageObserver1123* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)1124* @since 1.11125*/1126public abstract boolean drawImage(Image img,1127int dx1, int dy1, int dx2, int dy2,1128int sx1, int sy1, int sx2, int sy2,1129Color bgcolor,1130ImageObserver observer);11311132/**1133* Disposes of this graphics context and releases1134* any system resources that it is using.1135* A {@code Graphics} object cannot be used after1136* {@code dispose} has been called.1137* <p>1138* When a Java program runs, a large number of {@code Graphics}1139* objects can be created within a short time frame.1140* Although the finalization process of the garbage collector1141* also disposes of the same system resources, it is preferable1142* to manually free the associated resources by calling this1143* method rather than to rely on a finalization process which1144* may not run to completion for a long period of time.1145* <p>1146* Graphics objects which are provided as arguments to the1147* {@code paint} and {@code update} methods1148* of components are automatically released by the system when1149* those methods return. For efficiency, programmers should1150* call {@code dispose} when finished using1151* a {@code Graphics} object only if it was created1152* directly from a component or another {@code Graphics} object.1153* @see java.awt.Graphics#finalize1154* @see java.awt.Component#paint1155* @see java.awt.Component#update1156* @see java.awt.Component#getGraphics1157* @see java.awt.Graphics#create1158*/1159public abstract void dispose();11601161/**1162* Disposes of this graphics context once it is no longer referenced.1163*1164* @deprecated The {@code finalize} method has been deprecated.1165* Subclasses that override {@code finalize} in order to perform cleanup1166* should be modified to use alternative cleanup mechanisms and1167* to remove the overriding {@code finalize} method.1168* When overriding the {@code finalize} method, its implementation must explicitly1169* ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.1170* See the specification for {@link Object#finalize()} for further1171* information about migration options.1172* @see #dispose1173*/1174@Deprecated(since="9")1175public void finalize() {1176dispose();1177}11781179/**1180* Returns a {@code String} object representing this1181* {@code Graphics} object's value.1182* @return a string representation of this graphics context.1183*/1184public String toString() {1185return getClass().getName() + "[font=" + getFont() + ",color=" + getColor() + "]";1186}11871188/**1189* Returns the bounding rectangle of the current clipping area.1190* @return the bounding rectangle of the current clipping area1191* or {@code null} if no clip is set.1192* @deprecated As of JDK version 1.1,1193* replaced by {@code getClipBounds()}.1194*/1195@Deprecated1196public Rectangle getClipRect() {1197return getClipBounds();1198}11991200/**1201* Returns true if the specified rectangular area might intersect1202* the current clipping area.1203* The coordinates of the specified rectangular area are in the1204* user coordinate space and are relative to the coordinate1205* system origin of this graphics context.1206* This method may use an algorithm that calculates a result quickly1207* but which sometimes might return true even if the specified1208* rectangular area does not intersect the clipping area.1209* The specific algorithm employed may thus trade off accuracy for1210* speed, but it will never return false unless it can guarantee1211* that the specified rectangular area does not intersect the1212* current clipping area.1213* The clipping area used by this method can represent the1214* intersection of the user clip as specified through the clip1215* methods of this graphics context as well as the clipping1216* associated with the device or image bounds and window visibility.1217*1218* @param x the x coordinate of the rectangle to test against the clip1219* @param y the y coordinate of the rectangle to test against the clip1220* @param width the width of the rectangle to test against the clip1221* @param height the height of the rectangle to test against the clip1222* @return {@code true} if the specified rectangle intersects1223* the bounds of the current clip; {@code false}1224* otherwise.1225*/1226public boolean hitClip(int x, int y, int width, int height) {1227// Note, this implementation is not very efficient.1228// Subclasses should override this method and calculate1229// the results more directly.1230Rectangle clipRect = getClipBounds();1231if (clipRect == null) {1232return true;1233}1234return clipRect.intersects(x, y, width, height);1235}12361237/**1238* Returns the bounding rectangle of the current clipping area.1239* The coordinates in the rectangle are relative to the coordinate1240* system origin of this graphics context. This method differs1241* from {@link #getClipBounds() getClipBounds} in that an existing1242* rectangle is used instead of allocating a new one.1243* This method refers to the user clip, which is independent of the1244* clipping associated with device bounds and window visibility.1245* If no clip has previously been set, or if the clip has been1246* cleared using {@code setClip(null)}, this method returns the1247* specified {@code Rectangle}.1248* @param r the rectangle where the current clipping area is1249* copied to. Any current values in this rectangle are1250* overwritten.1251* @return the bounding rectangle of the current clipping area.1252*/1253public Rectangle getClipBounds(Rectangle r) {1254// Note, this implementation is not very efficient.1255// Subclasses should override this method and avoid1256// the allocation overhead of getClipBounds().1257Rectangle clipRect = getClipBounds();1258if (clipRect != null) {1259r.x = clipRect.x;1260r.y = clipRect.y;1261r.width = clipRect.width;1262r.height = clipRect.height;1263} else if (r == null) {1264throw new NullPointerException("null rectangle parameter");1265}1266return r;1267}1268}126912701271