• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Java Two Dimenstional

The Java2D API

By Dinesh Thakur

The Java2D API provides advanced two-dimensional graphics capabilities for programmers who require detailed and complex graphical manipulations. The API includes features for processing line art, text and images in packages java.awt, java.awt.image, java.awt.color, java.awt.font. java.awt.geom, java.awt.print and java.awt.image.renderable.

Drawing with the Java2D API is accomplished by using an instance of class Graphics2D (package java.awt). Class Graphics2D is a sub-class of class Graphics. In fact, the actual object used to draw in every

paint method is a Graphics2D object, which can be passed to the paint() method and accessed using the super-class Graphics reference, that is, g. To access the Graphics2D capabilities, the Graphics reference passed to paint to a Graphics2D reference must be downcast. To access the additional capabilities of Graphics2D while working with the Graphics class, the Graphics reference (g) must be type-cast to a Graphics2D reference by passing paint Component into Graphics2D. The syntax of casting Graphics2D to Graphics is the following:

                                     Graphics2D g2d = (Graphics2D ) g;

TextAttribute Java Example

By Dinesh Thakur

The variable map is which allows you to assign design attributes of the object type source AttributedString. Through this variable may change any aspect of the font . TextAttribute example shown the code as follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.Shape;
import java.awt.font.*;
import java.text.*;
import javax.swing.*;
public class DrawTextAttributeExample extends Applet {
public static void main(String[] args) {
Frame DrawTextAttribute  = new Frame("Draw TextAttribute  Example");
DrawTextAttribute .setSize(350, 250);
Applet DrawTextAttributeExample = new DrawTextAttributeExample();
DrawTextAttribute .add(DrawTextAttributeExample);
DrawTextAttribute .setVisible(true);
DrawTextAttribute .addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("TextAttribute in Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                G2D.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
                G2D.setColor(Color.blue);
                String str = "Dinesh Thakur";
                AttributedString AttributedStr = new AttributedString (str);
                Font font = new Font("Arial", Font.ITALIC, 15);
                AttributedStr.addAttribute(TextAttribute.FONT,font,0,str.length()-2);
                Image image = (new ImageIcon("DineshThakur.jpg")).getImage();
                ImageGraphicAttribute ImageGA = new ImageGraphicAttribute (image,(int)CENTER_ALIGNMENT);
                AttributedStr.addAttribute(TextAttribute.CHAR_REPLACEMENT,ImageGA,1,2);
                AttributedStr.addAttribute(TextAttribute.CHAR_REPLACEMENT,ImageGA,8,9);
                font = new Font("Times", Font.ITALIC, 24);
                AttributedStr.addAttribute(TextAttribute.FONT,font,3,9);
                AttributedCharacterIterator AttributedCI = AttributedStr.getIterator();
                FontRenderContext FontRC = G2D.getFontRenderContext();
               TextLayout TextLay = new TextLayout(AttributedCI,FontRC);
               TextLay.draw(G2D, 80, 70);
  }
}

TextAttribute Java Example

Filling a clipping path with different objects Java Example

By Dinesh Thakur

This code can be seen as built a clipping path with the text area of the word ” Dinesh Thakur ” located in the middle of the window. In the drawn area can be seen as the image is painted behind the chain. As seen, the effect is very colorful and easy to perform.

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.Shape;
import java.awt.font.*;
public class DrawClippingPathExample extends Applet {
public static void main(String[] args) {
Frame DrawClippingPath  = new Frame("Draw ClippingPath  Example");
DrawClippingPath .setSize(350, 250);
Applet DrawClippingPathExample = new DrawClippingPathExample();
DrawClippingPath .add(DrawClippingPathExample);
DrawClippingPath .setVisible(true);
DrawClippingPath .addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Filling a Clipping Path in Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                int w = getSize().width;
                int h = getSize().height;
               FontRenderContext FontRC = G2D.getFontRenderContext();
               Font font = new Font("Times",Font.BOLD,w/15);
               String str = new String ("Dinesh Thakur");
               TextLayout TextLay = new TextLayout(str,font,FontRC);
               float sw = (float)TextLay.getBounds().getWidth();
               AffineTransform AffineTrans = new AffineTransform();
               AffineTrans.setToTranslation(w/2-sw/2,h/1.5);
               Shape shp = TextLay.getOutline(AffineTrans);
               G2D.setClip(shp);
               G2D.setColor(Color.red);
               G2D.fill(shp.getBounds2D());
               G2D.setColor(Color.white);
               G2D.setStroke(new BasicStroke(2.0f));
               for ( double j = shp.getBounds().getY();
               j<shp.getBounds2D().getY()+shp.getBounds2D().getHeight();
               j=j+4) {
              Line2D L2D = new Line2D.Double(0.0,j,w,j);
              G2D.draw(L2D);
              }
  }
}

Filling a clipping path with different objects

Text Effect in Java Example

By Dinesh Thakur

This code can be seen as built a clipping path with the content of the text area of the word “Java ” located in the middle of the window.In the drawn area can be seen as the image is painted behind the chain. As seen, the effect is very colorful and easy to perform.

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.Shape;
import javax.swing.*;
public class DrawTextEffectExample extends Applet {
public static void main(String[] args) {
Frame DrawTextEffect  = new Frame("Draw TextEffect  Example");
DrawTextEffect .setSize(350, 250);
Applet DrawTextEffectExample = new DrawTextEffectExample();
DrawTextEffect .add(DrawTextEffectExample);
DrawTextEffect .setVisible(true);
DrawTextEffect .addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Text Effect in Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                int w = getSize().width;
                int h = getSize().height;
                Image image = (new ImageIcon("Canada.jpg")).getImage();
                FontRenderContext FontRC = G2D.getFontRenderContext();
                Font font = new Font("Times",Font.BOLD,100);
                TextLayout TextLay = new TextLayout("Java",font,FontRC);
                float sw = (float)TextLay.getBounds().getWidth();
                AffineTransform AffineTran = new AffineTransform();
                AffineTran.setToTranslation(w/2-sw/2,h*5/8);
                Shape s = TextLay.getOutline(AffineTran);
               G2D.setClip(s);
               G2D.drawImage(image,50,60,this);
               G2D.setColor(Color.green);
               G2D.draw(s);
  }
}

Text Effect in Java Example

How to draw text on an image Java Example

By Dinesh Thakur

With this code we will study how text is drawn on a any image.

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.Shape;
import javax.swing.*;
public class DrawTextonImageExample extends Applet {
public static void main(String[] args) {
Frame DrawTextonImage = new Frame("Draw TextonImage Example");
DrawTextonImage.setSize(350, 250);
Applet DrawTextonImageExample = new DrawTextonImageExample();
DrawTextonImage.add(DrawTextonImageExample);
DrawTextonImage.setVisible(true);
DrawTextonImage.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw text on an Image Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                int w = getSize().width;
                int h = getSize().height;
                Image image = (new ImageIcon("DineshThakur.jpg")).getImage();
                G2D.drawImage(image,140,80,this);
                FontRenderContext FontRC = G2D.getFontRenderContext();
                Font font = new Font("Times",Font.BOLD,w/25);
                String str = new String ("Dinesh Thakur");
                TextLayout TextL = new TextLayout(str,font,FontRC);
                float sw = (float)TextL.getBounds().getWidth();
                AffineTransform AffineTran = new AffineTransform();
                AffineTran.setToTranslation(w/2-sw/2,h*3/8);
                G2D.setColor(Color.green);
               Shape shape = TextL.getOutline(AffineTran);
               G2D.draw(shape);
               TextL.draw(G2D,w/2-sw/2,h*5/8);
  }
}

How to draw text on an image

TextLayout Java Example

By Dinesh Thakur

A much more powerful mechanism for drawing text is provided  the TextLayout class.This class not only allows you to draw text in a similar way  to as was done be fore but also provides a number of methods that  let youk now what is specific to draw features.

The following tables hows the methods involved in this section:

Method

Description

getFontRenderContext()

Returns information about therendering context.

TextLayout(String s, Font f,

FontRenderContext frc)

Creates anobject fromText Layouta chain, a source andtextrendering context

Font(String s, intstyle, int size)

Createa fontfrom aname,andplot stylesize.

draw(Graphics2D, float x,

float y)

Drawtext onGraphics2Dandindicates thedisplacementto the axisofcoordinates.

[Read more…] about TextLayout Java Example

Set operations Add Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a Addition of 2 circles. An example show the code below:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawaddExample extends Applet {
public static void main(String[] args) {
Frame Drawadd = new Frame("Draw add Example");
Drawadd.setSize(350, 250);
Applet DrawaddExample = new DrawaddExample();
Drawadd.add(DrawaddExample);
Drawadd.setVisible(true);
Drawadd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw add Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                GradientPaint GPaint = new GradientPaint(50.0f, 50.0f, Color.red,200.0f, 50.0f, Color.green);
                G2D.setPaint(GPaint);
                Ellipse2D E2D = new Ellipse2D.Double(50.0, 50.0, 80.0, 80.0);
                Ellipse2D E2D2 = new Ellipse2D.Double(100.0, 50.0, 80.0, 80.0);
                Area area1 = new Area(E2D);
                Area area2 = new Area(E2D2);
                area1.add(area2);
                G2D.fill(area1);
  }
}

Set operations Add Java Example

Set operations Intersection Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a Intersection of 2 circles . Intersection examples how the code as follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawintersectExample extends Applet {
public static void main(String[] args) {
Frame Drawintersect = new Frame("Draw intersect Example");
Drawintersect.setSize(350, 250);
Applet DrawintersectExample = new DrawintersectExample();
Drawintersect.add(DrawintersectExample);
Drawintersect.setVisible(true);
Drawintersect.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw intersect Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                GradientPaint GPaint = new GradientPaint(50.0f, 50.0f, Color.red,200.0f, 50.0f, Color.green);
                G2D.setPaint(GPaint);
                Ellipse2D E2D = new Ellipse2D.Double(50.0, 50.0, 80.0, 80.0);
                Ellipse2D E2D2 = new Ellipse2D.Double(100.0, 50.0, 80.0, 80.0);
                Area area1 = new Area(E2D);
                Area area2 = new Area(E2D2);
                area1.intersect(area2);
                G2D.fill(area1);
  }
}

Set operations Intersection Java Example

Set operations Subtraction Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a Subtraction of 2 circles . Subtraction examples how the code as follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawsubtractExample extends Applet {
public static void main(String[] args) {
Frame Drawsubtract = new Frame("Draw subtract Example");
Drawsubtract.setSize(350, 250);
Applet DrawsubtractExample = new DrawsubtractExample();
Drawsubtract.add(DrawsubtractExample);
Drawsubtract.setVisible(true);
Drawsubtract.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw subtract Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                GradientPaint GPaint = new GradientPaint(50.0f, 50.0f, Color.red,200.0f, 50.0f, Color.green);
                G2D.setPaint(GPaint);
                Ellipse2D E2D = new Ellipse2D.Double(50.0, 50.0, 80.0, 80.0);
                Ellipse2D E2D2 = new Ellipse2D.Double(100.0, 50.0, 80.0, 80.0);
                Area area1 = new Area(E2D);
                Area area2 = new Area(E2D2);
                area1.subtract(area2);
                G2D.fill(area1);
  }
}

Set operations Subtraction Java Example

Set operations exclusiveOr Java Example

By Dinesh Thakur

In this example we show how the Set operations going to make a union of 2 circles. Union exclusive (XOR ) examples how the code as follows: [Read more…] about Set operations exclusiveOr Java Example

GeneralPath Java Example

By Dinesh Thakur

Another way is to build figures using GeneralPath class, which but also inherits from Shapeisan interface, but a class. This class allows  define a shape by a sequence oflines that can be established through  functions in the following table:

Method

Description

GeneralPath

(constructor)

Representsa geometric figureconstructed fromstraight linesandquadratic andcubic curves

moveTo

Movethe pen tipto a particularpoint

curveTo

a cubic curvefrom a starting pointtoa final one

lineTo

definesa straight linefrom start pointtoend you

quadTo

representsa quadratic curvefrom a starting pointtoa final one

closePath

Closes pathof figurebya straight linejoiningthecurrent pen positiontothe start of thedrawing.

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawGeneralPathExample extends Applet {
public static void main(String[] args) {
Frame DrawGeneralPath = new Frame("Draw GeneralPath Example");
DrawGeneralPath.setSize(350, 250);
Applet DrawGeneralPathExample = new DrawGeneralPathExample();
DrawGeneralPath.add(DrawGeneralPathExample);
DrawGeneralPath.setVisible(true);
DrawGeneralPath.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw GeneralPath Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.green);
                GeneralPath GPath = new GeneralPath();
                GPath.moveTo(50.0f,50.0f);
                GPath.lineTo(100.0f,50.0f);
                GPath.curveTo(120.0f,30.0f,120.0f,100.0f,180.0f,125.0f);
                GPath.lineTo(50.0f,150.0f);
                GPath.closePath();
                G2D.fill(GPath);
                G2D.setColor(Color.blue);
                G2D.draw(GPath);
  }
}

