为什么 paintComponent 显示不正确?

Why is paintComponent not displaying correctly?

当运行我的程序时,有时绘制三角形,有时不绘制,有时只出现最后一个。本来我把代码放在一个for循环里,但是没有用,所以我试着倒着写出来,看看能不能用,但是没有用。正确的行为应该是,在屏幕上显示五个等距的三角形(在顶部矩形的正下方)。我尝试打印出数组,但是调用 println() 方法的次数是随机的,而不是常数。我听说 paintComponent() 方法可以被 Swing Framework 随时调用,但我不确定。本质上我是在问,为什么三角形(青色的)没有被正确绘制,我该如何修复它?

@SuppressWarnings("serial")
public class GraphicsClass extends JPanel {

    private int[] xCoordinates = {20, 40, 30};
    private int[] yCoordinates = {40, 40, 60};

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, 450, 40);
        g.fillRect(0, 260, 450, 40);

        g.setColor(Color.CYAN);
        g.fillPolygon(xCoordinates, yCoordinates, 3);
        xCoordinates[0] += 95;
        xCoordinates[1] += 95;
        xCoordinates[2] += 95;
        g.fillPolygon(xCoordinates, yCoordinates, 3);
        xCoordinates[0] += 95;
        xCoordinates[1] += 95;
        xCoordinates[2] += 95;
        g.fillPolygon(xCoordinates, yCoordinates, 3);
        xCoordinates[0] += 95;
        xCoordinates[1] += 95;
        xCoordinates[2] += 95;
        g.fillPolygon(xCoordinates, yCoordinates, 3);
        xCoordinates[0] += 95;
        xCoordinates[1] += 95;
        xCoordinates[2] += 95;
        g.fillPolygon(xCoordinates, yCoordinates, 3);
    }
}

这样想。 paintComponent 可以随时调用(在某些情况下,当组件首次在屏幕上绘制时,可以调用四次以上)。因此每次调用它时,您都将 95 添加到每个 xCoordinates,这将使 xCoordinates[0] 在调用 paintComponent 之后等于 400第一次,800第二次,以此类推第四次...

相反,您需要复制 xCoordinates 并修改它,例如...

public class TestPane extends JPanel {

    private int[] xCoordinates = {20, 40, 30};
    private int[] yCoordinates = {40, 40, 60};

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, 450, 40);
        g.fillRect(0, 260, 450, 40);

        int[] xPosy = Arrays.copyOf(xCoordinates, xCoordinates.length);
        g.setColor(Color.CYAN);
        for (int index = 0; index < 4; index++) {

            g.fillPolygon(xPosy, yCoordinates, 3);
            xPosy[0] += 95;
            xPosy[1] += 95;
            xPosy[2] += 95;

        }
    }
}

当然,您可以放弃一些奇怪的东西,只使用 2D 图形 Shape API

public class TestPane extends JPanel {

    private Polygon triangle;

    public TestPane() {
        triangle = new Polygon(new int[]{20, 40, 30}, new int[]{40, 40, 60}, 3);
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setColor(Color.DARK_GRAY);
        g2d.fillRect(0, 0, 450, 40);
        g2d.fillRect(0, 260, 450, 40);

        g2d.setColor(Color.CYAN);
        AffineTransform at = AffineTransform.getTranslateInstance(0, 0);
        for (int index = 0; index < 4; index++) {

            Shape shape = at.createTransformedShape(triangle);
            g2d.fill(shape);
            at.translate(95, 0);

        }
    }
}