我想使用Graphic fillpolygon绘制箭头。但是我的箭头处于反面。任何的想法?
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);
答案 0 :(得分:2)
Java 2D坐标在用户空间中给出,其中左上角是(0,0)。见Coordinates:
当使用从用户空间到设备空间的默认转换时,用户空间的原点是组件绘图区域的左上角。 x坐标向右增加,y坐标向下增加,如下图所示。窗口的左上角是0,0。所有坐标都使用整数指定,这通常就足够了。但是,某些情况需要浮点或甚至双精度,这也是支持的。
我找到Java 2D - Affine Transform to invert y-axis,所以我修改了它以将原点转换为左下角,并将其与箭头结合:
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);
}
}