Java图形2D UI箭头方向
我想用图形填充多边形画一个箭头。但我的箭头在另一边。有什么想法吗Java图形2D UI箭头方向,java,polygon,graphic,Java,Polygon,Graphic,我想用图形填充多边形画一个箭头。但我的箭头在另一边。有什么想法吗 int xpoints[] = { 20, 30, 30, 35, 25, 15, 20 }; int ypoints[] = { 10, 10, 30, 30, 45, 30, 30 }; int npoints = 7; g2D.fillPolygon(xpoints, ypoints, npoints); Java 2D坐标是在用户空间中给出的,其中左上角是(0,0)。见: 使用从用户空间到设备空间的默认转换时,用户空间的
int xpoints[] = { 20, 30, 30, 35, 25, 15, 20 };
int ypoints[] = { 10, 10, 30, 30, 45, 30, 30 };
int npoints = 7;
g2D.fillPolygon(xpoints, ypoints, npoints);
Java 2D坐标是在用户空间中给出的,其中左上角是(0,0)。见: 使用从用户空间到设备空间的默认转换时,用户空间的原点位于零部件绘图区域的左上角。x坐标向右增加,y坐标向下增加,如下图所示。窗口的左上角为0,0。所有坐标都是使用整数指定的,这通常就足够了。但是,在某些情况下,也支持浮点甚至双精度 我找到了,所以我修改了它,将原点平移到左下角,并将其与箭头组合在一起:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Insets insets = getInsets();
// int w = getWidth() - insets.left - insets.right;
int h = getHeight() - insets.top - insets.bottom;
AffineTransform oldAT = g2.getTransform();
try {
//Move the origin to bottom-left, flip y axis
g2.scale(1.0, -1.0);
g2.translate(0, -h - insets.top);
int xpoints[] = { 20, 30, 30, 35, 25, 15, 20 };
int ypoints[] = { 10, 10, 30, 30, 45, 30, 30 };
int npoints = 7;
g2.fillPolygon(xpoints, ypoints, npoints);
}
finally {
//restore
g2.setTransform(oldAT);
}
}
改变多边形坐标不是比变换整个画布更容易(更快)吗?@WChargin我确实解释过,在Java 2D中,左上角是(0,0)第一位。我给出了一个答案,让平台遵守OP认为的数据的自然表示,但没有什么能阻止发布你的答案,而你的答案恰恰相反。