Sponsored sites

Showing posts with label JAVA AWT Tutorial. Show all posts
Showing posts with label JAVA AWT Tutorial. Show all posts

Wednesday, 17 September 2014

Java Awt Quick Guide 12

AWT Rectangle2D Class

Introduction

The Rectangle2D class states a rectangle defined by a location (x,y) and dimension (w x h).

Class declaration

Following is the declaration for java.awt.geom.Rectangle2D class:
public abstract class Rectangle2D
   extends RectangularShape

Field

Following are the fields for java.awt.geom.Arc2D class:
  • static int OUT_BOTTOM -- The bitmask that indicates that a point lies below this Rectangle2D.
  • static int OUT_LEFT -- The bitmask that indicates that a point lies to the left of this Rectangle2D.
  • static int OUT_RIGHT -- The bitmask that indicates that a point lies to the right of this Rectangle2D.
  • static int OUT_TOP -- The bitmask that indicates that a point lies above this Rectangle2D.

Class constructors

S.N.Constructor & Description
1protected Rectangle2D()
This is an abstract class that cannot be instantiated directly.

Class methods

S.N.Method & Description
1void add(double newx, double newy)
Adds a point, specified by the double precision arguments newx and newy, to this Rectangle2D.
2void add(Point2D pt)
Adds the Point2D object pt to this Rectangle2D.
3void add(Rectangle2D r)
Adds a Rectangle2D object to this Rectangle2D.
4boolean contains(double x, double y)
Tests if the specified coordinates are inside the boundary of the Shape.
5boolean contains(double x, double y, double w, double h)
Tests if the interior of the Shape entirely contains the specified rectangular area.
6abstract Rectangle2D createIntersection(Rectangle2D r)
Returns a new Rectangle2D object representing the intersection of this Rectangle2D with the specified Rectangle2D.
7abstract Rectangle2D createUnion(Rectangle2D r)
Returns a new Rectangle2D object representing the union of this Rectangle2D with the specified Rectangle2D.
8boolean equals(Object obj)
Determines whether or not the specified Object is equal to this Rectangle2D.
9Rectangle2D getBounds2D()
Returns a high precision and more accurate bounding box of the Shape than the getBounds method.
10PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of this Rectangle2D.
11PathIterator getPathIterator(AffineTransform at, double flatness)
Returns an iteration object that defines the boundary of the flattened Rectangle2D.
12int hashCode()
Returns the hashcode for this Rectangle2D.
13static void intersect(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Intersects the pair of specified source Rectangle2D objects and puts the result into the specified destination Rectangle2D object.
14boolean intersects(double x, double y, double w, double h)
Tests if the interior of the Shape intersects the interior of a specified rectangular area.
15boolean intersectsLine(double x1, double y1, double x2, double y2)
Tests if the specified line segment intersects the interior of this Rectangle2D.
16boolean intersectsLine(Line2D l)
Tests if the specified line segment intersects the interior of this Rectangle2D.
17abstract int outcode(double x, double y)
Determines where the specified coordinates lie with respect to this Rectangle2D.
18int outcode(Point2D p)
Determines where the specified Point2D lies with respect to this Rectangle2D.
19void setFrame(double x, double y, double w, double h)
Sets the location and size of the outer bounds of this Rectangle2D to the specified rectangular values.
20abstract void setRect(double x, double y, double w, double h)
Sets the location and size of this Rectangle2D to the specified double values.
21void setRect(Rectangle2D r)
Sets this Rectangle2D to be the same as the specified Rectangle2D.
22static void union(Rectangle2D src1, Rectangle2D src2, Rectangle2D dest)
Unions the pair of source Rectangle2D objects and puts the result into the specified destination Rectangle2D object.

Methods inherited

This class inherits methods from the following classes:
  • java.awt.geom.RectangularShape
  • java.lang.Object

Ellipse2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Rectangle2D shape = new Rectangle2D.Float();
      shape.setFrame(100, 150, 200,100);
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Rectangle2D.Rectangle", 100, 120);
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Rectangle2D

AWT QuadCurve2D Class

Introduction

The QuadCurve2D class states a quadratic parametric curve segment in (x,y) coordinate space.

Class declaration

Following is the declaration for java.awt.geom.QuadCurve2D class:
public abstract class QuadCurve2D
   extends Object
      implements Shape, Cloneable

Class constructors

S.N.Constructor & Description
1protected QuadCurve2D() ()
This is an abstract class that cannot be instantiated directly.

Class methods

S.N.Method & Description
1Object clone()
Creates a new object of the same class and with the same contents as this object.
2boolean contains(double x, double y)
Tests if the specified coordinates are inside the boundary of the Shape.
3boolean contains(double x, double y, double w, double h)
Tests if the interior of the Shape entirely contains the specified rectangular area.
4boolean contains(Point2D p)
Tests if a specified Point2D is inside the boundary of the Shape.
5boolean contains(Rectangle2D r)
Tests if the interior of the Shape entirely contains the specified Rectangle2D.
6Rectangle getBounds()
Returns an integer Rectangle that completely encloses the Shape.
7abstract Point2D getCtrlPt()
Returns the control point.
8abstract double getCtrlX()
Returns the X coordinate of the control point in double precision.
9abstract double getCtrlY()
Returns the Y coordinate of the control point in double precision.
10doublegetFlatness()
Returns the flatness, or maximum distance of a control point from the line connecting the end points, of this QuadCurve2D.
11static double getFlatness(double[] coords, int offset)
Returns the flatness, or maximum distance of a control point from the line connecting the end points, of the quadratic curve specified by the control points stored in the indicated array at the indicated index.
12static double getFlatness(double x1, double y1, double ctrlx, double ctrly, double x2, double y2)
Returns the flatness, or maximum distance of a control point from the line connecting the end points, of the quadratic curve specified by the indicated control points.
13double getFlatnessSq()
Returns the square of the flatness, or maximum distance of a control point from the line connecting the end points, of this QuadCurve2D.
14static double getFlatnessSq(double[] coords, int offset)
Returns the square of the flatness, or maximum distance of a control point from the line connecting the end points, of the quadratic curve specified by the control points stored in the indicated array at the indicated index.
15static double getFlatnessSq(double x1, double y1, double ctrlx, double ctrly, double x2, double y2)
Returns the square of the flatness, or maximum distance of a control point from the line connecting the end points, of the quadratic curve specified by the indicated control points.
16abstract Point2D getP1()
Returns the start point.
17abstract Point2D getP2()
Returns the end point.
18PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of the shape of this QuadCurve2D.
19PathIterator getPathIterator(AffineTransform at, double flatness)
Returns an iteration object that defines the boundary of the flattened shape of this QuadCurve2D.
20abstract double getX1()
Returns the X coordinate of the start point in double in precision.
21abstract double getX2()
Returns the X coordinate of the end point in double precision.
22abstract double getY1()
Returns the Y coordinate of the start point in double precision.
23abstract double getY2()
Returns the Y coordinate of the end point in double precision.
24boolean intersects(double x, double y, double w, double h)
Tests if the interior of the Shape intersects the interior of a specified rectangular area.
25boolean intersects(Rectangle2D r)
Tests if the interior of the Shape intersects the interior of a specified Rectangle2D.
26void setCurve(double[] coords, int offset)
Sets the location of the end points and control points of this QuadCurve2D to the double coordinates at the specified offset in the specified array.
27abstract void setCurve(double x1, double y1, double ctrlx, double ctrly, double x2, double y2)
Sets the location of the end points and control point of this curve to the specified double coordinates.
28void setCurve(Point2D[] pts, int offset)
Sets the location of the end points and control points of this QuadCurve2D to the coordinates of the Point2D objects at the specified offset in the specified array.
29void setCurve(Point2D p1, Point2D cp, Point2D p2)
Sets the location of the end points and control point of this QuadCurve2D to the specified Point2D coordinates.
30void setCurve(QuadCurve2D c)
Sets the location of the end points and control point of this QuadCurve2D to the same as those in the specified QuadCurve2D.
31static int solveQuadratic(double[] eqn)
Solves the quadratic whose coefficients are in the eqn array and places the non-complex roots back into the same array, returning the number of roots.
32static int solveQuadratic(double[] eqn, double[] res)
Solves the quadratic whose coefficients are in the eqn array and places the non-complex roots into the res array, returning the number of roots.
33static void subdivide(double[] src, int srcoff, double[] left, int leftoff, double[] right, int rightoff)
Subdivides the quadratic curve specified by the coordinates stored in the src array at indices srcoff through srcoff + 5 and stores the resulting two subdivided curves into the two result arrays at the corresponding indices.
34void subdivide(QuadCurve2D left, QuadCurve2D right)
Subdivides this QuadCurve2D and stores the resulting two subdivided curves into the left and right curve parameters.
35static void subdivide(QuadCurve2D src, QuadCurve2D left, QuadCurve2D right)
Subdivides the quadratic curve specified by the src parameter and stores the resulting two subdivided curves into the left and right curve parameters.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

QuadCurve2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      QuadCurve2D shape = new QuadCurve2D.Double();
      shape.setCurve(250D,250D,100D,100D,200D,150D);  
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("QuadCurve2D.Curve", 100, 120);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT QuadCurve2D

AWT Line2D Class

Introduction

The Line2D class states a line segment in (x,y) coordinate space.

Class declaration

Following is the declaration for java.awt.geom.Line2D class:
public abstract class Line2D
   extends Object
      implements Shape, Cloneable

Class constructors

S.N.Constructor & Description
1protected Line2D() ()
This is an abstract class that cannot be instantiated directly.

Class methods

S.N.Method & Description
1Object clone()
Creates a new object of the same class as this object.
2boolean contains(double x, double y)
Tests if a specified coordinate is inside the boundary of this Line2D.
3boolean contains(double x, double y, double w, double h)
Tests if the interior of this Line2D entirely contains the specified set of rectangular coordinates.
4boolean contains(Point2D p)
Tests if a given Point2D is inside the boundary of this Line2D.
5boolean contains(Rectangle2D r)
Tests if the interior of this Line2D entirely contains the specified Rectangle2D.
6Rectangle getBounds()
Returns an integer Rectangle that completely encloses the Shape.
7abstract Point2D getP1()
Returns the start Point2D of this Line2D.
8abstract Point2D getP2()
Returns the end Point2D of this Line2D.
9PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of this Line2D.
10PathIterator getPathIterator(AffineTransform at, double flatness)
Returns an iteration object that defines the boundary of this flattened Line2D.
11abstract double getX1()
Returns the X coordinate of the start point in double precision.
12abstract double getX2()
Returns the X coordinate of the end point in double precision.
13abstract double getY1()
Returns the Y coordinate of the start point in double precision.
14abstract double getY2()
Returns the Y coordinate of the end point in double precision.
15boolean intersects(double x, double y, double w, double h)
Tests if the interior of the Shape intersects the interior of a specified rectangular area.
16boolean intersects(Rectangle2D r)
Tests if the interior of the Shape intersects the interior of a specified Rectangle2D.
17boolean intersectsLine(double x1, double y1, double x2, double y2)
Tests if the line segment from (x1,y1) to (x2,y2) intersects this line segment.
18boolean intersectsLine(Line2D l)
Tests if the specified line segment intersects this line segment.
19static boolean linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
Tests if the line segment from (x1,y1) to (x2,y2) intersects the line segment from (x3,y3) to (x4,y4).
20double ptLineDist(double px, double py)
Returns the distance from a point to this line.
21static double ptLineDist(double x1, double y1, double x2, double y2, double px, double py)
Returns the distance from a point to a line.
22double ptLineDist(Point2D pt)
Returns the distance from a Point2D to this line.
23double ptLineDistSq(double px, double py)
Returns the square of the distance from a point to this line.
24static double ptLineDistSq(double x1, double y1, double x2, double y2, double px, double py)
Returns the square of the distance from a point to a line.
25double ptLineDistSq(Point2D pt)
Returns the square of the distance from a specified Point2D to this line.
26double ptSegDist(double px, double py)
Returns the distance from a point to this line segment.
27static double ptSegDist(double x1, double y1, double x2, double y2, double px, double py)
Returns the distance from a point to a line segment.
28double ptSegDist(Point2D pt)
Returns the distance from a Point2D to this line segment.
29double ptSegDistSq(double px, double py)
Returns the square of the distance from a point to this line segment.
30static double ptSegDistSq(double x1, double y1, double x2, double y2, double px, double py)
Returns the square of the distance from a point to a line segment.
31double ptSegDistSq(Point2D pt)
Returns the square of the distance from a Point2D to this line segment.
32int relativeCCW(double px, double py)
Returns an indicator of where the specified point (px,py) lies with respect to this line segment.
33static int relativeCCW(double x1, double y1, double x2, double y2, double px, double py)
Returns an indicator of where the specified point (px,py) lies with respect to the line segment from (x1,y1) to (x2,y2).
34int relativeCCW(Point2D p)
Returns an indicator of where the specified Point2D lies with respect to this line segment.
35abstract void setLine(double x1, double y1, double x2, double y2)
Sets the location of the end points of this Line2D to the specified double coordinates.
36void setLine(Line2D l)
Sets the location of the end points of this Line2D to the same as those end points of the specified Line2D.
37void setLine(Point2D p1, Point2D p2)
Sets the location of the end points of this Line2D to the specified Point2D coordinates.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Line2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Line2D shape = new Line2D.Double();
      shape.setLine(250D,250D,150D,150D);  
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Line2D.Line", 100, 120);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Line2D

AWT Font Class

Introduction

The Font class states fonts, which are used to render text in a visible way.

Class declaration

Following is the declaration for java.awt.Font class:
public class Font
   extends Object
      implements Serializable

Field

Following are the fields for java.awt.geom.Arc2D class:
  • static int BOLD -- The bold style constant.
  • static int CENTER_BASELINE --The baseline used in ideographic scripts like Chinese, Japanese, and Korean when laying out text.
  • static String DIALOG --A String constant for the canonical family name of the logical font "Dialog".
  • static String DIALOG_INPUT --A String constant for the canonical family name of the logical font "DialogInput".
  • static int HANGING_BASELINE -- The baseline used in Devanigiri and similar scripts when laying out text.
  • static int ITALIC -- The italicized style constant.
  • static int LAYOUT_LEFT_TO_RIGHT -- A flag to layoutGlyphVector indicating that text is left-to-right as determined by Bidi analysis.
  • static int LAYOUT_NO_LIMIT_CONTEXT -- A flag to layoutGlyphVector indicating that text in the char array after the indicated limit should not be examined.
  • static int LAYOUT_NO_START_CONTEXT -- A flag to layoutGlyphVector indicating that text in the char array before the indicated start should not be examined.
  • static int LAYOUT_RIGHT_TO_LEFT -- A flag to layoutGlyphVector indicating that text is right-to-left as determined by Bidi analysis.
  • static String MONOSPACED -- A String constant for the canonical family name of the logical font "Monospaced".
  • protected String name -- The logical name of this Font, as passed to the constructor.
  • static int PLAIN --The plain style constant.
  • protected float pointSize -- The point size of this Font in float.
  • static int ROMAN_BASELINE --The baseline used in most Roman scripts when laying out text.
  • static String SANS_SERIF -- A String constant for the canonical family name of the logical font "SansSerif".
  • static String SERIF -- A String constant for the canonical family name of the logical font "Serif".
  • protected int size --The point size of this Font, rounded to integer.
  • protected int style -- The style of this Font, as passed to the constructor.
  • static int TRUETYPE_FONT -- Identify a font resource of type TRUETYPE.
  • static int TYPE1_FONT -- Identify a font resource of type TYPE1.

Class constructors

S.N.Constructor & Description
1protected Font() ()
Creates a new Font from the specified font.
2Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Creates a new Font from the specified font.
3Font(String name, int style, int size)
Creates a new Font from the specified font.

Class methods

S.N.Method & Description
1boolean canDisplay(char c)
Checks if this Font has a glyph for the specified character.
2boolean canDisplay(int codePoint)
Checks if this Font has a glyph for the specified character.
3int canDisplayUpTo(char[] text, int start, int limit)
Indicates whether or not this Font can display the characters in the specified text starting at start and ending at limit.
4int canDisplayUpTo(CharacterIterator iter, int start, int limit)
Indicates whether or not this Font can display the text specified by the iter starting at start and ending at limit.
5int canDisplayUpTo(String str)
Indicates whether or not this Font can display a specified String.
6static Font createFont(int fontFormat, File fontFile)
Returns a new Font using the specified font type and the specified font file.
7static Font createFont(int fontFormat, InputStream fontStream)
Returns a new Font using the specified font type and input data.
8GlyphVector createGlyphVector(FontRenderContext frc, char[] chars)
Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.
9GlyphVector createGlyphVector(FontRenderContext frc, CharacterIterator ci)
Creates a GlyphVector by mapping the specified characters to glyphs one-to-one based on the Unicode cmap in this Font.
10GlyphVector createGlyphVector(FontRenderContext frc, int[] glyphCodes)
Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.
11GlyphVector createGlyphVector(FontRenderContext frc, String str)
Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.
12static Font decode(String str)
Returns the Font that the str argument describes.
13Font deriveFont(AffineTransform trans)
Creates a new Font object by replicating the current Font object and applying a new transform to it.
14Font deriveFont(float size)
Creates a new Font object by replicating the current Font object and applying a new size to it.
15Font deriveFont(int style)
Creates a new Font object by replicating the current Font object and applying a new style to it.
16Font deriveFont(int style, AffineTransform trans)
Creates a new Font object by replicating this Font object and applying a new style and transform.
17Font deriveFont(int style, float size)
Creates a new Font object by replicating this Font object and applying a new style and size.
18Font deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Creates a new Font object by replicating the current Font object and applying a new set of font attributes to it.
19boolean equals(Object obj)
Compares this Font object to the specified Object.
20protected void finalize()
Disposes the native Font object.
21Map<TextAttribute,?> getAttributes()
Returns a map of font attributes available in this Font.
22AttributedCharacterIterator.Attribute[] getAvailableAttributes()
Returns the keys of all the attributes supported by this Font.
23byte getBaselineFor(char c)
Returns the baseline appropriate for displaying this character.
24String getFamily()
Returns the family name of this Font.
25String getFamily(Locale l)
Returns the family name of this Font, localized for the specified locale.
26static Font getFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Returns a Font appropriate to the attributes.
27static Font getFont(String nm)
Returns a Font object fom the system properties list.
28static Font getFont(String nm, Font font)
Gets the specified Font from the system properties list.
29String getFontName()
Returns the font face name of this Font.
30String getFontName(Locale l)
Returns the font face name of the Font, localized for the specified locale.
31float getItalicAngle()
Returns the italic angle of this Font.
32LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc)
Returns a LineMetrics object created with the specified arguments.
33LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)
Returns a LineMetrics object created with the specified arguments.
34LineMetrics getLineMetrics(String str, FontRenderContext frc)
Returns a LineMetrics object created with the specified String and FontRenderContext.
35LineMetrics getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc)
Returns a LineMetrics object created with the specified arguments.
36Rectangle2D getMaxCharBounds(FontRenderContext frc)
Returns the bounds for the character with the maximum bounds as defined in the specified FontRenderContext.
37int getMissingGlyphCode()
Returns the glyphCode which is used when this Font does not have a glyph for a specified unicode code point.
38String getName()
Returns the logical name of this Font.
39int getNumGlyphs()
Returns the number of glyphs in this Font.
40java.awt.peer.FontPeer getPeer()
Deprecated. Font rendering is now platform independent.
41String getPSName()
Returns the postscript name of this Font.
42int getSize()
Returns the point size of this Font, rounded to an integer.
43float getSize2D()
Returns the point size of this Font in float value.
44Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc)
Returns the logical bounds of the specified array of characters in the specified FontRenderContext.
45Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)
Returns the logical bounds of the characters indexed in the specified CharacterIterator in the specified FontRenderContext.
46Rectangle2D getStringBounds(String str, FontRenderContext frc)
Returns the logical bounds of the specified String in the specified FontRenderContext.
47Rectangle2D getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc)
Returns the logical bounds of the specified String in the specified FontRenderContext.
48int getStyle()
Returns the style of this Font.
49AffineTransform getTransform()
Returns a copy of the transform associated with this Font.
50int hashCode()
Returns a hashcode for this Font.
51boolean hasLayoutAttributes()
Return true if this Font contains attributes that require extra layout processing.
52boolean hasUniformLineMetrics()
Checks whether or not this Font has uniform line metrics.
53boolean isBold()
Indicates whether or not this Font object's style is BOLD.
54boolean isItalic()
Indicates whether or not this Font object's style is ITALIC.
55boolean isPlain()
Indicates whether or not this Font object's style is PLAIN.
56boolean isTransformed()
Indicates whether or not this Font object has a transform that affects its size in addition to the Size attribute.
57GlyphVector layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags)
Returns a new GlyphVector object, performing full layout of the text if possible.
58String toString()
Converts this Font object to a String representation.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Font Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;        
      Font plainFont = new Font("Serif", Font.PLAIN, 24);        
      g2.setFont(plainFont);
      g2.drawString("Welcome to TutorialsPoint", 50, 70); 
      Font italicFont = new Font("Serif", Font.ITALIC, 24);        
      g2.setFont(italicFont);
      g2.drawString("Welcome to TutorialsPoint", 50, 120); 
      Font boldFont = new Font("Serif", Font.BOLD, 24);        
      g2.setFont(boldFont);
      g2.drawString("Welcome to TutorialsPoint", 50, 170); 
      Font boldItalicFont = new Font("Serif", Font.BOLD+Font.ITALIC, 24);        
      g2.setFont(boldItalicFont);
      g2.drawString("Welcome to TutorialsPoint", 50, 220); 
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Font

