2010-10-18 80 views
0

我想使用Graphic fillpolygon绘制箭头。但是我的箭头处于反面。任何想法?Java图形2D UI箭头方向

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); 

回答

2

在用户空间中给出Java 2D坐标,其中左上角是(0,0)。请参阅Coordinates

当使用从用户空间到设备空间的默认转换时,用户空间的原点是组件绘图区域的左上角。 x坐标向右增加,y坐标向下增加,如下图所示。窗口的左上角是0,0。所有的坐标都是用整数指定的,这通常就足够了。但是,有些情况下需要浮点或甚至双精度,这也是支持的。

alt text

我发现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); 
    } 
} 

full source

alt text

+0

难道只是更容易(而且更快)更改多边形坐标比转换整个画布? – wchargin 2013-10-05 04:07:47

+0

@WChargin我没有解释,在Java 2D左上角是(0,0)。我提出了一个答案,使平台服从OP认为是数据的自然表示,但没有任何事情停止发布你的答案,而是相反。 – 2013-10-05 04:53:42