Shape Test : Shape « 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 » ShapeScreenshots 
Shape Test
Shape Test
     
/**
 @version 1.00 1999-09-11
 @author Cay Horstmann
 */

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
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.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Random;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

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

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

    Container contentPane = getContentPane();

    panel = new ShapePanel();
    contentPane.add(panel, "Center");
    comboBox = new JComboBox();
    comboBox.addItem(new LineMaker());
    comboBox.addItem(new RectangleMaker());
    comboBox.addItem(new RoundRectangleMaker());
    comboBox.addItem(new EllipseMaker());
    comboBox.addItem(new ArcMaker());
    comboBox.addItem(new PolygonMaker());
    comboBox.addItem(new QuadCurveMaker());
    comboBox.addItem(new CubicCurveMaker());
    comboBox.addActionListener(this);
    contentPane.add(comboBox, "North");
  }

  public void actionPerformed(ActionEvent event) {
    ShapeMaker shapeMaker = (ShapeMakercomboBox.getSelectedItem();
    panel.setShapeMaker(shapeMaker);
  }

  private JComboBox comboBox;

  private ShapePanel panel;
}

class ShapePanel extends JPanel implements MouseListener, MouseMotionListener {
  public ShapePanel() {
    addMouseListener(this);
    addMouseMotionListener(this);
    current = -1;
  }