AWT Color Class

Introduction

The Color class states colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.

Class declaration

Following is the declaration for java.awt.Color class:
public class Color
   extends Object
      implements Paint, Serializable

Field

Following are the fields for java.awt.geom.Arc2D class:
  • static Color black -- The color black.
  • static Color BLACK -- The color black.
  • static Color blue -- The color blue.
  • static Color BLUE -- The color blue.
  • static Color cyan -- The color cyan.
  • static Color CYAN -- The color cyan.
  • static Color DARK_GRAY -- The color dark gray.
  • static Color darkGray -- The color dark gray.
  • static Color gray -- The color gray.
  • static Color GRAY -- The color gray.
  • static Color green -- The color green.
  • static Color GREEN -- The color green.
  • static Color LIGHT_GRAY -- The color light gray.
  • static Color lightGray -- The color light gray.
  • static Color magenta -- The color magenta.
  • static Color MAGENTA -- The color magenta.
  • static Color orange -- The color orange.
  • static Color ORANGE -- The color orange.
  • static Color pink -- The color pink.
  • static Color PINK -- The color pink.
  • static Color red -- The color red.
  • static Color RED -- The color red.
  • static Color white -- The color white.
  • static Color WHITE -- The color white.
  • static Color yellow -- The color yellow.
  • static Color YELLOW -- The color yellow.

