Java paintComponent()的多个调用

Java paintComponent()的多个调用,java,swing,awt,java-2d,Java,Swing,Awt,Java 2d,我有两门课: public class Pencil extends JComponent implements MouseListener, MouseMotionListener{ Plansa plansa; Graphics g; public Pencil(Plansa newCanvas){ this.plansa = newCanvas; this.plansa.setFlagShape(false); } @Override public void mouseDr

我有两门课:

public class Pencil extends JComponent implements MouseListener, MouseMotionListener{
Plansa plansa;
Graphics g;
public Pencil(Plansa newCanvas){
    this.plansa = newCanvas;
    this.plansa.setFlagShape(false);
}
@Override
public void mouseDragged(MouseEvent arg0) {
    plansa.setMouseDragged(arg0);
    this.plansa.setFlagShape(false);
    plansa.paintComponent(plansa.getGraphics());
}

@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent arg0) {
    plansa.setMousePressed(arg0);
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
    plansa.setMouseReleased(arg0);
    this.plansa.setFlagShape(true);
    plansa.paintComponent(plansa.getGraphics());

}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
}

}
还有这个:

public class Plansa extends JPanel{
Image image;
Pencil pencil;
 //...
 void init(){
    this.setShape("freeLine");
    this.setColor(Color.BLACK);
    this.size=1;
    this.type="round";
    this.fill=false;
    this.setBackground(Color.WHITE);
    pencil = new Pencil(this);
    addMouseListener(pencil);
    addMouseMotionListener(pencil);
    flagPaint = true;
    flagShape = false;
}
public Plansa(){
    this.setSize(800, 600);
    init();
}

//...

       @Override
    public void paintComponent(Graphics g) {
    g.setColor(currentColor);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(setBrush(size,type));
    super.paintComponent(g);
    switch(shape){
        default: break;
        case "freeLine":{ 
            g.drawLine(xDragged, yDragged, xCurrent, yCurrent); 
            break;
        }
            case "rectangle":{
                if(flagShape == true){
            g.drawRect(xPressed, yPressed, Math.max(xCurrent-xPressed,xPressed-xCurrent), Math.max(yCurrent-yPressed,yPressed-yCurrent));
            if(fill == true) g.fillRect(xPressed, yPressed, Math.max(xCurrent-xPressed,xPressed-xCurrent), Math.max(yCurrent-yPressed,yPressed-yCurrent));
                }
            break;
        }
        case "circle":{
            if(flagShape == true){
            int radius = (int)Math.sqrt(Math.max(xCurrent-xPressed,xPressed-xCurrent)*Math.max(xCurrent-xPressed,xPressed-xCurrent)+Math.max(yCurrent-yPressed,yPressed-yCurrent)*Math.max(yCurrent-yPressed,yPressed-yCurrent));
            g.drawOval(xPressed, yPressed, radius, radius);
            if(fill == true) g.fillOval(xPressed, yPressed, radius, radius);
            }
            break;
        }
        case "oval":{
            if(flagShape == true){
            g.drawOval(xPressed, yPressed, Math.max(xCurrent-xPressed,xPressed-xCurrent), Math.max(yCurrent-yPressed,yPressed-yCurrent));
            if(fill == true) g.fillOval(xPressed, yPressed, Math.max(xCurrent-xPressed,xPressed-xCurrent), Math.max(yCurrent-yPressed,yPressed-yCurrent));
            }
            break;
        }
        case "line":{
            if(flagShape == true){
            g.drawLine(xPressed, yPressed, xCurrent, yCurrent);
            }
            break;
        }
    }

}

 //...
}
我的问题是每次调用paintComponent方法时,JPanel都会清除,剩下的唯一一项就是我刚才绘制的那个项。有什么办法可以避免这种情况吗?

不要使用/注释掉:

super.paintComponent(g);

该行就是进行清除的行。

将所有对象存储在ArrayList中,并在绘图时对其进行迭代


你可以让他们都实现一个定制的界面,例如Drawable,然后存储在ArrayList中。

在仔细检查代码之后,你似乎试图将组件用作油漆工,这有点像用跑车作为卡丁车,为了一点好处而增加了很多额外的东西

相反,您应该为自己定义一个kine接口,该接口提供您想要绘制/绘制的内容的基本需求,然后定义实现该功能的具体类实现

然后,您将维护一个可以绘制的所有图形的列表

下面的示例虽然简单,但提供了一个起点,可以对其进行增强,以便在需要时选择图形、重新排序和删除

