无法摆脱绘制的线条

时间:2012-10-24 11:41:53

标签: java swing awt jpanel paint

我在互联网上发现了类似的问题,但提供的解决方案无效。

我想清除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);
        }
    }

}

2 个答案:

答案 0 :(得分: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();
            }
        });
    }
}

答案 1 :(得分:3)

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

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

这样,您可以确保在重新开始绘画时不会出现以前的颜料。