Class constructors

S.N.Constructor & Description
1Color(ColorSpace cspace, float[] components, float alpha)
Creates a color in the specified ColorSpace with the color components specified in the float array and the specified alpha.
2Color(float r, float g, float b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0.0 - 1.0).
3Color(float r, float g, float b, float a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0.0 - 1.0).
4Color(int rgb)
Creates an opaque sRGB color with the specified combined RGB value consisting of the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
5Color(int rgba, boolean hasalpha)
Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7.
6Color(int r, int g, int b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).
7Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

Class methods

S.N.Method & Description
1Color brighter()
Creates a new Color that is a brighter version of this Color.
2PaintContext createContext(ColorModel cm, Rectangle r, Rectangle2D r2d, AffineTransform xform, RenderingHints hints)
Creates and returns a PaintContext used to generate a solid color pattern.
3Color darker()
Creates a new Color that is a darker version of this Color.
4static Color decode(String nm)
Converts a String to an integer and returns the specified opaque Color.
5boolean equals(Object obj)
Determines whether another object is equal to this Color.
6int getAlpha()
Returns the alpha component in the range 0-255.
7int getBlue()
Returns the blue component in the range 0-255 in the default sRGB space.
8static Color getColor(String nm)
Finds a color in the system properties.
9static Color getColor(String nm, Color v)
Finds a color in the system properties.
10static Color getColor(String nm, int v)
Finds a color in the system properties.
11float[] getColorComponents(ColorSpace cspace, float[] compArray)
Returns a float array containing only the color components of the Color in the ColorSpace specified by the cspace parameter.
12float[] getColorComponents(float[] compArray)
Returns a float array containing only the color components of the Color, in the ColorSpace of the Color.
13ColorSpace getColorSpace()
Returns the ColorSpace of this Color.
14float[] getComponents(ColorSpace cspace, float[] compArray)
Returns a float array containing the color and alpha components of the Color, in the ColorSpace specified by the cspace parameter.
15float[] getComponents(float[] compArray)
Returns a float array containing the color and alpha components of the Color, in the ColorSpace of the Color.
16int getGreen()
Returns the green component in the range 0-255 in the default sRGB space.
17static Color getHSBColor(float h, float s, float b)
Creates a Color object based on the specified values for the HSB color model.
18int getRed()
Returns the red component in the range 0-255 in the default sRGB space.
19int getRGB()
Returns the RGB value representing the color in the default sRGB ColorModel.
20float[] getRGBColorComponents(float[] compArray)
Returns a float array containing only the color components of the Color, in the default sRGB color space.
21float[] getRGBComponents(float[] compArray)
Returns a float array containing the color and alpha components of the Color, as represented in the default sRGB color space.
22int getTransparency()
Returns the transparency mode for this Color.
23int hashCode()
Computes the hash code for this Color.
24static int HSBtoRGB(float hue, float saturation, float brightness)
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.
25static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals)
Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and brightness that are the three components of the HSB model.
26String toString()
Returns a string representation of this Color.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Color Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;        
      Font plainFont = new Font("Serif", Font.PLAIN, 24);        
      g2.setFont(plainFont);
      g2.setColor(Color.red);
      g2.drawString("Welcome to TutorialsPoint", 50, 70); 
      g2.setColor(Color.GRAY);
      g2.drawString("Welcome to TutorialsPoint", 50, 120);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Color

