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



Home Java Example Java Swing Graphics2D Different Line Styles

Related Tutorials


 
 

Share on Google+Share on Google+

Different Line Styles

Advertisement
To show different styles of line, we have used BasicStroke class. The class BasicStroke provides several line drawing attributes: the line width, the dash pattern, the end cap style for the line, and the join style for the line.

Different Line Styles

     

This section shows you different styles of Line.

To show different styles of line, we have used BasicStroke class. The class BasicStroke provides several line drawing attributes: the line width, the dash pattern, the end cap style for the line, and the join style for the line. 

We have defined three different line styles. These are the thick lines having different cap and join styles.

 

 

Following are the cap styles:

1) The BasicStroke.CAP_BUTT specifies that the line have no end cap.
2) The CAP_SQUARE specifies a rectangular end cap, and 
3) The CAP_ROUND draws end cap in a semicircular shape.

Following are the join styles:

1) BasicStroke.JOIN_BEVEL connects the outer corners of their wide outlines with a straight segment.
2) BasicStroke.JOIN_MITER extends the outer edges of line until they meet.
3) BasicStroke.JOIN_ROUND rounding off the corner of a line at a radius of half the line width.

The class GeneralPath defines the path for the different styles of line. The setStroke() method sets the settings of the strokes of line used when drawing shapes into the image. To control the stroke width, color, miter limit, dashing settings, end cap style and line join style, setStroke() method is used.

Here is the code of DifferentLineStyles.java

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

public class DifferentLineStyles extends JPanel{
  public String getName() {
  return "Lines";
  }
  int[] x = new int[] { 50100 }; 
  int[] y = new int[] { 75075 }; 

  Stroke[] stroke1 = new Stroke[] {
 new BasicStroke(20.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
 new BasicStroke(20.0f, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER),
 new BasicStroke(20.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND), };

 Stroke stroke2 = new BasicStroke(1.0f,BasicStroke.CAP_BUTT,BasicStroke.
   JOIN_BEVEL,
1.0fnew float[]{ 6.0f2.0f1.0f2.0f },0.0f);
 Font font = new Font("Book Antiqua", Font.BOLD, 15);
 String[] caps = new String[]{ "CAP_BUTT""CAP_SQUARE""CAP_ROUND" };
 String[] joins = new String[]{ "JOIN_BEVEL""JOIN_MITER","JOIN_ROUND" };
 public void paint(Graphics g){
 Graphics2D g2d = (Graphics2D) g;
 GeneralPath path = new GeneralPath();
 path.moveTo(x[0], y[0]); 
 path.lineTo(x[1], y[1]); 
 path.lineTo(x[2], y[2]); 
 g2d.translate(3050);

 for (int k = 0; k < stroke1.length; k++) {
 g2d.setColor(Color.red); 
 g2d.setStroke(stroke1[k]);
 g2d.draw(path); 
 g2d.setColor(Color.black); 
 g2d.setStroke(stroke2); 
 g2d.draw(path); 
 g2d.drawString(caps[k], 6100); 
 g2d.drawString(joins[k], 6110);
 g2d.translate(1500);
  }
  }
  public static void main(String[] args){
  JFrame frame = new JFrame("Different Line Styles");
  frame.setContentPane(new DifferentLineStyles());
  frame.setSize(450,210);
  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 16, 2008

Related Tutorials

Discuss: Different Line Styles  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
charity
September 7, 2011
computer.!!!

..now i know..thx for the information!!
DMCA.com