在运行程序时,有时会绘制三角形,有时则不会绘制三角形,有时只会显示最后一个三角形。最初,我将代码放入for循环中,但是它没有用,所以我尝试倒退并将其全部写出,以查看它是否有效,但无济于事。正确的行为应该是,在屏幕上等距显示五个三角形(位于顶部矩形的正下方)。我尝试打印出数组,但是调用println()方法的次数是随机的,而不是常量。我听说Swing框架可以随时调用paintComponent()方法,但是我不确定。本质上,我是在问,为什么三角形(青色的)没有正确绘制,我该如何解决?

@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]之后使400等于paintComponent,在第二次调用后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 Graphics 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);

        }
    }
}

关于java - 为什么paintComponent无法正确显示?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28955616/

10-11 20:50