1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author Fabrice Bouyé (fabriceb@spc.int)
* @version 1.0
*/
public class ArrowPanel extends JPanel implements ComponentListener, MouseInputListener {
/** Default width.
*/
public static final int DEFAULT_WIDTH = 300;
/** Default height.
*/
public static final int DEFAULT_HEIGHT = 300;
/** Default size.
*/
public static final Dimension DEFAULT_SIZE = new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
/** Starting angle.
*/
public static final double DEFAULT_ANGLE = Math.PI / 4.0;
/** Labels for angles around the trigonometric circle.
*/
private static final String[] ANGLE_LABELS = new String[] {"0", "\u03C0/2", "\u03C0", "3*\u03C0/2"};
/** The trigonometric circle shape.
*/
private Ellipse2D.Double circle = new Ellipse2D.Double();
/** Current segment angle vs X axis.
*/
private double angle = DEFAULT_ANGLE;
/** Current segment.
*/
private Line2D.Double segment = new Line2D.Double();
/** Creates a new instance.
*/
public ArrowPanel() {
super();
addComponentListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(DEFAULT_SIZE);
setSize(DEFAULT_SIZE);
}
/** {@inheritDoc}
*/
@Override protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
Graphics2D g2 = (Graphics2D) graphics.create(insets.left, insets.top, width, height);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try {
paintAxis(g2, width, height);
paintCircle(g2, width, height);
paintArrow(g2, width, height);
} finally {
g2.dispose();
}
}
/** Paint the axis.
* @param graphics The graphics context in which to render.
* @param width The width of the drawing area.
* @param height The height of the drawing area.
*/
private void paintAxis(Graphics2D graphics, int width, int height) {
graphics.setColor(Color.GRAY);
int centerX = (int) (circle.getX() + circle.getWidth() / 2.0);
int centerY = (int) (circle.getY() + circle.getHeight() / 2.0);
graphics.drawLine(0, centerY, width, centerY);
graphics.drawLine(centerX, 0, centerX, height);
}
/** Paint the circle.
* @param graphics The graphics context in which to render.
* @param width The width of the drawing area.
* @param height The height of the drawing area.
*/
private void paintCircle(Graphics2D graphics, int width, int height) {
graphics.setColor(Color.BLACK);
graphics.draw(circle);
int centerX = (int) (circle.getX() + circle.getWidth() / 2.0);
int centerY = (int) (circle.getY() + circle.getHeight() / 2.0);
int radius = (int) circle.getWidth() / 2;
// Draw angle labels around the circle.
graphics.translate(centerX, centerY);
AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 2.0);
Point2D.Double point = new Point2D.Double(radius + 20, 0);
for (int i = 0; i < 4; i++) {
graphics.drawString(ANGLE_LABELS[i], (float) point.x, (float) point.y);
transform.transform(point, point);
}
graphics.translate( -centerX, -centerY);
}
/** Paint the arrow.
* @param graphics The graphics context in which to render.
* @param width The width of the drawing area.
* @param height The height of the drawing area.
*/
private void paintArrow(Graphics2D graphics, int width, int height) {
graphics.setColor(Color.BLUE);
graphics.draw(segment);
drawArrowHeadAtLocation(graphics, segment.getX2(), segment.getY2(), angle);
}
/** Paint the arrow head at given location and angle.
* @param graphics The graphics context in which to render.
* @param x The X coordinate.
* @param y the Y coordinate.
* @param angle The angle (in radian).
*/
private void drawArrowHeadAtLocation(Graphics2D graphics, double x, double y, double angle) {
graphics.translate(x, y);
graphics.rotate(angle);
drawArrowHead(graphics);
graphics.rotate( -angle);
graphics.translate( -x, -y);
}
/** Draw a simple arrow head at (0, 0) that points to the right.
* @param graphics The graphics context in which to render.
*/
private void drawArrowHead(Graphics2D graphics) {
graphics.drawLine(0, 0, -10, 5);
graphics.drawLine(0, 0, -10, -5);
graphics.drawLine( -10, 5, -10, -5);
}
///////////////
// Geometry. //
///////////////
/** Recalculate all shapes with given drawing area dimension.
* @param width The width of the drawing area.
* @param height The height of the drawing area.
*/
private void recalculateShapes(double width, double height) {
double centerX = width / 2.0;
double centerY = height / 2.0;
double radius = Math.min(width / 2.0, height / 2.0) / 2.0;
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
double arrowLength = radius * 1.33;
double endX = centerX + arrowLength;
double endY = centerY;
AffineTransform transform = AffineTransform.getRotateInstance(angle, centerX, centerY);
Point2D endPoint = transform.transform(new Point2D.Double(endX, endY), null);
segment.setLine(centerX, centerY, endPoint.getX(), endPoint.getY());
}
/** Reset the segment's angle relative to a give mouse event.
* @param event The mouse event.
*/
private void resetAngleRelativeToEvent(MouseEvent event) {
Insets insets = getInsets();
int x = event.getX() - insets.left;
int y = event.getY() - insets.top;
resetAngleRelativeToLocation(x, y);
}
/** Reset the segment's angle relative to a given location.
* @param locationX The X coordinate.
* @param locationY The Y corrdinate.
*/
private void resetAngleRelativeToLocation(double locationX, double locationY) {
try {
int centerX = (int) (circle.getX() + circle.getWidth() / 2.0);
int centerY = (int) (circle.getY() + circle.getHeight() / 2.0);
// Angle between (center, location) and X axis.
double newAngle = calculateAngle(centerX, centerY, locationX, locationY);
AffineTransform transform = AffineTransform.getRotateInstance(newAngle - angle, centerX, centerY);
Point2D endPoint = transform.transform(segment.getP2(), null);
segment.setLine(centerX, centerY, endPoint.getX(), endPoint.getY());
angle = newAngle;
}
// Silently consume exception.
catch (Throwable t) {
}
}
/////////////////////
// static methods. //
/////////////////////
/** Self-test main.
* @param args Arguments from the command line.
*/
public static void main(String ...args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ArrowPanel(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
/** Gets the angle between the given vector and the horizontal axis.
* @param x1 The X coordinate of the first point.
* @param y1 The Y coordinate of the first point.
* @param x2 The X coordinate of the first point.
* @param y2 The Y coordinate of the first point.
* @return A value within the <CODE>[0, 2*PI[</CODE> range.
* @exception ValueNotDefinedException If the vector's length is zero, the angle is not defined.
* @since 1.1
*/
public static double calculateAngle(double x1, double y1, double x2, double y2) throws ValueNotDefinedException {
double a = (x2 - x1);
double b = (y2 - y1);
double length = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
if (length == 0) {
throw new ValueNotDefinedException();
}
double p = length; //Math.sqrt(a*a+b*b);
double theta = Math.asin(Math.abs(b) / p);
if (b < 0) {
theta = -theta;
}
if (a < 0) {
theta = Math.PI - theta;
}
// Rescale into [0, 2*PI[.
theta %= 2 * Math.PI;
if (theta < 0) {
theta += 2 * Math.PI;
}
return theta;
}
///////////////////////
// Event management. //
///////////////////////
// ComponentListener interface.
/** {@inheritDoc}
*/
public void componentResized(ComponentEvent event) {
Dimension size = getSize();
Insets insets = getInsets();
int width = size.width - (insets.left + insets.right);
int height = size.height - (insets.top + insets.bottom);
recalculateShapes(width, height);
repaint();
}
/** {@inheritDoc}
*/
public void componentMoved(ComponentEvent event) {
}
/** {@inheritDoc}
*/
public void componentShown(ComponentEvent event) {
}
/** {@inheritDoc}
*/
public void componentHidden(ComponentEvent event) {
}
// MouseInputListener interface.
/** {@inheritDoc}
*/
public void mouseClicked(MouseEvent event) {
}
/** {@inheritDoc}
*/
public void mousePressed(MouseEvent event) {
resetAngleRelativeToEvent(event);
repaint();
}
/** {@inheritDoc}
*/
public void mouseReleased(MouseEvent event) {
}
/** {@inheritDoc}
*/
public void mouseEntered(MouseEvent event) {
}
/** {@inheritDoc}
*/
public void mouseExited(MouseEvent event) {
}
/** {@inheritDoc}
*/
public void mouseDragged(MouseEvent event) {
resetAngleRelativeToEvent(event);
repaint();
}
/** {@inheritDoc}
*/
public void mouseMoved(MouseEvent event) {
}
////////////////////
// Inner classes. //
////////////////////
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author Fabrice Bouyé (fabriceb@spc.int)
* @version 1.0
*/
public static class ValueNotDefinedException extends Exception {}
} |
Partager