The Java 2D API provides two-dimensional graphics, text, and imaging capabilities for Java programs through extensions to the Abstract Windowing Toolkit (AWT). Java.awt.geom package provides the Java 2D classes for defining and performing operations on objects related to two-dimensional geometry.
AWT provides Graphics2D class that extends Graphics class that provides more sophisticated control over geometry, coordinate transformations, color management, and text layout. This class is also used to draw 2D graphics provided by java.awt.geom package that we will learn next.
Graphics2D class has the following declaration -
public abstract class Graphics2D extends Graphics
Graphics2D graphics2dObjName = (Graphics2D) graphicsObject;
Some important methods of Graphics2D are as follows -
public abstract void draw(Shape s)
public abstract void fill(Shape s)
public abstract void setStroke(Stroke s)
public abstract void translate(int x, int y)
public abstract void rotate(double theta)
public void draw3DRect(int x, int y, int width, int height, boolean raised)
public void fill3DRect(int x, int y, int width, int height, boolean raised)
import java.awt.*; import java.applet.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; BasicStroke bs = new BasicStroke(10.0f); gd.setStroke(bs); gd.drawRect(10,10,100,100); int width = getSize().width; int height = getSize().height; gd.translate(width/2, height/2); gd.drawRect(10,10,100,100); } }
public BasicStroke(float width) Where width - the width of the BasicStroke It constructs a solid BasicStroke with the specified line width.
public Dimension getSize()The java.awt.Dimension class encapsulates the width and height of a component (in integer precision) in a single object. We can get the width and height of current component (i.e. Applet) as follows -
int width = getSize().width; int height = getSize().height;
gd.translate(width/2, height/2);
Provides the Java 2D classes for defining and performing operations on objects related to two-dimensional geometry. Some popular classes of the package are as follows -
Used to draw a 2D line. It is an abstract class, so we cannot directly instantiated directly.
It has the following two sub classes -
Line2D.Double() It constructs and initializes a Line with coordinates (0, 0) to (0, 0).
Line2D.Double(double x1, double y1, double x2, double y2) Constructs and initializes a Line2D from (x1, y1) to (x2, y2)coordinates.
Line2D.Float() Constructs and initializes a Line with coordinates (0, 0) to (0, 0).
Line2D.Float(float x1, float y1, float x2, float y2) Constructs and initializes a Line from (x1, y1) to (x2, y2) coordinates.
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; Line2D ln1 = new Line2D.Float(50.0f,20.12f,200.13f,20.4f); Line2D ln2 = new Line2D.Double(70.5,40.5,200.4,40.5); gd.draw(ln1); gd.draw(ln2); } }
The Rectangle2D class describes a rectangle defined by a location (x,y) and dimension (width x height). It is an abstract class, so we cannot instantiated it directly.
It has two subclasses -
Rectangle2D.Double() Constructs a new Rectangle2D, initialized to location (0, 0) and size (0, 0).
Rectangle2D.Double(double x, double y, double width, double height) Constructs and initializes a Rectangle2D whose starting points are specified by (x, y) and width and height are specified by width and height attributes.
Rectangle2D.Float() Constructs a new Rectangle2D, initialized to location (0.0, 0.0) and size (0.0, 0.0).
Rectangle2D.Float(float x, float y, float width, float height) Constructs and initializes a Rectangle2D whose starting points are specified by (x, y) and width and height are specified by width and height attributes.
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; Rectangle2D r1 = new Rectangle2D.Float(10.5f,10.5f,200.0f,100.0f); gd.draw(r1); //or gd.fill(r1); Rectangle2D r2= new Rectangle2D.Double(50.55,50.55,200.00,100.00); gd.fill(r2); //or gd.draw(r2); } }
The RoundRectangle2D class defines a rectangle with rounded corners defined by a location (x,y), a dimension (w x h), and the width and height of an arc with which to round the corners. It is an abstract class, so we cannot instantiated it directly.
It has two subclasses -
RoundRectangle2D.Double() It constructs a new RoundRectangle2D, initialized to location (0.0, 0.0), size (0.0, 0.0), and corner arcs of radius 0.0.
RoundRectangle2D.Double(double x, double y, double w, double h, double arc_width, double arc_height) - It constructs and initializes a RoundRectangle2D that starts from (x, y) , size (width, height), and corner arcs of size (arc_width, arc_height).
RoundRectangle2D.Float() It constructs a new RoundRectangle2D, initialized to location (0.0, 0.0), size (0.0, 0.0), and corner arcs of radius 0.0.
RoundRectangle2D.Float(float x, float y, float width, float height, float arc_width, float arc_height) It constructs and initializes a RoundRectangle2D from the specified float coordinates.
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; RoundRectangle2D r1 = new RoundRectangle2D.Float(10.5f,10.5f,200.0f,100.0f, 30.5f, 40.5f); gd.draw(r1); //or gd.fill(r1); RoundRectangle2D r2= new RoundRectangle2D.Double(50.55,50.55,200.00,100.00, 30.50, 40.50); gd.fill(r2); //or gd.draw(r2); } }
Ellipse2D.Double()- It constructs a new Ellipse2D, initialized to location (0, 0) and size (0, 0).
Ellipse2D.Double(double x, double y, double width, double height)- It constructs and initializes an Ellipse2D within a rectangle whose starting points are (x, y) and width and height is specified by (width, height).
Ellipse2D.Float()- It constructs a new Ellipse2D, initialized to location (0, 0) and size (0, 0).
Ellipse2D.Float(float x, float y, float w, float h)- It constructs and initializes an Ellipse2D from the specified coordinates.
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; Ellipse2D e1 = new Ellipse2D.Float(10.5f,10.5f,200.0f,100.0f); gd.draw(e1); //or gd.fill(r1); Ellipse2D e2= new Ellipse2D.Double(50.55,50.55,200.00,100.00); gd.fill(e2); //or gd.draw(r2); } }
It is similar to the arc we used previously with the additional option of defining arc closure type that can have one of the following values - OPEN, CHORD or PIE
These are the static integer fields have the following meanings -
Arc2D.Double() - It constructs a new OPEN arc, initialized to location (0, 0), size (0, 0), angular extents (start = 0, extent = 0).
Arc2D.Double(double x, double y, double w, double h, double start, double extent, int type)- It constructs a new arc, initialized to the specified location, size, angular extents, and closure type. Parameters - x - The X coordinate of the upper-left corner of the arc's framing rectangle. y - The Y coordinate of the upper-left corner of the arc's framing rectangle. w - The overall width of the full ellipse of which this arc is a partial section. h - The overall height of the full ellipse of which this arc is a partial section. start - The starting angle of the arc in degrees. extent - The angular extent of the arc in degrees. type - The closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE.
Arc2D.Double(int type)- It constructs a new arc, initialized to location (0, 0), size (0, 0), angular extents (start = 0, extent = 0), and the specified closure type. Parameters - type - The closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE.
Arc2D.Double(Rectangle2D ellipseBounds, double start, double extent, int type) - It constructs a new arc, initialized to the specified location, size, angular extents, and closure type. Parameters - ellipseBounds - The framing rectangle that defines the outer boundary of the full ellipse of which this arc is a partial section. start - The starting angle of the arc in degrees. extent - The angular extent of the arc in degrees. type - The closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE.
Arc2D.Float() - It constructs a new OPEN arc, initialized to location (0, 0), size (0, 0), angular extents (start = 0, extent = 0).
Arc2D.Float(float x, float y, float w, float h, float start, float extent, int type) - It constructs a new arc, initialized to the specified location, size, angular extents, and closure type. Parameters - x - The X coordinate of the upper-left corner of the arc's framing rectangle. y - The Y coordinate of the upper-left corner of the arc's framing rectangle. w - The overall width of the full ellipse of which this arc is a partial section. h - The overall height of the full ellipse of which this arc is a partial section. start - The starting angle of the arc in degrees. extent - The angular extent of the arc in degrees. type - The closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE.
Arc2D.Float(int type) - It constructs a new arc, initialized to location (0, 0), size (0, 0), angular extents (start = 0, extent = 0), and the specified closure type. Parameters - type - The closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE.
Arc2D.Float(Rectangle2D ellipseBounds, float start, float extent, int type) - It constructs a new arc, initialized to the specified location, size, angular extents, and closure type. Parameters - ellipseBounds - The framing rectangle that defines the outer boundary of the full ellipse of which this arc is a partial section. start - The starting angle of the arc in degrees. extent - The angular extent of the arc in degrees. type - The closure type for the arc: Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE.
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; Arc2D a1 = new Arc2D.Double(10,10,100,100,90,90,Arc2D.OPEN); Arc2D a2 = new Arc2D.Double(50,50,100,100,90,90,Arc2D.CHORD); Arc2D a3 = new Arc2D.Double(80,80,100,100,90,90,Arc2D.PIE); gd.draw(a1); // or gd.fill(a1); gd.draw(a2); // or gd.fill(a2); gd.draw(a3); // or gd.fill(a3); } }
The CubicCurve2D class defines a cubic parametric curve segment in (x,y) coordinate space. It is an abstract class, so we cannot instantiated it directly.
It has two subclasses -
CubicCurve2D.Double() - It constructs and initializes a CubicCurve with coordinates (0, 0, 0, 0, 0, 0, 0, 0).
CubicCurve2D.Double(double x1, double y1, double ctrlx1, double ctrly1, double ctrlx2, double ctrly2, double x2, double y2) - It constructs and initializes a CubicCurve2D from the specified double coordinates. Parameters - x1 - the X coordinate for the start point of the resulting CubicCurve2D y1 - the Y coordinate for the start point of the resulting CubicCurve2D ctrlx1 - the X coordinate for the first control point of the resulting CubicCurve2D ctrly1 - the Y coordinate for the first control point of the resulting CubicCurve2D ctrlx2 - the X coordinate for the second control point of the resulting CubicCurve2D ctrly2 - the Y coordinate for the second control point of the resulting CubicCurve2D x2 - the X coordinate for the end point of the resulting CubicCurve2D y2 - the Y coordinate for the end point of the resulting CubicCurve2D
CubicCurve2D.Float() - It constructs and initializes a CubicCurve with coordinates (0, 0, 0, 0, 0, 0, 0, 0).
CubicCurve2D.Float(float x1, float y1, float ctrlx1, float ctrly1, float ctrlx2, float ctrly2, float x2, float y2) - It constructs and initializes a CubicCurve2D from the specified float coordinates. Parameters - x1 - the X coordinate for the start point of the resulting CubicCurve2D y1 - the Y coordinate for the start point of the resulting CubicCurve2D ctrlx1 - the X coordinate for the first control point of the resulting CubicCurve2D ctrly1 - the Y coordinate for the first control point of the resulting CubicCurve2D ctrlx2 - the X coordinate for the second control point of the resulting CubicCurve2D ctrly2 - the Y coordinate for the second control point of the resulting CubicCurve2D x2 - the X coordinate for the end point of the resulting CubicCurve2D y2 - the Y coordinate for the end point of the resulting CubicCurve2D
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; CubicCurve2D c1 = new CubicCurve2D.Double(200,100,100,150,300,250,200,300); gd.draw(c1); } }
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; CubicCurve2D c1 = new CubicCurve2D.Double(200,100,250, 200,180, 250,200,350); CubicCurve2D c2 = new CubicCurve2D.Double(260,100,300, 200,210, 230,200,350); Line2D l1 = new Line2D.Double(200, 100, 230, 130); Line2D l2 = new Line2D.Double(260, 100, 230, 130); gd.draw(c1); gd.draw(c2); gd.draw(l1); gd.draw(l2); } }Using the classes and methods we learn till now, it is not possible to fill the above shape with any color. To fill such shapes java.awt.geom package contain a class named GeneralPath.
GeneralPath() It constructs a new empty single precision GeneralPath object.It inherits several methods from Path2D class. The most commonly used methods are as follows -
public final void moveTo(double x, double y) Here coordinates of the path is specified in double data type. Parameters - x - the specified X coordinate y - the specified Y coordinate
public final void moveTo(float x, float y) Here coordinates of the path is specified in double data type. Parameters - x - the specified X coordinate y - the specified Y coordinate
public final void lineTo(double x, double y) Adds a point to the path by drawing a straight line from the current coordinates to the new specified coordinates specified in double precision. Parameters - x - the specified X coordinate y - the specified Y coordinate
public final void lineTo(float x, float y) Adds a point to the path by drawing a straight line from the current coordinates to the new specified coordinates specified in float precision. Parameters - x - the specified X coordinate y - the specified Y coordinate
public final void quadTo(double x1, double y1, double x2, double y2) Here parameters are specified by double data type. Parameters - x1 - the X coordinate of the quadratic control point y1 - the Y coordinate of the quadratic control point x2 - the X coordinate of the final end point y2 - the Y coordinate of the final end point
public final void quadTo(float x1, float y1, float x2, float y2) Here parameters are specified by float data type. Parameters - x1 - the X coordinate of the quadratic control point y1 - the Y coordinate of the quadratic control point x2 - the X coordinate of the final end point y2 - the Y coordinate of the final end point.
public final void curveTo(double x1, double y1, double x2, double y2, double x3, double y3) Here parameters are specified by double data type. Parameters - x1 - the X coordinate of the first Bézier control point y1 - the Y coordinate of the first Bézier control point x2 - the X coordinate of the second Bézier control point y2 - the Y coordinate of the second Bézier control point x3 - the X coordinate of the final end point y3 - the Y coordinate of the final end point
public final void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) Here parameters are specified by float data type. Parameters - x1 - the X coordinate of the first Bézier control point y1 - the Y coordinate of the first Bézier control point x2 - the X coordinate of the second Bézier control point y2 - the Y coordinate of the second Bézier control point x3 - the X coordinate of the final end point y3 - the Y coordinate of the final end point
import java.awt.*; import java.applet.*; import java.awt.geom.*; public class Test extends Applet { public void paint(Graphics g) { Graphics2D gd = (Graphics2D)g; GeneralPath p1 = new GeneralPath(); p1.moveTo(200,100); p1.curveTo(250, 200,180, 250,200,350); p1.curveTo(210, 230,300, 200,260,100); p1.lineTo(230, 130); p1.lineTo(200, 100); gd.setColor(Color.red); gd.fill(p1); } }
import java.awt.*; import java.applet.*; public class Test extends Applet { public void paint(Graphics g) { int width = getSize().width; int height = getSize().height; int cell_width = width/8; int cell_height = height/8; for(int i=0;i<8;i++) { for(int j=0; j<8;j++) { if((i+j)%2 == 0) { g.setColor(Color.white); } else { g.setColor(Color.black); } g.fillRect(j*cell_width, i*cell_height, cell_width, cell_height); } } } }
import java.awt.*; import java.applet.*; public class Test extends Applet { public void paint(Graphics g) { int no = 100; int width = getSize().width; int height = getSize().height; /* A snake ladder board contains total 100 cells(10 rows, 10 columns) so one cell with and height will be total_width/10 and total_height/10 respectivly*/ int cell_width = width/10; int cell_height = height/10; for(int i=0;i<10;i++) { int t = 0; for(int j=0; j<10;j++) { int val = (i+j)%5; switch(val) // for 5 different colors { case 0: g.setColor(Color.red); break; case 1: g.setColor(Color.green); break; case 2: g.setColor(Color.blue); break; case 3: g.setColor(Color.yellow); break; case 4: g.setColor(Color.white); break; } g.fillRect(j*cell_width, i*cell_height, cell_width, cell_height); g.setColor(Color.black); // cell number should be of black color. /* if an even row then cell number will be printed from left to right And if an odd row cell number will be printed right to left. We have concatenated a blank string with the cell number to convert that to string. */ if(i%2 == 0) { g.drawString(no - t+" ", j*cell_width, (i+1)*cell_height); } else { g.drawString((no - 9)+ t +" ", j*cell_width, (i+1)*cell_height); } t++; } no = no -10; } } }