Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Graphics2D Draw Dashed Oval

Related Tutorials


 
 

Share on Google+Share on Google+

Draw Dashed Oval

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

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 7, 2008

Related Tutorials

Discuss: Draw Dashed Oval   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com