AWT BasicStroke Class

Introduction

The BasicStroke class states colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.

Class declaration

Following is the declaration for java.awt.BasicStroke class:
public class BasicStroke
   extends Object
      implements Stroke

Field

Following are the fields for java.awt.geom.Arc2D class:
  • static int CAP_BUTT -- Ends unclosed subpaths and dash segments with no added decoration.
  • static int CAP_ROUND -- Ends unclosed subpaths and dash segments with a round decoration that has a radius equal to half of the width of the pen.
  • static int CAP_SQUARE -- Ends unclosed subpaths and dash segments with a square projection that extends beyond the end of the segment to a distance equal to half of the line width.
  • static int JOIN_BEVEL -- Joins path segments by connecting the outer corners of their wide outlines with a straight segment.
  • static int JOIN_MITER -- Joins path segments by extending their outside edges until they meet.
  • static int JOIN_ROUND -- Joins path segments by rounding off the corner at a radius of half the line width.

Class constructors

S.N.Constructor & Description
1BasicStroke()
Constructs a new BasicStroke with defaults for all attributes.
2BasicStroke(float width)
Constructs a solid BasicStroke with the specified line width and with default values for the cap and join styles.
3BasicStroke(float width, int cap, int join)
Constructs a solid BasicStroke with the specified attributes.
4BasicStroke(float width, int cap, int join, float miterlimit)
Constructs a solid BasicStroke with the specified attributes.
5BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase)
Constructs a new BasicStroke with the specified attributes.

