title: How to rotate a polygon about a point
body: <p>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.</p>
  
  <p>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)</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>int size - the size of the arrow divide 2.</p>
  
  <p>int x1, y1 - first shapes x and y coords. (centre of shape)</p>
  
  <p>int y2, x2 - second shapes x and y coords. (centre of shape)</p>
  
  <p>You can see what I mean in these pictures:
  <img src="https://i.stack.imgur.com/tIalB.png" alt="Example 1"></p>
  
  <p><img src="https://i.stack.imgur.com/PgYQb.png" alt="Example 2"></p>
  
  <p>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.</p>