Created
October 31, 2015 14:38
-
-
Save rkkautsar/7529b4816eba4dc24ed6 to your computer and use it in GitHub Desktop.
drawArrow in Graphics2D (http://stackoverflow.com/questions/4112701/drawing-a-line-with-arrow-in-java)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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