  public void setShapeMaker(ShapeMaker aShapeMaker) {
    shapeMaker = aShapeMaker;
    int n = shapeMaker.getPointCount();
    points = new Point2D[n];
    for (int i = 0; i < n; i++) {
      double x = generator.nextDouble() * getWidth();
      double y = generator.nextDouble() * getHeight();
      points[inew Point2D.Double(x, y);
    }
    repaint();
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (points == null)
      return;
    Graphics2D g2 = (Graphics2Dg;
    for (int i = 0; i < points.length; i++) {
      double x = points[i].getX() - SIZE / 2;
      double y = points[i].getY() - SIZE / 2;
      g2.fill(new Rectangle2D.Double(x, y, SIZE, SIZE));
    }

    g2.draw(shapeMaker.makeShape(points));
  }

  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 Random generator = new Random();

  private static int SIZE = 10;

  private int current;

  private ShapeMaker shapeMaker;
}

abstract class ShapeMaker {
  public ShapeMaker(int aPointCount) {
    pointCount = aPointCount;
  }

  public int getPointCount() {
    return pointCount;
  }

  public abstract Shape makeShape(Point2D[] p);

  public String toString() {
    return getClass().getName();
  }

  private int pointCount;
}

class LineMaker extends ShapeMaker {
  public LineMaker() {
    super(2);
  }

  public Shape makeShape(Point2D[] p) {
    return new Line2D.Double(p[0], p[1]);
  }
}

class RectangleMaker extends ShapeMaker {
  public RectangleMaker() {
    super(2);
  }

  public Shape makeShape(Point2D[] p) {
    Rectangle2D s = new Rectangle2D.Double();
    s.setFrameFromDiagonal(p[0], p[1]);
    return s;
  }
}

class RoundRectangleMaker extends ShapeMaker {
  public RoundRectangleMaker() {
    super(2);
  }

  public Shape makeShape(Point2D[] p) {
    RoundRectangle2D s = new RoundRectangle2D.Double(00002020);
    s.setFrameFromDiagonal(p[0], p[1]);
    return s;
  }
}

class EllipseMaker extends ShapeMaker {
  public EllipseMaker() {
    super(2);
  }

  public Shape makeShape(Point2D[] p) {
    Ellipse2D s = new Ellipse2D.Double();
    s.setFrameFromDiagonal(p[0], p[1]);
    return s;
  }
}

class ArcMaker extends ShapeMaker {
  public ArcMaker() {
    super(4);
  }

  public Shape makeShape(Point2D[] p) {
    double centerX = (p[0].getX() + p[1].getX()) 2;
    double centerY = (p[0].getY() + p[1].getY()) 2;
    double width = Math.abs(p[1].getX() - p[0].getX());
    double height = Math.abs(p[1].getY() - p[0].getY());

    double distortedStartAngle = Math.toDegrees(Math.atan2(
        -(p[2].getY() - centerY* width, (p[2].getX() - centerX)
            * height));
    double distortedEndAngle = Math.toDegrees(Math.atan2(
        -(p[3].getY() - centerY* width, (p[3].getX() - centerX)
            * height));
    double distortedAngleDifference = distortedEndAngle
        - distortedStartAngle;
    if (distortedStartAngle < 0)
      distortedStartAngle += 360;
    if (distortedAngleDifference < 0)
      distortedAngleDifference += 360;

    Arc2D s = new Arc2D.Double(0000, distortedStartAngle,
        distortedAngleDifference, Arc2D.OPEN);
    s.setFrameFromDiagonal(p[0], p[1]);

    GeneralPath g = new GeneralPath();
    g.append(s, false);
    Rectangle2D r = new Rectangle2D.Double();
    r.setFrameFromDiagonal(p[0], p[1]);
    g.append(r, false);
    Point2D center = new Point2D.Double(centerX, centerY);
    g.append(new Line2D.Double(center, p[2])false);
    g.append(new Line2D.Double(center, p[3])false);
    return g;
  }
}

class PolygonMaker extends ShapeMaker {
  public PolygonMaker() {
    super(6);
  }

  public Shape makeShape(Point2D[] p) {
    GeneralPath s = new GeneralPath();
    s.moveTo((floatp[0].getX()(floatp[0].getY());
    for (int i = 1; i < p.length; i++)
      s.lineTo((floatp[i].getX()(floatp[i].getY());
    s.closePath();
    return s;
  }
}

class QuadCurveMaker extends ShapeMaker {
  public QuadCurveMaker() {
    super(3);
  }

  public Shape makeShape(Point2D[] p) {
    return new QuadCurve2D.Double(p[0].getX(), p[0].getY(), p[1].getX(),
        p[1].getY(), p[2].getX(), p[2].getY());
  }
}

class CubicCurveMaker extends ShapeMaker {
  public CubicCurveMaker() {
    super(4);
  }

  public Shape makeShape(Point2D[] p) {
    return new CubicCurve2D.Double(p[0].getX(), p[0].getY(), p[1].getX(),
        p[1].getY(), p[2].getX(), p[2].getY(), p[3].getX(), p[3].getY());
  }
}



           
         
    
    
    
    
  
Related examples in the same category
1. Creating Basic Shapes
2. fillRect (int, int, int, int) method draws a solid rectangle
3. Creating a Shape Using Lines and Curves
4. Combining Shapes
5. Draw rectangles, use the drawRect() method. To fill rectangles, use the fillRect() method
6. Draw lineDraw line
7. Draw a PolygonDraw a Polygon
8. Draw an oval outline
9. Draw a (Round)rectangleDraw a (Round)rectangle
10. Fill a polygonFill a polygon
11. Fill a solid oval
12. Fill a (Round)rectangleFill a (Round)rectangle
13. Change fontChange font
14. Draw rectangle 2Draw rectangle 2
15. Draw ArcDraw Arc
16. Draw EllipseDraw Ellipse
17. Fill a Rectangle 2Fill a Rectangle 2
18. Fill Arc 2Fill Arc 2
19. Draw textDraw text
20. Draw unicode string Draw unicode string
21. Shape combineShape combine
22. EffectsEffects
23. Mouse drag and drop to drawMouse drag and drop to draw
24. Arc demonstration: scale, move, rotate, sheerArc demonstration: scale, move, rotate, sheer
25. Hypnosis SpiralHypnosis Spiral
26. GlyphVector.getNumGlyphs()
27. Resize a shape
28. Rectangle with rounded corners drawn using Java 2D Graphics API
29. Compares two ellipses and returns true if they are equal or both null.
30. Compares two lines are returns true if they are equal or both null.
31. Creates a diagonal cross shape.
32. Creates a diamond shape.
33. Creates a new Stroke-Object for the given type and with.
34. Creates a region surrounding a line segment by 'widening' the line segment.
35. Creates a triangle shape that points downwards.
36. Creates a triangle shape that points upwards.
37. Generate Polygon
38. Polygon with float coordinates.
39. Polyline 2D
40. Serialises a Shape object.
41. Tests two polygons for equality. If both are null this method returns true.
42. Union two rectangles
43. Calculate Intersection Clip
44. Draws a shape with the specified rotation about (x, y).
45. Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2 has a height or width of zero!).
46. Reads a Point2D object that has been serialised by the writePoint2D(Point2D, ObjectOutputStream)} method.
47. Returns a point based on (x, y) but constrained to be within the bounds of a given rectangle.
48. RectListManager is a class to manage a list of rectangular regions.
w_ww__.__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.