GeneralPath Java Example

Point2D Java Example

By Dinesh Thakur

Point2D class does not draw anything; it is the representation of points Java2D. But yes you can draw shapes from points. Following is at able that lists some of the Constructors and methods of Point2D class:

Constructors and Methods

Description

Point2D.Double ()

Creates a Point2D at (0, 0).

Point2D.Double (double X, double Y)

Creates a Point2D at (X, Y).

Point2D.Float ()

Creates a Point2D at (0, 0).

Point2D.Float (float X, float Y)

Creates a Point2D at (X, Y).

getX ()

Returns the horizontal coordinate of the point.

getY ()

Returns the vertical coordinate of the point.

Point2D.Float (float X, float Y)

Sets the location of the Point as (X, Y).

Drawingan Point2D exampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawPoint2DExample extends Applet {
public static void main(String[] args) {
Frame DrawPoint2D = new Frame("Draw Point2D Example");
DrawPoint2D.setSize(350, 250);
Applet DrawPoint2DExample = new DrawPoint2DExample();
DrawPoint2D.add(DrawPoint2DExample);
DrawPoint2D.setVisible(true);
DrawPoint2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw Point2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setStroke(new BasicStroke(3.0f));
                Point2D P2D = new Point2D.Float(23.5f, 48.9f);
                Point2D P2D2 = new Point2D.Float(158.0f, 173.0f);
                Line2D L2D = new Line2D.Float(P2D, P2D2);
                G2D.draw(L2D);
  }
}

