views:

59

answers:

1

I want to draw an arrow using Graphic fillpolygon. But my arrow head is in a reverse side. Any idea?

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);
+1  A: 

Java 2D coordinates are given in user space, in which the top-left is (0, 0). See Coordinates:

When the default transformation from user space to device space is used, the origin of user space is the upper-left corner of the component’s drawing area. The x coordinate increases to the right, and the y coordinate increases downward, as shown in the following figure. The top-left corner of a window is 0,0. All coordinates are specified using integers, which is usually sufficient. However, some cases require floating point or even double precision which are also supported.

alt text

I found Java 2D - Affine Transform to invert y-axis, so I modified it to translate the origin to bottom-left, and combined it with your arrow:

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

eed3si9n