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);
解決
Java 2D座標は、最上部が(0、0)のユーザースペースで提供されます。見る 座標:
ユーザースペースからデバイススペースへのデフォルトの変換が使用される場合、ユーザースペースの起源はコンポーネントの描画領域の左上隅です。 X座標は右に増加し、次の図に示すようにY座標は下方に増加します。窓の左上隅は0,0です。すべての座標は、通常十分である整数を使用して指定されています。ただし、一部のケースでは、フローティングポイントまたはサポートされている二重精度さえ必要とします。
私は見つけました Java 2d -y軸を反転するアフィン変換, 、そこで、原点を左下に変換するように変更し、矢と組み合わせました。
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);
}
}
所属していません StackOverflow