Skip to content

Instantly share code, notes, and snippets.

@rkkautsar
Created October 31, 2015 14:38
Show Gist options
  • Save rkkautsar/7529b4816eba4dc24ed6 to your computer and use it in GitHub Desktop.
Save rkkautsar/7529b4816eba4dc24ed6 to your computer and use it in GitHub Desktop.
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
Graphics2D g = (Graphics2D) g1.create();
double dx = x2 - x1, dy = y2 - y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx*dx + dy*dy);
AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
at.concatenate(AffineTransform.getRotateInstance(angle));
g.transform(at);
// Draw horizontal arrow starting in (0, 0)
g.drawLine(0, 0, len, 0);
g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment