Re: Draw a scaled arrow
This code probably makes more sense and can be run as is.
So this is what I am trying to achieve.
1. I am displaying an image, and want to draw arrows on this image at
the scale the image is displayed at using the mouse,
while the mouse button is not released
draw arrow to the JPanel at whatever scale the image is displayed at
but when I release mouse button draw to image back at 1:1, the reason it
is 1:1 is because the actual mage does not change size, oly what is
drawn to the jpanel changes size.
The problem lies with the arrow head, I need to apply the scale to it's
width an height and same with line to keep it in proportion.
My trig is a bit rusty, but I can make out that the 3 sets of points
make the triangle head, but when I try and apply the scale to the
xHeadPoints the head looses shape to name but one problem.
Hopefully the code below will be slightly clearer and originated from
the Sun Forum. I guess I just need a pointer as to where to apply the
scale factors to the arrow head so that it is scaled correctly if zoomed
in or out and drawn to the image correctly.
Code below:
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ArrowPanel extends JPanel {
FixedHeadArrow arrow = new FixedHeadArrow();
public ArrowPanel() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
arrow.x0 = e.getX();
arrow.y0 = e.getY();
arrow.x1 = arrow.x0;
arrow.y1 = arrow.y0;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
arrow.x1 = e.getX();
arrow.y1 = e.getY();
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
arrow.draw(g);
}
public static void main(String[] args) {
ArrowPanel arrowPanel = new ArrowPanel();
JFrame f = new JFrame();
f.add(arrowPanel);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400);
f.setLocationRelativeTo(null); // center
f.setVisible(true);
}
public class FixedHeadArrow {
private static final double HEAD_LENGTH = 15;
private static final double HEAD_WIDTH = 7; // actually half
the width
private static final double TAN = HEAD_WIDTH / HEAD_LENGTH;
int x0;
int y0;
int x1;
int y1;
private int xHeadPoints[] = new int[3];
private int yHeadPoints[] = new int[3];
public void draw(Graphics g) {
int dx = x1 - x0;
int dy = y1 - y0;
double lineLength = Math.sqrt(dx * dx + dy * dy);
double ratio = HEAD_LENGTH / lineLength;
xHeadPoints[0] = x0 + (int) Math.round((1 - ratio) * dx +
(ratio * dy) * TAN);
yHeadPoints[0] = y0 + (int) Math.round((1 - ratio) * dy -
(ratio * dx) * TAN);
xHeadPoints[1] = x0 + (int) Math.round((1 - ratio) * dx -
(ratio * dy) * TAN);
yHeadPoints[1] = y0 + (int) Math.round((1 - ratio) * dy +
(ratio * dx) * TAN);
// 3rd point of arrow head is line end
xHeadPoints[2] = x1;
yHeadPoints[2] = y1;
g.drawLine(x0, y0, x1, y1);
System.out.println("x0: " + x0);
System.out.println("x1: " + x1);
System.out.println("y0: " + y0);
System.out.println("y1: " + y1);
System.out.println("Tan: " + TAN);
System.out.println("ratio: " + ratio);
g.fillPolygon(xHeadPoints, yHeadPoints, 3);
}
}
}
Thanks
Rich