Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/port80/graph/impl/Arrow.java


1   //
2   // Copyright(c) 2002, Chris Leung
3   //
4   
5   package com.port80.graph.impl;
6   
7   import java.awt.*;
8   import java.awt.geom.*;
9   import java.awt.font.*;
10  import java.util.*;
11  import com.port80.graph.*;
12  
13  /** Arrow shape template objects.
14   *
15   *  Arrows are template items that can be rendered with an
16   *  AffineTransform or a given (x,y,width,height).
17   *
18   */
19  public class Arrow implements IArrow {
20  
21      // Static fields ///////////////////////////////////////////////////////
22      //
23  
24      private static final Color DEFCOLOR = Color.black;
25  
26      // Instance fields /////////////////////////////////////////////////////
27      //
28  
29      private String name;
30      private Shape shapeObject;
31      private Shape leadinObject;
32      private float lineWidth;
33      private Color color;
34      private Color leadinColor;
35      //
36      private double length;
37      private double width;
38  
39      // Constructors ////////////////////////////////////////////////////////
40      //
41  
42      public Arrow(String name, Shape shape, Shape leadin, float linewidth) {
43          this.name = name;
44          this.shapeObject = shape;
45          this.leadinObject = leadin;
46          this.lineWidth = linewidth;
47          //
48          this.color = DEFCOLOR;
49          this.leadinColor = DEFCOLOR;
50          //
51          this.length = shape.getBounds2D().getMaxX();
52      }
53      public Arrow(String name, Shape shape, Shape leadin,
54                   float linewidth,
55                   Color color, Color leadincolor) {
56          this.name = name;
57          this.shapeObject = shape;
58          this.leadinObject = leadin;
59          this.lineWidth = linewidth;
60          this.color = color;
61          this.leadinColor = leadincolor;
62          //
63          this.length = shape.getBounds2D().getMaxX();
64          this.width=shape.getBounds2D().getMaxY();
65      }
66  
67      public IArrow cloneInstance() {
68          AffineTransform tx = new AffineTransform();
69          IArrow ret = new Arrow(name,
70                                 tx.createTransformedShape(shapeObject),
71                                 tx.createTransformedShape(leadinObject),
72                                 lineWidth,
73                                 color, leadinColor
74                                );
75          return ret;
76      }
77  
78      // IArrow interface ///////////////////////////////////////////////
79      //
80  
81      public String getName() {
82          return name;
83      }
84      public double getLength() {
85          return length;
86      }
87      public double getWidth() {
88          return width;
89      }
90      public float getLineWidth() {
91          return lineWidth;
92      }
93      public Color getColor() {
94          return color;
95      }
96      public Color getLeadinColor() {
97          return leadinColor;
98      }
99  
100     public void setColor(Color color) {
101         this.color = color;
102     }
103     public void setLeadinColor(Color color) {
104         this.leadinColor = color;
105     }
106 
107     /** Render arrow with given transform instead of transform stored in the Arrow object.
108      */
109     public void render(Graphics2D g2d, float tailx, float taily, float headx, float heady) {
110         AffineTransform tx = AffineTransform.getTranslateInstance(tailx, taily);
111         tx.rotate(-Math.atan2(heady - taily, headx - tailx));
112         render(g2d, tx);
113     }
114 
115     /** Render arrow to given Graphics2D with the given transform
116      *  instead of the defined transform in Arrow itself.
117      */
118     public void render(Graphics2D g2d, AffineTransform tx) {
119         Shape shape;
120         if (leadinObject != null) {
121             shape = tx.createTransformedShape(leadinObject);
122             g2d.setPaint(leadinColor);
123             g2d.fill(shape);
124         }
125         shape = tx.createTransformedShape(shapeObject);
126         g2d.setPaint(color);
127         if (lineWidth > 0) {
128             g2d.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
129             g2d.draw(shape);
130         } else {
131             g2d.fill(shape);
132         }
133     }
134 
135 
136     public String toString() {
137         return name;
138     }
139 
140     ////////////////////////////////////////////////////////////////////////
141 
142 }
143