3

我在互联网上发现了类似的问题,但提供的解决方案不起作用。

我想清除JPanel。为此,我调用repaint()(从clear()方法)并将标志设置为 false 以避免调用我的绘图方法(drawLines())。画线仍在面板上。

我尝试用背景颜色重新绘制相同的线条。这也没有奏效。

  public class WektPanel extends JPanel{    

    boolean check = false;
    Color c = Color.BLUE;
    boolean oval = false;

    public WektPanel() {
        setBackground(c);
        setVisible(true);

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(check)
            drawLines(3, g);

    }

    void clear(){
        check=false;
        repaint();
    }
    void draw(){
        check=true;
        repaint();
    }

    void drawLines(int stroke, Graphics g){

        g.drawLine(0,0,getWidth(),getHeight());
        g.drawLine(0,getHeight(),getWidth(),0);
        for(int i=1; i<stroke;i++){
                g.drawLine(0+i,0,getWidth(),getHeight()-i);
                g.drawLine(0,0+i,getWidth()-i,getHeight());
                g.drawLine(0,getHeight()-i,getWidth()-i,0);
                g.drawLine(0+i,getHeight(),getWidth(),0+i);
        }
    }

}
4

2 回答 2

3

你压倒一切paint(),但打电话super.paintComponent(g)。你应该覆盖paintComponent(g).

编辑:我无法遵循你的check逻辑。这对我有用。

public class Test {

    private class WektPanel extends JPanel {

        boolean clear;

        public WektPanel() {
            setBackground(Color.BLUE);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 200);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawLines(3, g);
        }

        public void clear() {
            clear = true;
            repaint();
        }

        public void draw() {
            clear = false;
            repaint();
        }

        private void drawLines(int stroke, Graphics g) {
            if (!clear) {
                g.drawLine(0, 0, getWidth(), getHeight());
                g.drawLine(0, getHeight(), getWidth(), 0);
                for (int i = 1; i < stroke; i++) {
                    g.drawLine(0 + i, 0, getWidth(), getHeight() - i);
                    g.drawLine(0, 0 + i, getWidth() - i, getHeight());
                    g.drawLine(0, getHeight() - i, getWidth() - i, 0);
                    g.drawLine(0 + i, getHeight(), getWidth(), 0 + i);
                }
            }
        }
    }

    private void showGUI() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final WektPanel wektPanel = new WektPanel();
        f.add(wektPanel, BorderLayout.CENTER);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new AbstractAction("Clear") {

            @Override
            public void actionPerformed(ActionEvent e) {
                wektPanel.clear();
            }
        }));
        buttonPanel.add(new JButton(new AbstractAction("Draw") {

            @Override
            public void actionPerformed(ActionEvent e) {
                wektPanel.draw();
            }
        }));
        f.add(buttonPanel, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().showGUI();
            }
        });
    }
}
于 2012-10-24T11:45:43.783 回答
3

方法中的第一行paint应该是这样的:

g.clearRect(0, 0, getWidth(), getHeight());

这样,当您再次开始绘画时,您可以确保以前的绘画不会在那里。

于 2012-10-24T11:47:22.907 回答