I am using Java's Graphics2D libraries to draw various shapes with lines connecting them together. Some of these lines will need an arrow head at the end of the line. The shapes can be in any position so the angle of the arrow will change.
So far my code draws the arrow and rotates it except it isn't ever at the right angle or in the right location. When I move my shapes around the screen the arrow appears to orbit the shape it is meant to point to. (coord x2, y2)
private static void drawArrow(Graphics2D g, int size, int x1, int y1, int x2, int y2) {
double dx = x2 - x1, dy = y2 - y1;
double theta = Math.atan2(dy, dx);
AffineTransform at = AffineTransform.getTranslateInstance(x2, y2);
Polygon p = new Polygon();
p.addPoint(0, 0);
p.addPoint(size, 0 - size);
p.addPoint(0 - size, 0 - size);
at.rotate(theta, x2, y2);
java.awt.Shape shape = at.createTransformedShape(p);
g.fill(shape);
}
int size - the size of the arrow divide 2.
int x1, y1 - first shapes x and y coords. (centre of shape)
int y2, x2 - second shapes x and y coords. (centre of shape)
You can see what I mean in these pictures:


I have a feeling I'm close to getting this because it seems to orbit the shape perfectly, which suggests to me it is just not rotating at the right angle or point.
double theta = Math.atan2(dy, dx);have you tried printing this value? - Cruncher