Academic Java    Java Tutorial  >  Graphics  >  GeneralPath

GeneralPath Example

Java GeneralPath EXAMPLE output This applet uses the GeneralPath class.

This class represents a geometric path constructed from straight lines, and curves. It can also contain multiple subpaths.

It has instance methods such as lineTo() and quadTo() that constructs a path from the current point to a new point using a straight line or quadratic curve, as appropriate.

The applet uses GeneralPath to define a star.
// GeneralPath Example
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class GeneralPathExample extends JApplet {

   @Override public void paint( Graphics g ) {
      super.paint(g);

      Graphics2D g2 = ( Graphics2D ) g;

      int[] x = {55, 67, 109, 73, 83, 55, 27, 37, 1, 43};
      int[] y = {0, 36, 36, 54, 96, 72, 96, 54, 36, 36};

      GeneralPath star = new GeneralPath();
      star.moveTo(x[0],y[0]);
      for (int i=1; i<x.length; ++i) star.lineTo(x[i], y[i]);
      star.closePath();

      g2.setColor(Color.pink);
      g2.fill(star);
      g2.setColor(Color.blue);
      g2.draw(star);

   }
}