本质上,这是门把手+1提出的相同概念

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestDraw {

    public static void main(String[] args) {
        new TestDraw();
    }

    public TestDraw() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new Pencil());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Pencil extends JPanel implements MouseListener, MouseMotionListener {

        private List<Drawable> drawables;
        private Drawable activeDrawable;

        private Point clickPoint;

        public Pencil() {
            drawables = new ArrayList<>(5);
            addMouseListener(this);
            addMouseMotionListener(this);
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Drawable drawable : drawables) {
                drawable.paint(g2d);
            }
            g2d.dispose();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (activeDrawable != null) {
                Point p = e.getPoint();
                Rectangle bounds = activeDrawable.getBounds();

                int x = bounds.x;
                int y = bounds.y;
                int width = p.x - clickPoint.x;
                int height = p.y - clickPoint.y;
                if (width < 0) {
                    width *= -1;
                    x = p.x;
                }
                if (height < 0) {
                    height *= -1;
                    y = p.y;
                }
                bounds = new Rectangle(x, y, width, height);
                System.out.println(bounds);
                activeDrawable.setBounds(bounds);
                repaint();
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        protected Drawable createActiveShape(MouseEvent e) {

            System.out.println("Anchor = " + e.getPoint());
            Drawable drawable = new FreeLine(e.getPoint());
            drawable.setLocation(e.getPoint());
            return drawable;

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // You could also check to see if the clicked on a drawable...
            clickPoint = e.getPoint();
            activeDrawable = createActiveShape(e);
            drawables.add(activeDrawable);
            repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (activeDrawable != null) {
                Rectangle bounds = activeDrawable.getBounds();
                if (bounds.width == 0 || bounds.height == 0) {
                    drawables.remove(activeDrawable);
                }
            }
            clickPoint = null;
            activeDrawable = null;
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }
    }

    public interface Drawable {

        public void setLocation(Point p);

        public void setSize(Dimension dim);

        public void setBounds(Rectangle bounds);

        public Rectangle getBounds();

        public void paint(Graphics2D g2d);
    }

    public abstract class AbstractDrawable implements Drawable {

        private Rectangle bounds;

        public AbstractDrawable() {
            bounds = new Rectangle();
        }

        @Override
        public void setLocation(Point p) {
            bounds.setLocation(p);
        }

        @Override
        public void setBounds(Rectangle bounds) {
            this.bounds = bounds;
        }

        @Override
        public void setSize(Dimension dim) {
            bounds.setSize(dim);
        }

        @Override
        public Rectangle getBounds() {
            return bounds;
        }
    }

    public class FreeLine extends AbstractDrawable {

        private Point anchor;

        public FreeLine(Point anchor) {
            this.anchor = anchor;
        }

        @Override
        public void paint(Graphics2D g2d) {
            Rectangle bounds = getBounds();
            Point p1 = new Point(anchor);
            Point p2 = new Point(bounds.getLocation());
            if (p1.x > p2.x) {
                p2.x = p1.x - bounds.width;
            } else {
                p2.x = p1.x + bounds.width;
            }
            if (p1.y > p2.y) {
                p2.y = p1.y - bounds.height;
            } else {
                p2.y = p1.y + bounds.height;
            }
            g2d.draw(new Line2D.Float(p1, p2));
        }
    }

对于渐进式绘图,通常更好的方法是使用BuffereImage作为画布,如图所示


你不应该自己打电话。这是绘制链的一部分,由RepainManager通过事件调度线程代表您调用。相反,您应该使用repaint,它向repainmanager发出请求,以便在将来某个时间安排更新。您也不应该使用getGraphics。此方法可能返回null,并且只是上一个绘制周期的快照。它将在下一个绘制周期中被擦除。我想到的问题是,为什么不直接将平面添加到父组件,然后使用空/绝对布局管理器来布局组件呢。其余的都会照顾好你的。另外,请记住,绘画是无状态的。也就是说,在每一个油漆循环中所需的油漆都必须在一个合适的油漆方法中重新粉刷。我会考虑这一点,谢谢。一般来说,渐进绘画的一个更好的想法是用一个缓冲图案作为画布,如图所示。“Andrew Thompson,我试过了,效果不错。”谢谢。我认为应该进一步强调的是,这个建议只是为了观察代码之间的差异,所有现实世界的实现都应该调用super.paintcomponent;我也想到了这一点,但不知道如何做,对象使用什么数据类型。我应该创建一个新类,还是使用一些默认值?好的,现在我知道了。这个例子非常有用。非常感谢你的帮助@门把手你还是给了我这个主意