Class methods

S.N.Method & Description
1Shape createStrokedShape(Shape s)
Returns a Shape whose interior defines the stroked outline of a specified Shape.
2boolean equals(Object obj)
Tests if a specified object is equal to this BasicStroke by first testing if it is a BasicStroke and then comparing its width, join, cap, miter limit, dash, and dash phase attributes with those of this BasicStroke.
3float[] getDashArray()
Returns the array representing the lengths of the dash segments.
4float getDashPhase()
Returns the current dash phase.
5int getEndCap()
Returns the end cap style.
6int getLineJoin()
Returns the line join style.
7float getLineWidth()
Returns the line width.
8float getMiterLimit()
Returns the limit of miter joins.
9int hashCode()
Returns the hashcode for this stroke.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

BasicStroke Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;        
      g2.setStroke(new BasicStroke(3.0f));
      g2.setPaint(Color.blue);

      Rectangle2D shape = new Rectangle2D.Float();
      shape.setFrame(100, 150, 200,100);
      g2.draw(shape);

      Rectangle2D shape1 = new Rectangle2D.Float();
      shape1.setFrame(110, 160, 180,80);
      g2.setStroke(new BasicStroke(1.0f));
   
      g2.draw(shape1);
      Font plainFont = new Font("Serif", Font.PLAIN, 24);        
      g2.setFont(plainFont);
      g2.setColor(Color.DARK_GRAY);
      g2.drawString("TutorialsPoint", 130, 200);
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtGraphicsDemo
Verify the following output
AWT BasicStroke

Java Awt Quick Guide 11

AWT Graphics Class

Introduction

The Graphics class is the abstract super class for all graphics contexts which allow an application to draw onto components that can be realized on various devices, or onto off-screen images as well.
A Graphics object encapsulates all state information required for the basic rendering operations that Java supports. State information includes the following properties.
  • The Component object on which to draw.
  • A translation origin for rendering and clipping coordinates.
  • The current clip.
  • The current color.
  • The current font.
  • The current logical pixel operation function.
  • The current XOR alternation color

Class declaration

Following is the declaration for java.awt.Graphics class:
public abstract class Graphics
   extends Object

Class constructors

S.N.Constructor & Description
1Graphics() ()
Constructs a new Graphics object.

Class methods