Point2D Java Example

CubicCurve2D Java Example

By Dinesh Thakur

CubicCurve2D which also shows the segments joining  the ends with the points control and the set together. An example of use appears in the following code, which also have highlighted the four reference points. The result can be seen in CubicCurve2D Java Example.

Following is a table that lists some of the Constructors and methods of CubicCurve2D class:

Constructors and Methods

Description

CubicCurve2D.Double ()

Creates a CubicCurve2D from (0, 0) to (0, 0), with control point (0,0)

CubicCurve2D.Double (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Creates a CubicCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

CubicCurve2D.Float ()

Creates a CubicCurve2D from (0, 0) to (0, 0), with control points (0,0).

CubicCurve2D.Float (float X1, float Y1, float CX1, float CY1, float X2, float Y2)

Creates a CubicCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

boolean intersects (double X, double Y, double Width, double Height)

Returns true if the rectangle described by (X, Y, Width, Height) intersects the CubicCurve

void setCurve (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Sets the ends of the CubicCurve as (X1, Y1) and (X2, Y2).

Drawingan CubicCurve exampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawCubicCurve2DExample extends Applet {
public static void main(String[] args) {
Frame DrawCubicCurve2D = new Frame("Draw CubicCurve2D Example");
DrawCubicCurve2D.setSize(350, 250);
Applet DrawCubicCurve2DExample = new DrawCubicCurve2DExample();
DrawCubicCurve2D.add(DrawCubicCurve2DExample);
DrawCubicCurve2D.setVisible(true);
DrawCubicCurve2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw CubicCurve2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.blue);
                G2D.setStroke(new BasicStroke(3.0f));
                CubicCurve2D CC2D = new CubicCurve2D.Float(40.0f, 60.0f, 60.0f, 120.0f, 140.0f, 130.0f, 150.0f, 210.0f);
                G2D.draw(CC2D);
                G2D.setColor(Color.blue);
                G2D.draw(new Rectangle2D.Float(40.0f, 60.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(60.0f, 120.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(140.0f, 130.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(150.0f, 210.0f, 1.0f, 1.0f));
  }
}

CubicCurve2D Java Example

QuadCurve2D Java Example

By Dinesh Thakur

The QuadCurve2D class lets build a curved segment based on mathematical equations. The curve generated is also called curve quadratic Bezier and based on a very simple idea is to establish two points that define the ends of a curved segment and a third point, called checkpoint that allows “stretching” more or less the curvature of the segment.

Following is at able that lists some of the Constructors and methods of QuadCurve2D class:

Constructors and Methods

Description

QuadCurve2D.Double ()

Creates a QuadCurve2D from (0, 0) to (0, 0), with control point (0,0)

QuadCurve2D.Double (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Creates a QuadCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

QuadCurve2D.Float ()

Creates a QuadCurve2D from (0, 0) to (0, 0), with control points (0,0).

QuadCurve2D.Float (float X1, float Y1, float CX1, float CY1, float X2, float Y2)

Creates a QuadCurve2D from (X1, Y1) to (X2, Y2), with control point (CX1, CY1).

boolean intersects (double X, double Y, double Width, double Height)

Returns true iff the rectangle described by (X, Y, Width, Height) intersects the QuadCurve

void setCurve (double X1, double Y1, double CX1, double CY1, double X2, double Y2)

Sets the ends of the QuadCurve as (X1, Y1) and (X2, Y2).

Drawingan Curve exampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawQuadCurve2DExample extends Applet {
public static void main(String[] args) {
Frame DrawQuadCurve2D = new Frame("Draw QuadCurve2D Example");
DrawQuadCurve2D.setSize(350, 250);
Applet DrawQuadCurve2DExample = new DrawQuadCurve2DExample();
DrawQuadCurve2D.add(DrawQuadCurve2DExample);
DrawQuadCurve2D.setVisible(true);
DrawQuadCurve2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw QuadCurve2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.green);
                G2D.setStroke(new BasicStroke(3.0f));
                QuadCurve2D QC2D = new QuadCurve2D.Float(40.0f, 70.0f, 40.0f, 170.0f, 190.0f, 220.0f);
                G2D.draw(QC2D);
                G2D.setColor(Color.red);
                G2D.draw(new Rectangle2D.Float(40.0f, 70.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(40.0f, 170.0f, 1.0f, 1.0f));
                G2D.draw(new Rectangle2D.Float(190.0f, 220.0f, 1.0f, 1.0f));
  }
}

QuadCurve2D Java Example

Arc2D Java Example

By Dinesh Thakur

The class allows Arc2D draw an arc of an ellipse or a circle. For this, first the rectangle that contains the ellipse whose arc you want to draw is defined ; then assuming that the center of the rectangle sets the point (0,0) of a Cartesian coordinate axis , the angles start and end are specified the arc in degrees . Finally , indicate whether you want to close the bow joining ends or not. This closure can be of three types :

• Arc2D.OPEN : the arc is open .
• Arc2D.CHORD : the ends of the arch are connected by a line segment .
• Arc2D.PIE : Each end of the arc joined by a segment with point ( 0 , 0 ) of the coordinate axis .
The following code draws a rectangle reference . Then, and within a rectangle equal to the reference , an arc that starts in the draw angle 0 ° and ends at 135 . Finally the arc closes like a piece of a pie chart ( Arc2D.PIE ) .

Following isatablethat listssome of the Constructors and methods of Ellipse2Dclass:

Constructors and Methods

Description

Arc2D.Double ()

Creates an OPEN Arc2D with a size of (0, 0) at a location of (0, 0), and angle (0, 0). 

Arc2D.Double (int Type)

Creates an Arc2D of the specified Type with a size of (0, 0) at a location of (0, 0), and angle (0, 0). 

Arc2D.Double (double X, double Y, double Width, double Height, double Theta, double Delta, int Type)

Creates an Arc2D of the specified Type with a size of (Width, Height) at location (X, Y), and angle (Theta, Delta). 

Arc2D.Float ()

Creates an OPEN Arc2D with a size of (0, 0) at a location of (0, 0), and angle (0, 0). 

Arc2D.Float (int Type)

Creates an Arc2D of the specified Type with a size of (0, 0) at a location of (0, 0), and angle (0, 0)

Arc2D.Float (float X, float Y, float Width, float Height, float Theta, float Delta, int Type)

Creates an Arc2D of the specified Type with a size of (Width, Height) at location (X, Y), and angle (Theta, Delta). 

contains (double X, double Y)

Returns true if the point (X, Y) is within the Arc. 

contains (double X, double Y, double Width, double Height)

Returns true if the Arc (X, Y, Width, Height) is entirely within the Arc.

intersects (double X, double Y, double Width, double Height)

Returns true if the Arc (X, Y, Width, Height) intersects the Arc.

setArc (double X, double Y, double Width, double Height, double Theta, double Delta, int Type)

Sets the location, bounding size, arc size, and type of the arc. 

Drawinganarcexampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawArc2DExample extends Applet {
public static void main(String[] args) {
Frame DrawArc2D = new Frame("Draw Arc2D Example");
DrawArc2D.setSize(350, 250);
Applet DrawArc2DExample = new DrawArc2DExample();
DrawArc2D.add(DrawArc2DExample);
DrawArc2D.setVisible(true);
DrawArc2D.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw Arc2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.blue);
                G2D.setStroke(new BasicStroke(1.0f));
                Rectangle2D Rect2D = new Rectangle2D.Float(100.0f, 75.0f, 50.0f, 100.0f);
                G2D.draw(Rect2D);
                G2D.setColor(Color.green);
                G2D.setStroke(new BasicStroke(3.0f));
                Arc2D Ar2D = new Arc2D.Float(100.0f, 75.0f, 50.0f, 100.0f, 0.0f, 135.0f, Arc2D.PIE);
               G2D.draw(Ar2D);
  }
}

Arc2D Java Example

Ellipse2D Java Example

By Dinesh Thakur

The ellipses or ovals are drawn by Ellipse2D class.This requires you defined to be a rectangle enclosing the boundaries between the ellipse.There the concept of a circle, so that it must be obtained by an ellipse enclosed in a square.

In Ellipse2D Java Example we are using package java.awt.geom.*. Following is a table that lists some of the Constructors and methods of Ellipse2D class:

 

Constructors and Methods

Description

Ellipse2D.Double ()

Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0).

Ellipse2D.Double (double X, double Y, double W, double H)

Creates a Ellipse2D with a size of (W, H) at location (X, Y).

Ellipse2D.Float ()

Creates a Ellipse2D with a size of (0, 0) at a location of (0, 0).

Ellipse2D.Float (float X, float Y, float W, float H)

Creates a Ellipse2D with a size of (W, H) at location (X, Y).

contains (double X, double Y)

Returns true if the point (X, Y) is within the Ellipse.

contains (double X, double Y, double W, double H)

Returns true if the Ellipse (X, Y, W, H) is entirely within the Ellipse.

intersects (double X, double Y, double W, double H)

Returns true if the Ellipse (X, Y, W, H) intersects the Ellipse.

setFrame (double X, double Y, double W, double H)

Sets the location and size of the Ellipse.

Drawinganellipseexampleshownthecodeas follows:

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Draw2DEllipseExample extends Applet {
public static void main(String[] args) {
Frame Draw2DEllipse = new Frame("Draw 2DEllipse Example");
Draw2DEllipse.setSize(350, 250);
Applet Draw2DEllipseExample = new Draw2DEllipseExample();
Draw2DEllipse.add(Draw2DEllipseExample);
Draw2DEllipse.setVisible(true);
Draw2DEllipse.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw Ellipse2D Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.orange);
                G2D.setStroke(new BasicStroke(3.0f));
                Ellipse2D E2D = new Ellipse2D.Float(100.0f,75.0f,50.0f,100.0f);
                G2D.draw(E2D);
  }
}

Ellipse2D Java Example

Line2D Java Example

By Dinesh Thakur

Draw a straight line is one of the easiest things to do with Java2D. This is achieved through the Line2D class, whose constructor accepts four parameters, namely the coordinates of the start and end respectively.

In Line2D Java Example we are using package java.awt.geom.*. This class provides a line segment in (x, y) coordinate space. Following is at able that lists some of the Constructors and methods of Line2D class:

 

Constructors and Methods

Description

Line2D.Double ()

Creates a Line2D from (0, 0) to (0, 0).

Line2D.Double (double X1, double Y1, double X2, double Y2)

Creates a Line2D from (X1, Y1) to (X2, Y2).

Line2D.Float ()

Creates a Line2D from (0, 0) to (0, 0).

Line2D.Float (float X1, float Y1, float X2, float Y2)

Creates a Line2D from (X1, Y1) to (X2, Y2).

intersects (double X, double Y, double W, double H)

Returns true if the rectangle described by (X, Y, Width, Height) intersects the Line.

intersectsLine (double X1, double Y1, double X2, double Y2)

Returns true if the line from (X1, Y1) to (X2, Y2) intersects the Line.

intersectsLine (Line2D L)

Returns true if the line L intersects the Line.

setLine (double X1, double Y1, double X2, double Y2)

Sets the ends of the line as (X1, Y1) and (X2, Y2).

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Draw2DLineExample extends Applet {
public static void main(String[] args) {
Frame Draw2DLine = new Frame("Draw Line2D Example");
Draw2DLine.setSize(350, 250);
Applet Draw2DLineExample = new Draw2DLineExample();
Draw2DLine.add(Draw2DLineExample);
Draw2DLine.setVisible(true);
Draw2DLine.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw 2D Line Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.pink);
                G2D.setStroke(new BasicStroke(3.0f));
                Line2D L2D = new Line2D.Float(50.0f, 50.0f, 200.0f, 200.0f);
                G2D.draw(L2D);
  }
}

Line2D Java Example

Rectangle2D Java Example

By Dinesh Thakur

The class that is used to draw rectangles and squares is the Rectangle2D. The constructor specifies the first two parameters of the corner position upper left relative to the coordinate system of the window.

These four parameters can be specified using float or double values, using for this builders Rectangle2D. Float() and Rectangle2D. Double() respectively.This possibility to construct geometric figures using coordinates in float or double is a constant that is repeated in all the shapes. A rectangle can use the fill method of the java.awt.Graphics2D class. Following is at able that lists some of the Constructors and methods of Rectangle2D class:

Constructors and Methods

Description

Rectangle2D.Double ()

Creates a Rectangle2D with a size of (0, 0) at a location of (0, 0).

Rectangle2D.Double (double X, double Y, double W, double H)

Creates a Rectangle2D with a size of (W, H) at location (X, Y)

Rectangle2D.Float ()

Creates a Rectangle2D with a size of (0, 0) at a location of (0, 0).

Rectangle2D.Float (float X, float Y, float W, float H)

Creates a Rectangle2D with a size of (W, H) at location (X, Y)

contains (double X, double Y)

Returns true if the point (X, Y) is within the rectangle.

contains (double X, double Y, double W, double H)

Returns true if the rectangle (X, Y, W, H) is entirely within the rectangle.

intersects (double X, double Y, double W, double H)

Returns true if the rectangle (X, Y, W, H) intersects the rectangle.

intersectsLine (double X1, double Y1, double X2, double Y2)

setRect (double X, double Y, double W, double H)

Returns true if the line from (X1, Y1) to (X2, Y2) intersects the rectangle.

Sets the location and size of the rectangle.

 

import java.applet.Applet; 
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
public class DrawRectangle2DExample extends Applet {
public static void main(String[] args) {
Frame Draw2DRect = new Frame("Draw 2D Rectangles Example");
Draw2DRect.setSize(350, 250);
Applet DrawRectangle2DExample = new DrawRectangle2DExample();
Draw2DRect.add(DrawRectangle2DExample);
Draw2DRect.setVisible(true);
Draw2DRect.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw 2D Rectangles Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                super.paint(g);
                Graphics2D G2D = (Graphics2D)g;
                G2D.setColor(Color.blue);
                G2D.setStroke(new BasicStroke(3.0f));
                Rectangle2D Rect2D = new Rectangle2D.Float(100.0f, 75.0f, 50.0f, 100.0f);
                G2D.draw(Rect2D);
  }
}
 

Rectangle2D Java Example

Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW