View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim;
2   
3   import java.awt.*;
4   import java.io.Serializable;
5   
6   /***
7   * A line.
8   *
9   * @author © 1999 DIRAT Laurent
10  * @version 2.0  22/07/1999
11  */
12  public class Line implements Serializable {
13      /***
14      * The starting point of the line.
15      */
16      private Point p1;
17      
18      /***
19      * The ending point of the line.
20      */
21      private Point p2;
22      
23      /***
24      * The thickness of the line.
25      */
26      private int thickness;
27      
28      /***
29      * The alignment of the drawing of the line.
30      * <UL>
31      * <LI> LEFT : the thickness of the line is distributed to the left of the line</LI>
32      * <LI> RIGHT : the thickness of the line is distributed to the right of the line</LI>
33      * <LI> CENTER : the thickness of the line is distributed in equitably on both sides of the line </LI>
34      * </UL>
35      * Be carefull of the position of the points. The line goes from P1 to P2.
36      */
37      private byte alignment;
38      
39      private Polygon line;
40      
41      /***
42      * The constructor.<BR>
43      * Constructs the line [(0,0),(0,0)], with a thickness of 1.
44      */
45      public Line() {
46          this(new Point(), new Point(), 1, CENTER);
47      }
48      
49      /***
50      * The constructor.<BR>
51      * @param p1 the starting point of the line
52      * @param p2 the ending point of the line
53      * @param thickness the thickness of the line
54      * @param alignment the alignment of the line
55      */
56      public Line(Point p1, Point p2, int thickness, byte alignment) {
57          this.p1 = p1;
58          this.p2 = p2;
59          this.thickness = thickness;
60          this.alignment = alignment;
61          computeLine();
62      }
63      
64      /***
65      * Sets a new starting point.
66      * @param p1 the starting point of the line
67      */
68      public void setP1(Point p1) {
69          this.p1 = p1;
70          computeLine();
71      }
72      
73      /***
74      * Returns the starting point of the line.
75      */
76      public Point getP1() {
77          return p1;
78      }
79      
80      /***
81      * Sets a new ending point.
82      * @param p2 the ending point of the line
83      */
84      public void setP2(Point p2) {
85          this.p2 = p2;
86          computeLine();
87      }
88      
89      /***
90      * Returns the ending point of the line.
91      */
92      public Point getP2() {
93          return p2;
94      }
95      
96      /***
97      * Sets a new alingment.
98      * @param alignment the alignment of the line
99      */
100     public void setAlignment(byte alignment) {
101         this.alignment = alignment;
102     }
103     
104     /***
105     * Returns the alignement of the line.
106     */
107     public int getAlignment() {
108         return alignment;
109     }
110     
111     /***
112     * Sets the thickness of the line.
113     * @param thickness the thickness of the line
114     */
115     public void setThickness(int thickness) {
116         this.thickness = thickness;
117         computeLine();
118     }
119     
120     /***
121     * Returns the thickness of the line.
122     */
123     public int getThickness() {
124         return thickness;
125     }
126     
127     /***
128     * Draws the line.
129     * @param g the graphics in which the line has to be drawn.
130     */
131     public void draw(Graphics g) {
132         g.fillPolygon(line);
133     }
134     
135     /***
136     * Returns the <CODE>Polygon</CODE> which represents the line.
137     */
138     public Polygon getLine() {
139         return line;
140     }
141     
142     private void computeLine() {
143         line = new Polygon();
144         Point vectDir = new Point(p2.x - p1.x, p2.y - p1.y);
145         float ortho_x = -vectDir.y;
146         float ortho_y = vectDir.x;
147         // On normalise ortho
148         float norme = (float) Math.sqrt(ortho_x*ortho_x + ortho_y*ortho_y);
149         ortho_x = (ortho_x/norme)*((float) thickness);
150         ortho_y = (ortho_y/norme)*((float) thickness);
151         
152         int x, y;
153         
154         switch(alignment) {
155             case RIGHT :
156                 line.addPoint(p1.x, p1.y);
157                 line.addPoint(p2.x, p2.y);
158                 line.addPoint((int) Math.round(((int) p2.x) - ortho_x), (int) Math.round(((int) p2.y) - ortho_y));
159                 line.addPoint((int) Math.round(((int) p1.x) - ortho_x), (int) Math.round(((int) p1.y) - ortho_y));
160                 break;
161             case CENTER :
162                 float half_x = ortho_x/2.0f;
163                 float half_y = ortho_y/2.0f;
164                 line.addPoint((int) Math.round(((int) p1.x) - half_x), (int) Math.round(((int) p1.y) - half_y));
165                 line.addPoint((int) Math.round(((int) p2.x) - half_x), (int) Math.round(((int) p2.y) - half_y));
166                 line.addPoint((int) Math.round(((int) p2.x) + half_x), (int) Math.round(((int) p2.y) + half_y));
167                 line.addPoint((int) Math.round(((int) p1.x) + half_x), (int) Math.round(((int) p1.y) + half_y));
168                 break;
169             case LEFT :
170                 line.addPoint(p1.x, p1.y);
171                 line.addPoint(p2.x, p2.y);
172                 line.addPoint((int) Math.round(((int) p2.x) + ortho_x), (int) Math.round(((int) p2.y) + ortho_y));
173                 line.addPoint((int) Math.round(((int) p1.x) + ortho_x), (int) Math.round(((int) p1.y) + ortho_y));
174                 break;
175         }
176     }
177     
178     /***
179     * The different alignements of the line.
180     */
181     
182     /***
183     * The thickness of the line is distributed to the left of the line
184     */
185     public static final byte LEFT      = 0;
186     
187     /***
188     * The thickness of the line is distributed in equitably on both sides of the line
189     */
190     public static final byte CENTER    = 1;
191     
192     /***
193     * The thickness of the line is distributed to the right of the line
194     */
195     public static final byte RIGHT     = 2;
196 }