Sponsored sites

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

No comments:

Post a Comment