Sponsored sites

Wednesday 17 September 2014

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

No comments:

Post a Comment