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