S.N.Method & Description
1abstract void clearRect(int x, int y, int width, int height)
Clears the specified rectangle by filling it with the background color of the current drawing surface.
2abstract void clipRect(int x, int y, int width, int height)
Intersects the current clip with the specified rectangle.
3abstract void copyArea(int x, int y, int width, int height, int dx, int dy)
Copies an area of the component by a distance specified by dx and dy.
4abstract Graphics create()
Creates a new Graphics object that is a copy of this Graphics object.
5Graphics create(int x, int y, int width, int height)
Creates a new Graphics object based on this Graphics object, but with a new translation and clip area.
6abstract void dispose()
Disposes of this graphics context and releases any system resources that it is using.
7void draw3DRect(int x, int y, int width, int height, boolean raised)
Draws a 3-D highlighted outline of the specified rectangle.
8abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified rectangle.
9void drawBytes(byte[] data, int offset, int length, int x, int y)
Draws the text given by the specified byte array, using this graphics context's current font and color.
10void drawChars(char[] data, int offset, int length, int x, int y)
Draws the text given by the specified character array, using this graphics context's current font and color.
11abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
Draws as much of the specified image as is currently available.
12abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)
Draws as much of the specified image as is currently available.
13abstract boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.
14abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.
15abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.
16abstract boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface.
17abstract void drawLine(int x1, int y1, int x2, int y2)
Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.
18abstract void drawOval(int x, int y, int width, int height)
Draws the outline of an oval.
19abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates.
20void drawPolygon(Polygon p)
Draws the outline of a polygon defined by the specified Polygon object.
21abstract void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
Draws a sequence of connected lines defined by arrays of x and y coordinates.
22void drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle.
23abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Draws an outlined round-cornered rectangle using this graphics context's current color.
24abstract void drawString(AttributedCharacterIterator iterator, int x, int y)
Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.
25abstract void drawString(String str, int x, int y)
Draws the text given by the specified string, using this graphics context's current font and color.
26void fill3DRect(int x, int y, int width, int height, boolean raised)
Paints a 3-D highlighted rectangle filled with the current color.
27abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.
28abstract void fillOval(int x, int y, int width, int height)
Fills an oval bounded by the specified rectangle with the current color.
29abstract void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
30void fillPolygon(Polygon p)
Fills the polygon defined by the specified Polygon object with the graphics context's current color.
31abstract void fillRect(int x, int y, int width, int height)
Fills the specified rectangle.
32abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Fills the specified rounded corner rectangle with the current color.
33void finalize()
Disposes of this graphics context once it is no longer referenced.
34abstract Shape getClip()
Gets the current clipping area.
35abstract Rectangle getClipBounds()
Returns the bounding rectangle of the current clipping area.
36Rectangle getClipBounds(Rectangle r)
Returns the bounding rectangle of the current clipping area.
37Rectangle getClipRect()
Deprecated. As of JDK version 1.1, replaced by getClipBounds().
38abstract Color getColor()
Gets this graphics context's current color.
39abstract Font getFont()
Gets the current font.
40FontMetrics getFontMetrics()
Gets the font metrics of the current font.
41abstract FontMetrics getFontMetrics(Font f)
Gets the font metrics for the specified font.
42boolean hitClip(int x, int y, int width, int height)
Returns true if the specified rectangular area might intersect the current clipping area.
43abstract void setClip(int x, int y, int width, int height)
Sets the current clip to the rectangle specified by the given coordinates.
44abstract void setClip(Shape clip)
Sets the current clipping area to an arbitrary clip shape.
45abstract void setColor(Color c)
Sets this graphics context's current color to the specified color.
46abstract void setFont(Font font)
Sets this graphics context's font to the specified font.
47abstract void setPaintMode()
Sets the paint mode of this graphics context to overwrite the destination with this graphics context's current color.
48abstract void setXORMode(Color c1)
Sets the paint mode of this graphics context to alternate between this graphics context's current color and the new specified color.
49String toString()
Returns a String object representing this Graphics object's value.
50abstract void translate(int x, int y)
Translates the origin of the graphics context to the point (x, y) in the current coordinate system.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Graphics Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      g.setColor(Color.GRAY);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 150);      
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Graphics

AWT Graphics2D Class

Introduction

The Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout.

Class declaration

Following is the declaration for java.awt.Graphics2D class:
public abstract class Graphics2D
   extends Graphics

Class constructors

S.N.Constructor & Description
1Graphics2D()
Constructs a new Graphics2D object.

Class methods

S.N.Method & Description
1abstract void addRenderingHints(Map hints)
Sets the values of an arbitrary number of preferences for the rendering algorithms.
2abstract void clip(Shape s)
Intersects the current Clip with the interior of the specified Shape and sets the Clip to the resulting intersection.
3abstract void draw(Shape s)
Strokes the outline of a Shape using the settings of the current Graphics2D context.
4void draw3DRect(int x, int y, int width, int height, boolean raised)
Draws a 3-D highlighted outline of the specified rectangle.
5abstract void drawGlyphVector(GlyphVector g, float x, float y)
Renders the text of the specified GlyphVector using the Graphics2D context's rendering attributes.
6abstract void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
Renders a BufferedImage that is filtered with a BufferedImageOp.
7abstract boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
Renders an image, applying a transform from image space into user space before drawing.
8abstract void drawRenderableImage(RenderableImage img, AffineTransform xform)
Renders a RenderableImage, applying a transform from image space into user space before drawing.
9abstract void drawRenderedImage(RenderedImage img, AffineTransform xform)
Renders a RenderedImage, applying a transform from image space into user space before drawing.
10abstract void drawString(AttributedCharacterIterator iterator, float x, float y)
Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.
11abstract void drawString(AttributedCharacterIterator iterator, int x, int y)
Renders the text of the specified iterator applying its attributes in accordance with the specification of the TextAttribute class.
12abstract void drawString(String str, float x, float y)
Renders the text specified by the specified String, using the current text attribute state in the Graphics2D context.
13abstract void drawString(String str, int x, int y)
Renders the text of the specified String, using the current text attribute state in the Graphics2D context.
14abstract void fill(Shape s)
Fills the interior of a Shape using the settings of the Graphics2D context.
15void fill3DRect(int x, int y, int width, int height, boolean raised)
Paints a 3-D highlighted rectangle filled with the current color.
16abstract Color getBackground()
Returns the background color used for clearing a region.
17abstract Composite getComposite()
Returns the current Composite in the Graphics2D context.
18abstract GraphicsConfiguration getDeviceConfiguration()
Returns the device configuration associated with this Graphics2D.
19abstract FontRenderContext getFontRenderContext()
Get the rendering context of the Font within this Graphics2D context.
20abstract Paint getPaint()
Returns the current Paint of the Graphics2D context.
21abstract Object getRenderingHint(RenderingHints.Key hintKey)
Returns the value of a single preference for the rendering algorithms.
22abstract RenderingHints getRenderingHints()
Gets the preferences for the rendering algorithms.
23abstract Stroke getStroke()
Returns the current Stroke in the Graphics2D context.
24abstract AffineTransform getTransform()
Returns a copy of the current Transform in the Graphics2D context.
25abstract boolean hit(Rectangle rect, Shape s, boolean onStroke)
Checks whether or not the specified Shape intersects the specified Rectangle, which is in device space.
26abstract void rotate(double theta)
Concatenates the current Graphics2D Transform with a rotation transform.
27abstract void rotate(double theta, double x, double y)
Concatenates the current Graphics2D Transform with a translated rotation transform.
28abstract void scale(double sx, double sy)
Concatenates the current Graphics2D Transform with a scaling transformation Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling.
29abstract void setBackground(Color color)
Sets the background color for the Graphics2D context.
30abstract void setComposite(Composite comp)
Sets the Composite for the Graphics2D context.
31abstract void setPaint(Paint paint)
Sets the Paint attribute for the Graphics2D context.
32abstract void setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
Sets the value of a single preference for the rendering algorithms.
33abstract void setRenderingHints(Map hints)
Replaces the values of all preferences for the rendering algorithms with the specified hints.
34abstract void setStroke(Stroke s)
Sets the Stroke for the Graphics2D context.
35abstract void setTransform(AffineTransform Tx)
Overwrites the Transform in the Graphics2D context.
36abstract void shear(double shx, double shy)
Concatenates the current Graphics2D Transform with a shearing transform.
37abstract void transform(AffineTransform Tx)
Composes an AffineTransform object with the Transform in this Graphics2D according to the rule last-specified-first-applied.
38abstract void translate(double tx, double ty)
Concatenates the current Graphics2D Transform with a translation transform.
39abstract void translate(int x, int y)
Translates the origin of the Graphics2D context to the point (x, y) in the current coordinate system.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Graphics2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
         RenderingHints.VALUE_ANTIALIAS_ON);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g2.drawString("Welcome to TutorialsPoint", 50, 70); 
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Graphics2D

