Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-23-2008, 01:05 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Java2D Line Styles
Code:
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Stroke; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.GeneralPath; import javax.swing.JFrame; import javax.swing.JPanel; /** A demonstration of Java2D line styles */ public class LineStyles extends JPanel{ public String getName() { return "LineStyles"; } public int getWidth() { return 450; } public int getHeight() { return 180; } int[] xpoints = new int[] { 0, 50, 100 }; // X coordinates of our shape int[] ypoints = new int[] { 75, 0, 75 }; // Y coordinates of our shape // Here are three different line styles we will demonstrate // They are thick lines with different cap and join styles Stroke[] linestyles = new Stroke[] { new BasicStroke(25.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL), new BasicStroke(25.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER), new BasicStroke(25.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND), }; // Another line style: a 2 pixel-wide dot-dashed line Stroke thindashed = new BasicStroke(2.0f, // line width /* cap style */BasicStroke.CAP_BUTT, /* join style, miter limit */BasicStroke.JOIN_BEVEL, 1.0f, /* the dash pattern */new float[] { 8.0f, 3.0f, 2.0f, 3.0f }, /* the dash phase */0.0f); /* on 8, off 3, on 2, off 3 */ // Labels to appear in the diagram, and the font to use to display them. Font font = new Font("Helvetica", Font.BOLD, 12); String[] capNames = new String[] { "CAP_BUTT", "CAP_SQUARE", "CAP_ROUND" }; String[] joinNames = new String[] { "JOIN_BEVEL", "JOIN_MITER", "JOIN_ROUND" }; /** This method draws the example figure */ public void paint(Graphics g1) { Graphics2D g = (Graphics2D) g1; // Use anti-aliasing to avoid "jaggies" in the lines g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Define the shape to draw GeneralPath shape = new GeneralPath(); shape.moveTo(xpoints[0], ypoints[0]); // start at point 0 shape.lineTo(xpoints[1], ypoints[1]); // draw a line to point 1 shape.lineTo(xpoints[2], ypoints[2]); // and then on to point 2 // Move the origin to the right and down, creating a margin g.translate(20, 40); // Now loop, drawing our shape with the three different line styles for (int i = 0; i < linestyles.length; i++) { g.setColor(Color.gray); // Draw a gray line g.setStroke(linestyles[i]); // Select the line style to use g.draw(shape); // Draw the shape g.setColor(Color.black); // Now use black g.setStroke(thindashed); // And the thin dashed line g.draw(shape); // And draw the shape again. // Highlight the location of the vertexes of the shape // This accentuates the cap and join styles we're demonstrating for (int j = 0; j < xpoints.length; j++) g.fillRect(xpoints[j] - 2, ypoints[j] - 2, 5, 5); g.drawString(capNames[i], 5, 105); // Label the cap style g.drawString(joinNames[i], 5, 120); // Label the join style g.translate(150, 0); // Move over to the right before looping again } } public static void main(String[] a){ JFrame f = new JFrame(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setContentPane(new LineStyles()); f.setSize(450,200); f.setVisible(true); } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A demonstration of Java2D transformations Java Tip java.awt 0 06-21-2008 10:50 PM
print line kazitula Java Applets 2 01-26-2008 04:05 PM
JOptionPane - message styles Java Tip Java Tips 0 12-17-2007 11:20 AM
line number kazitula New To Java 3 11-22-2007 12:27 AM
Reading in data from file line by line bluekswing New To Java 1 10-02-2007 02:19 AM


All times are GMT +3. The time now is 08:05 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org