我想使用图形填充多边形绘制箭头。但我的箭头在反面。任何想法?
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。所有坐标都使用整数指定,这通常就足够了。但是,某些情况需要浮点甚至双精度,这也是受支持的。
我发现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);
}
}