AWT Arc2D Class

Introduction

The Arc2D class is the superclass for all objects that store a 2D arc defined by a framing rectangle, start angle, angular extent (length of the arc), and a closure type (OPEN, CHORD, or PIE).

Class declaration

Following is the declaration for java.awt.Arc2D class:
public abstract class Arc2D
   extends RectangularShape

Field

Following are the fields for java.awt.geom.Arc2D class:
  • static int CHORD -- The closure type for an arc closed by drawing a straight line segment from the start of the arc segment to the end of the arc segment.
  • static int OPEN -- The closure type for an open arc with no path segments connecting the two ends of the arc segment.
  • static int PIE -- The closure type for an arc closed by drawing straight line segments from the start of the arc segment to the center of the full ellipse and from that point to the end of the arc segment.

Class constructors

S.N.Constructor & Description
1protected Arc2D(int type)
This is an abstract class that cannot be instantiated directly.

Class methods

S.N.Method & Description
1boolean contains(double x, double y)
Determines whether or not the specified point is inside the boundary of the arc.
2boolean contains(double x, double y, double w, double h)
Determines whether or not the interior of the arc entirely contains the specified rectangle.
3boolean contains(Rectangle2D r)
Determines whether or not the interior of the arc entirely contains the specified rectangle.
4boolean containsAngle(double angle)
Determines whether or not the specified angle is within the angular extents of the arc.
5boolean equals(Object obj)
Determines whether or not the specified Object is equal to this Arc2D.
6abstract double getAngleExtent()
Returns the angular extent of the arc.
7abstract double getAngleStart()
Returns the starting angle of the arc.
8int getArcType()
Returns the arc closure type of the arc: OPEN, CHORD, or PIE.
9Rectangle2D getBounds2D()
Returns the high-precision framing rectangle of the arc.
10Point2D getEndPoint()
Returns the ending point of the arc.
11PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of the arc.
12Point2D getStartPoint()
Returns the starting point of the arc.
13int hashCode()
Returns the hashcode for this Arc2D.
14boolean intersects(double x, double y, double w, double h)
Determines whether or not the interior of the arc intersects the interior of the specified rectangle.
15protected abstract Rectangle2D makeBounds(double x, double y, double w, double h)
Constructs a Rectangle2D of the appropriate precision to hold the parameters calculated to be the framing rectangle of this arc.
16abstract void setAngleExtent(double angExt)
Sets the angular extent of this arc to the specified double value.
17void setAngles(double x1, double y1, double x2, double y2)
Sets the starting angle and angular extent of this arc using two sets of coordinates.
18void setAngles(Point2D p1, Point2D p2)
Sets the starting angle and angular extent of this arc using two points.
19abstract void setAngleStart(double angSt)
Sets the starting angle of this arc to the specified double value.
20void setAngleStart(Point2D p)
Sets the starting angle of this arc to the angle that the specified point defines relative to the center of this arc.
21void setArc(Arc2D a)
Sets this arc to be the same as the specified arc.
22abstract void setArc(double x, double y, double w, double h, double angSt, double angExt, int closure)
Sets the location, size, angular extents, and closure type of this arc to the specified double values.
23void setArc(Point2D loc, Dimension2D size, double angSt, double angExt, int closure)
Sets the location, size, angular extents, and closure type of this arc to the specified values.
24void setArc(Rectangle2D rect, double angSt, double angExt, int closure)
Sets the location, size, angular extents, and closure type of this arc to the specified values.
25void setArcByCenter(double x, double y, double radius, double angSt, double angExt, int closure)
Sets the position, bounds, angular extents, and closure type of this arc to the specified values.
26void setArcByTangent(Point2D p1, Point2D p2, Point2D p3, double radius)
Sets the position, bounds, and angular extents of this arc to the specified value.
27void setArcType(int type)
Sets the closure type of this arc to the specified value: OPEN, CHORD, or PIE.
28void setFrame(double x, double y, double w, double h)
Sets the location and size of the framing rectangle of this Shape to the specified rectangular values.

Methods inherited

This class inherits methods from the following classes:
  • java.awt.geom.RectangularShape
  • java.lang.Object

Arc2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Arc2D.Float arc = new Arc2D.Float(Arc2D.PIE);
      arc.setFrame(70, 200, 150, 150);
      arc.setAngleStart(0);
      arc.setAngleExtent(145);
      Graphics2D g2 = (Graphics2D) g; 
      g2.setColor(Color.gray);
      g2.draw(arc);
      g2.setColor(Color.red);
      g2.fill(arc);
      g2.setColor(Color.black);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Arc2D.PIE", 100, 120); 
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtGraphicsDemo
Verify the following output
AWT Arc2D

AWT CubicCurve2D Class

Introduction

The CubicCurve2D class states a cubic parametric curve segment in (x,y) coordinate space.

Class declaration

Following is the declaration for java.awt.geom.CubicCurve2D class:
public abstract class CubicCurve2D
   extends Object
      implements Shape, Cloneable

Class constructors

S.N.Constructor & Description
1protected CubicCurve2D()
This is an abstract class that cannot be instantiated directly.

Class methods

