Line Styles : Line « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » LineScreenshots 
Line Styles
Line Styles

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */
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[] { 050100 }// X coordinates of our shape

  int[] ypoints = new int[] { 75075 }// 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.0f3.0f2.0f3.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 = (Graphics2Dg1;
    // 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(2040);

    // 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[j2, ypoints[j255);

      g.drawString(capNames[i]5105)// Label the cap style
      g.drawString(joinNames[i]5120)// Label the join style

      g.translate(1500)// 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);
  }
}

           
       
Related examples in the same category
1. Program to draw gridsProgram to draw grids
2. Xsplinefun displays colorful moving splines in a window
w___w__w___.___j_a___va___2__s_.___c_o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.