重新绘制而不清除

时间:2013-03-25 17:43:22

标签: java swing paint repaint paintcomponent

我正在开发一个模仿Paint的程序。问题是当我绘制一个新形状时,前一个形状被删除。我试图评论我对paintComponents的超级调用,但它可以起作用,但留下了太多的绘图。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Canvas1 extends JPanel{

    Circle c;
    Rectangle r;
    Line l;
    String str;
    int x1,x2,y1,y2;
    Graphics g;
    int draw;
    int hollow=0;
    Color cc;


    public Canvas1(){

        LineListener listener = new LineListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);


        setBackground (Color.white);
        setPreferredSize (new Dimension(400, 400));

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(cc);
        if (this.draw == 1)
        {
            c = new Circle (x1,y1,x2,y2);
            if (hollow == 0)
            {
                c.hollow = false;
            }
            if (hollow == 1)
            {
                c.hollow = true;
            }
            c.draw(g);
        }
        if (this.draw ==2)
        {
            r = new Rectangle (x1,y1,x2,y2);
            if (hollow == 0)
            {
                r.hollow = false;
            }
            if (hollow == 1)
            {
                r.hollow = true;
            }   
            r.draw(g);
        }
        if (this.draw ==0)
        {
            l = new Line(x1,y1,x2,y2);
            l.draw(g);
        }
        if (this.draw ==3)
        {
            g.drawString(str, x1, y1);
        }

    }
    public void update(Graphics g) {
        paint(g);
        }
    private class LineListener implements MouseListener, MouseMotionListener{

        public void mousePressed(MouseEvent event){
            x1 = event.getX();
            y1 = event.getY();
            if (draw ==3)
            {
                str = JOptionPane.showInputDialog("Enter String");
                repaint();
            }
        }

        public void mouseDragged(MouseEvent event){
            if (draw ==1 || draw==2)
            {
                x2 = event.getX()-x1;
                y2 = event.getY()-y1;
            }
            if (draw ==0)
            {
                x2 = event.getX();
                y2 = event.getY();
            }
            repaint();

        }

        public void mouseClicked (MouseEvent event) {

        }
        public void mouseReleased (MouseEvent event) {
            if (draw ==1 || draw==2)
            {
                x2 = event.getX()-x1;
                y2 = event.getY()-y1;
            }
            if (draw ==0)
            {
                x2 = event.getX();
                y2 = event.getY();
            }
        }
        public void mouseEntered (MouseEvent event) {}
        public void mouseExited (MouseEvent event) {}
        public void mouseMoved (MouseEvent event) {}
    }
}

2 个答案:

答案 0 :(得分:2)

正如您所发现的那样,您需要致电super.paintComponent(g),否则JPanel背景不会被绘制,一切都变得一团糟。问题是,任何时候只能绘制一个形状,因为draw字段只能是单个值。一种解决方案是创建ArrayList个形状,并在List的{​​{1}}中绘制每个形状。

答案 1 :(得分:2)

Custom Painting Approaches显示了此问题的两种常见解决方案。

一个是从List中绘制。另一种是从BufferedImage中绘制。