Stroke Test : Stroke « 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. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
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
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » StrokeScreenshots 
Stroke Test
Stroke Test
     

/**
 @version 1.00 1999-09-11
 @author Cay Horstmann
 */

import java.awt.BasicStroke;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class StrokeTest {
  public static void main(String[] args) {
    JFrame frame = new StrokeTestFrame();
    frame.show();
  }
}

class StrokeTestFrame extends JFrame implements ActionListener {
  public StrokeTestFrame() {
    setTitle("StrokeTest");
    setSize(400400);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = getContentPane();
    canvas = new StrokePanel();
    contentPane.add(canvas, "Center");

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(33));
    ButtonGroup group1 = new ButtonGroup();

    buttCapButton = new JRadioButton("Butt Cap"true);
    buttonPanel.add(buttCapButton);
    group1.add(buttCapButton);
    buttCapButton.addActionListener(this);

    roundCapButton = new JRadioButton("Round Cap"false);
    buttonPanel.add(roundCapButton);
    group1.add(roundCapButton);
    roundCapButton.addActionListener(this);

    squareCapButton = new JRadioButton("Square Cap"false);
    buttonPanel.add(squareCapButton);
    group1.add(squareCapButton);
    squareCapButton.addActionListener(this);

    ButtonGroup group2 = new ButtonGroup();

    bevelJoinButton = new JRadioButton("Bevel Join"true);
    buttonPanel.add(bevelJoinButton);
    group2.add(bevelJoinButton);
    bevelJoinButton.addActionListener(this);

    miterJoinButton = new JRadioButton("Miter Join"false);
    buttonPanel.add(miterJoinButton);
    group2.add(miterJoinButton);
    miterJoinButton.addActionListener(this);

    roundJoinButton = new JRadioButton("Round Join"false);
    buttonPanel.add(roundJoinButton);
    group2.add(roundJoinButton);
    roundJoinButton.addActionListener(this);

    ButtonGroup group3 = new ButtonGroup();

    solidLineButton = new JRadioButton("Solid Line"true);
    buttonPanel.add(solidLineButton);
    group3.add(solidLineButton);
    solidLineButton.addActionListener(this);

    dashedLineButton = new JRadioButton("Dashed Line"false);
    buttonPanel.add(dashedLineButton);
    group3.add(dashedLineButton);
    dashedLineButton.addActionListener(this);

    contentPane.add(buttonPanel, "North");
  }

  public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == buttCapButton)
      canvas.setCap(BasicStroke.CAP_BUTT);
    else if (source == roundCapButton)
      canvas.setCap(BasicStroke.CAP_ROUND);
    else if (source == squareCapButton)
      canvas.setCap(BasicStroke.CAP_SQUARE);
    else if (source == bevelJoinButton)
      canvas.setJoin(BasicStroke.JOIN_BEVEL);
    else if (source == miterJoinButton)
      canvas.setJoin(BasicStroke.JOIN_MITER);
    else if (source == roundJoinButton)
      canvas.setJoin(BasicStroke.JOIN_ROUND);
    else if (source == solidLineButton)
      canvas.setDash(false);
    else if (source == dashedLineButton)
      canvas.setDash(true);
  }

  private StrokePanel canvas;

  private JRadioButton buttCapButton;

  private JRadioButton roundCapButton;

  private JRadioButton squareCapButton;

  private JRadioButton bevelJoinButton;

  private JRadioButton miterJoinButton;

  private JRadioButton roundJoinButton;

  private JRadioButton solidLineButton;

  private JRadioButton dashedLineButton;
}

class StrokePanel extends JPanel implements MouseListener, MouseMotionListener {
  public StrokePanel() {
    addMouseListener(this);
    addMouseMotionListener(this);
    points = new Point2D[3];
    points[0new Point2D.Double(200100);
    points[1new Point2D.Double(100200);
    points[2new Point2D.Double(200200);
    current = -1;
    width = 10.0F;
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2Dg;
    GeneralPath path = new GeneralPath();
    path.moveTo((floatpoints[0].getX()(floatpoints[0].getY());
    for (int i = 1; i < points.length; i++)
      path.lineTo((floatpoints[i].getX()(floatpoints[i].getY());
    BasicStroke stroke;
    if (dash) {
      float miterLimit = 10.0F;
      float[] dashPattern = 10F10F10F10F10F10F30F10F,
          30F10F30F10F10F10F10F10F10F30F };
      float dashPhase = 0;
      stroke = new BasicStroke(width, cap, join, miterLimit, dashPattern,
          dashPhase);
    else
      stroke = new BasicStroke(width, cap, join);
    g2.setStroke(stroke);
    g2.draw(path);
  }

  public void setJoin(int j) {
    join = j;
    repaint();
  }

  public void setCap(int c) {
    cap = c;
    repaint();
  }

  public void setDash(boolean d) {
    dash = d;
    repaint();
  }

  public void mousePressed(MouseEvent event) {
    Point p = event.getPoint();
    for (int i = 0; i < points.length; i++) {
      double x = points[i].getX() - SIZE / 2;
      double y = points[i].getY() - SIZE / 2;
      Rectangle2D r = new Rectangle2D.Double(x, y, SIZE, SIZE);
      if (r.contains(p)) {
        current = i;
        return;
      }
    }
  }

  public void mouseReleased(MouseEvent event) {
    current = -1;
  }

  public void mouseEntered(MouseEvent event) {
  }

  public void mouseExited(MouseEvent event) {
  }

  public void mouseClicked(MouseEvent event) {
  }

  public void mouseMoved(MouseEvent event) {
  }

  public void mouseDragged(MouseEvent event) {
    if (current == -1)
      return;
    points[current= event.getPoint();
    repaint();
  }

  private Point2D[] points;

  private static int SIZE = 10;

  private int current;

  private float width;

  private int cap;

  private int join;

  private boolean dash;
}

           
         
    
    
    
    
  
Related examples in the same category
1. Dashed rectangleDashed rectangle
2. A dashed stroke
3. Stroking or Filling with a Texture
4. Basic strokeBasic stroke
5. Thick stroke demoThick stroke demo
6. Dashed strokeDashed stroke
7. Stroke with iron effectStroke with iron effect
8. Smokey effectSmokey effect
9. Custom StrokesCustom Strokes
10. Changing the Thickness of the Stroking Pen
11. Tries to deduct the stroke-type from the given stroke object.
12. Tries to extract the stroke-width from the given stroke object.
13. A component for choosing a stroke from a list of available strokes.
14. Cancel the effects of the zoom on a particular Stroke
15. Serialises a Stroke object
w___w_w_.ja___v_a_2___s.___c__o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.