JPanel super.pantComponent(Graphics g)无法按需要清除我的面板

问题描述 投票:0回答:1

这是在面板上绘制形状的代码。我的想法是:在启动绘制事件时,清除面板,如果任何“绘图仪”具有“ isDraw = true”,我将对其进行绘制。但是在运行时,我更改了变量(请参见throw debug),但是面板没有清除任何内容,所有行都仍然存在。我该怎么办?

protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        // setBackground(Color.CYAN);
        drawAxis(g);
        for (PlotterSin plotter : allPlotter) {
            if (plotter.isDraw()) {

            Polygon p = new Polygon();
            Polygon p2 = new Polygon();

            for (int x = -170; x <= 170; x++) {
                p.addPoint(x + 200, 100 - (int) (50 * plotter.func((x / 100.0) * 2 * Math.PI)));

            }

            g.setColor(plotter.getColor());
            g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
            g.drawString("-2\u03c0", 95, 115);
            g.drawString("2\u03c0", 305, 115);
            g.drawString("0", 200, 115);
        }
        // g.setColor(Color.blue);
        // g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);
    }

}
java graphics jpanel paintcomponent
1个回答
0
投票

我忘了在更改后调用JFrame的repaint()包含该Jpanel。 jpanel的paintComponent()仅在它的父repaint()称为

之后才能工作
© www.soinside.com 2019 - 2024. All rights reserved.