1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.gauger;
21
22 import java.awt.BasicStroke;
23 import java.awt.Color;
24 import java.awt.Graphics2D;
25 import java.awt.Point;
26 import java.awt.Rectangle;
27 import java.awt.geom.Line2D;
28
29 import com.cosylab.gui.components.Gauger;
30
31
32
33
34
35
36
37
38 public class ThinNeedle implements ActiveElement
39 {
40
41
42
43 private final static double RELATIVE_NEEDLE_SIZE = 0.6;
44 private Gauger gauger;
45 private boolean changeSignificant;
46 private Point p;
47 private double angle;
48 private Rectangle bounds = new Rectangle(0, 0, 0, 0);
49 private double currentPosition = 0.5;
50 protected ScaleTransform transform = null;
51
52
53 private Point p1 = new Point(0, 0);
54 private Point p2 = new Point(0, 0);
55
56 private Line2D line = new Line2D.Double();
57
58
59
60
61
62
63 public ThinNeedle(Gauger gauger)
64 {
65 p = new Point();
66 angle = 0;
67 this.gauger = gauger;
68 changeSignificant = true;
69 }
70
71
72
73
74 protected synchronized void calculatePolygon()
75 {
76 double sin = Math.sin(angle-Math.PI/2.0);
77 double cos = Math.cos(angle-Math.PI/2.0);
78
79 double x1 = (int)(p1.x * sin - p1.y * cos + p.x);
80 double y1 = (int)(p1.x * cos + p1.y * sin + p.y);
81
82 double x2 = (int)(p2.x * sin - p2.y * cos + p.x);
83 double y2 = (int)(p2.x * cos + p2.y * sin + p.y);
84
85 line.setLine(x1, y1, x2, y2);
86
87 bounds = line.getBounds();
88 bounds.grow(3, 3);
89 }
90
91 private synchronized final void invalidate()
92 {
93 double size = transform.scaleHeight(0.5) * RELATIVE_NEEDLE_SIZE;
94 p2.x = 0;
95 p2.y = (int)size;
96 }
97
98
99
100
101
102
103 public Rectangle getBounds()
104 {
105 return bounds;
106 }
107
108
109
110
111
112
113 public boolean isChangeSignificant()
114 {
115 return changeSignificant;
116 }
117
118 protected void updatePosition(double x)
119 {
120 if((transform != null)) {
121 transform.mapUVtoXY(x, 0, p);
122 angle = transform.getAngle(x);
123 calculatePolygon();
124 }
125 }
126
127
128
129
130
131
132
133 public void setPosition(double proportionalPosition)
134 {
135 updatePosition(proportionalPosition);
136
137 if(changeSignificant) {
138 currentPosition = proportionalPosition;
139 }
140 }
141
142
143
144
145
146
147 public synchronized void render(Graphics2D g)
148 {
149 g.setColor(gauger.getNeedleColor());
150
151 g.setColor(Color.BLACK);
152 BasicStroke bs = new BasicStroke(2.0f);
153 g.setStroke(bs);
154 g.draw(line);
155
156 }
157
158
159
160
161
162
163
164 public void setTransform(ScaleTransform transform)
165 {
166 this.transform = transform;
167 invalidate();
168 updatePosition(currentPosition);
169 }
170 }
171
172