Draw Dashed Oval

To give the stylistic and impressive representation of the outline, we have used the class BasicStroke.

Draw Dashed Oval

To give the stylistic and impressive representation of the outline, we have used the class BasicStroke.

Draw Dashed Oval

Draw Dashed Oval

     

This section illustrates you how to draw a dashed oval.

To give the stylistic and impressive representation of the outline, we have used the class BasicStroke. This class has been given by the interface Stroke. The class BasicStroke defines some methods to give the impressive way of showing the outline. 

The BasicStroke.CAP_BUTT gives the dash segments. The BasicStroke.JOIN_BEVEL connects the outer corners of outlines with a straight segment. The class Ellipse2D is defined to provide the round figure which shows the outline.

The method setPaint(Color.gray) paints the outline with gray color. The method setStroke() sets the stroke settings for the Graphics2D context, when you draw the shape.

Here is the code of DrawDashedOval.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class DrawDashedOval extends JApplet {
  public void init() {
  setBackground(Color.white);
 }
 float fl[] = { 12.0f };
 BasicStroke basicStroke = new BasicStroke(2.0f,
 BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 12.0f,
    fl, 
0.0f);
  
 public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.setPaint(Color.gray);
  g2d.setStroke(basicStroke);
  g2d.draw(new Ellipse2D.Double(5050250250));
 }
 public static void main(String args[]) {
  JFrame frame = new JFrame("Show Dashed Oval");
  DrawDashedOval dashedOval= new  DrawDashedOval();
  frame.getContentPane().add("Center", dashedOval);
  dashedOval.init();
  frame.setSize(350350);
  frame.setVisible(true);
  }
}

Output will be displayed as:

Download Source Code