In the Following example DashedStrokeApplet shows how to Draw Dashed Stroke Polygon Applet Window Example using Graphics2D class and setPaint(),setStroke(),BasicStroke() method of Graphics class. The Syntax for drawPolygon(int[] xPoints, int[] yPoints, int numPoint); and The Syntax for BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) ; setStroke is an inteface defined in the java.awt package.
Here is the java code for the program DashedStrokeApplet:.
import java.applet.Applet;
import java.awt.*;
import java.awt.BasicStroke;
import java.awt.event.*;
public class DashedStrokeApplet extends Applet
{
public static void main(String[] args)
{
DashedStrokeApplet DashedStroke = new DashedStrokeApplet();
Frame StrokeApplet = new Frame("Draw Dashed Stroke Polygon Applet Window Example");
StrokeApplet.add(DashedStroke);
StrokeApplet.setSize(350, 250);
StrokeApplet.setVisible(true);
StrokeApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } });
}
public void paint (Graphics g)
{
g.setColor(Color.darkGray);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Stroke Polygon Applet Window Example", 10, 20);
g.setFont(new Font("Arial",Font.BOLD,10));
g.drawString("http://ecomputernotes.com", 200, 205);
Graphics2D Gr2D = (Graphics2D) g;
// Array of a dash pattern 40-pixel line, 10-pixel gap, 20-pixel line, 10-pixel gap
float [] d1 = {40, 10, 20, 10};
int[] X_polygon = {70, 210, 170, 210, 70, 110};
int[] Y_polygon = {50,50, 120, 190, 190, 120};
Gr2D.setPaint(Color.green);
//The Syntax for BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) ;
BasicStroke BasicS1 = new BasicStroke (9, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
Gr2D.setStroke (BasicS1);
Gr2D.drawPolygon (X_polygon, Y_polygon, 6);
Gr2D.setPaint(Color.blue);
BasicStroke BasicS2 = new BasicStroke (7, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0F, d1, 0.F);
Gr2D.setStroke (BasicS2);
//The Syntax for drawPolygon(int[] xPoints, int[] yPoints, int numPoint);
Gr2D.drawPolygon (X_polygon, Y_polygon, 6);
}
}