S.N.Method & Description
1Object clone()
Creates a new object of the same class as this object.
2boolean contains(double x, double y)
Tests if the specified coordinates are inside the boundary of the Shape.
3boolean contains(double x, double y, double w, double h)
Tests if the interior of the Shape entirely contains the specified rectangular area.
4boolean contains(Point2D p)
Tests if a specified Point2D is inside the boundary of the Shape.
5boolean contains(Rectangle2D r)
Tests if the interior of the Shape entirely contains the specified Rectangle2D.
6Rectangle getBounds()
Returns an integer Rectangle that completely encloses the Shape.
7abstract Point2D getCtrlP1()
Returns the first control point.
8abstract Point2D getCtrlP2()
Returns the second control point.
9abstract double getCtrlX1()
Returns the X coordinate of the first control point in double precision.
10abstract double getCtrlX2()
Returns the X coordinate of the second control point in double precision.
11abstract double getCtrlY1()
Returns the Y coordinate of the first control point in double precision.
12abstract double getCtrlY2()
Returns the Y coordinate of the second control point in double precision.
13double getFlatness()
Returns the flatness of this curve.
14static double getFlatness(double[] coords, int offset)
Returns the flatness of the cubic curve specified by the control points stored in the indicated array at the indicated index.
15static double getFlatness(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)
Returns the flatness of the cubic curve specified by the indicated control points.
16double getFlatnessSq()
Returns the square of the flatness of this curve.
17static double getFlatnessSq(double[] coords, int offset)
Returns the square of the flatness of the cubic curve specified by the control points stored in the indicated array at the indicated index.
18static double getFlatnessSq(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)
Returns the square of the flatness of the cubic curve specified by the indicated control points.
19abstract Point2D getP1()
Returns the start point.
20abstract Point2D getP2()
Returns the end point.
21PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of the shape.
22PathIterator getPathIterator(AffineTransform at, double flatness)
Return an iteration object that defines the boundary of the flattened shape.
23abstract double getX1()
Returns the X coordinate of the start point in double precision.
24abstract double getX2()
Returns the X coordinate of the end point in double precision.
25abstract double getY1()
Returns the Y coordinate of the start point in double precision.
26abstract double getY2()
Returns the Y coordinate of the end point in double precision.
27boolean intersects(double x, double y, double w, double h)
Tests if the interior of the Shape intersects the interior of a specified rectangular area.
28boolean intersects(Rectangle2D r)
Tests if the interior of the Shape intersects the interior of a specified Rectangle2D.
29void setCurve(CubicCurve2D c)
Sets the location of the end points and control points of this curve to the same as those in the specified CubicCurve2D.
30void setCurve(double[] coords, int offset)
Sets the location of the end points and control points of this curve to the double coordinates at the specified offset in the specified array.
31abstract void setCurve(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)
Sets the location of the end points and control points of this curve to the specified double coordinates.
32void setCurve(Point2D[] pts, int offset)
Sets the location of the end points and control points of this curve to the coordinates of the Point2D objects at the specified offset in the specified array.
33void setCurve(Point2D p1, Point2D cp1, Point2D cp2, Point2D p2)
Sets the location of the end points and control points of this curve to the specified Point2D coordinates.
34static int solveCubic(double[] eqn)
Solves the cubic whose coefficients are in the eqn array and places the non-complex roots back into the same array, returning the number of roots.
35static int solveCubic(double[] eqn, double[] res)
Solve the cubic whose coefficients are in the eqn array and place the non-complex roots into the res array, returning the number of roots.
36void subdivide(CubicCurve2D left, CubicCurve2D right)
Subdivides this cubic curve and stores the resulting two subdivided curves into the left and right curve parameters.
37static void subdivide(CubicCurve2D src, CubicCurve2D left, CubicCurve2D right)
Subdivides the cubic curve specified by the src parameter and stores the resulting two subdivided curves into the left and right curve parameters.
38static void subdivide(double[] src, int srcoff, double[] left, int leftoff, double[] right, int rightoff)
Subdivides the cubic curve specified by the coordinates stored in the src array at indices srcoff through (srcoff + 7) and stores the resulting two subdivided curves into the two result arrays at the corresponding indices.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

CubicCurve2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      CubicCurve2D shape = new CubicCurve2D.Float();
      shape.setCurve(250F,250F,20F,90F,140F,100F,350F,330F);       
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("CubicCurve2D.Curve", 100, 120);
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT CubicCurve2D

AWT Ellipse2D Class

Introduction

The Ellipse2D class states an ellipse that is defined by a framing rectangle.

Class declaration

Following is the declaration for java.awt.geom.Ellipse2D class:
public abstract class Ellipse2D
   extends RectangularShape

Class constructors

S.N.Constructor & Description
1protected Ellipse2D()
This is an abstract class that cannot be instantiated directly.

Class methods

S.N.Method & Description
1boolean contains(double x, double y)
Tests if the specified coordinates are inside the boundary of the Shape.
2boolean contains(double x, double y, double w, double h)
Tests if the interior of the Shape entirely contains the specified rectangular area.
3boolean equals(Object obj)
Determines whether or not the specified Object is equal to this Ellipse2D.
4PathIterator getPathIterator(AffineTransform at)
Returns an iteration object that defines the boundary of this Ellipse2D.
5int hashCode()
Returns the hashcode for this Ellipse2D.
6boolean intersects(double x, double y, double w, double h)
Tests if the interior of the Shape intersects the interior of a specified rectangular area.

Methods inherited

This class inherits methods from the following classes:
  • java.lang.Object

Ellipse2D Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
AWTGraphicsDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class AWTGraphicsDemo extends Frame {
       
   public AWTGraphicsDemo(){
      super("Java AWT Examples");
      prepareGUI();
   }

   public static void main(String[] args){
      AWTGraphicsDemo  awtGraphicsDemo = new AWTGraphicsDemo();  
      awtGraphicsDemo.setVisible(true);
   }

   private void prepareGUI(){
      setSize(400,400);
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      }); 
   }    

   @Override
   public void paint(Graphics g) {
      Ellipse2D shape = new Ellipse2D.Float();
      shape.setFrame(100, 150, 200,100);
      Graphics2D g2 = (Graphics2D) g; 
      g2.draw (shape);
      Font font = new Font("Serif", Font.PLAIN, 24);
      g2.setFont(font);
      g.drawString("Welcome to TutorialsPoint", 50, 70);
      g2.drawString("Ellipse2D.Oval", 100, 120); 
   }
}
Compile the program using command prompt. Go to D:/ > AWT and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AWTGraphicsDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AWTGraphicsDemo
Verify the following output
AWT Ellipse2D