我将此代码重写为JavaFX时遇到问题:
private final int ARR_SIZE = 8;
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
Graphics2D g = (Graphics2D) g1.create();
g.setPaint(Color.BLACK);
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);
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);
}
我的解决方案无法正常运行,因为代码的结果是:
drawArrow(gc, 200, 50, 50, 50);
是
预期结果是
private final int ARR_SIZE = 8;
void drawArrow(GraphicsContext gc, int x1, int y1, int x2, int y2) {
gc.setFill(Color.BLACK);
double dx = x2 - x1, dy = y2 - y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx * dx + dy * dy);
Affine affine = new Affine(Affine.translate(x1, y1));
affine.createConcatenation(Affine.rotate(angle, 0, 0));
gc.setTransform(affine);
gc.strokeLine(0, 0, len, 0);
gc.fillPolygon(new double[]{len, len - ARR_SIZE, len - ARR_SIZE, len}, new double[]{0, -ARR_SIZE, ARR_SIZE, 0},
4);
}
你可以帮我吗?
答案 0 :(得分:4)
有两个原因:
Transform.rotate
期望以度为单位的角度(请参阅javadoc for Rotate
constructor; Transform.rotate
的javadoc“重定向”那里),但Math.atan2
会返回弧度。 Transform.createConcatenation
创建新的Transform
,而不是修改现有的Transform
。此代码应该有效:
void drawArrow(GraphicsContext gc, int x1, int y1, int x2, int y2) {
gc.setFill(Color.BLACK);
double dx = x2 - x1, dy = y2 - y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx * dx + dy * dy);
Transform transform = Transform.translate(x1, y1);
transform = transform.createConcatenation(Transform.rotate(Math.toDegrees(angle), 0, 0));
gc.setTransform(new Affine(transform));
gc.strokeLine(0, 0, len, 0);
gc.fillPolygon(new double[]{len, len - ARR_SIZE, len - ARR_SIZE, len}, new double[]{0, -ARR_SIZE, ARR_SIZE, 0},
4);
}