6

我有一个简单的类,它在鼠标拖动时画一条线,或者在鼠标按下(释放)时画一个点。

当我最小化应用程序然后恢复它时,窗口的内容会消失,除了最后一个点(像素)。我知道super.paint(g)每次窗口更改时该方法都会重新绘制背景,但无论我是否使用它,结果似乎都是一样的。两者的区别在于,当我不使用它时,窗口上只画了一个像素,但不是我的全部画。我怎样才能解决这个问题?

这是课程。

package painting;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;

class CustomCanvas extends Canvas{   
    Point oldLocation= new Point(10, 10);
    Point location= new Point(10, 10);
    Dimension dimension = new Dimension(2, 2);     
    CustomCanvas(Dimension dimension){  
        this.dimension = dimension;   
        this.init();
        addListeners();
    }    
    private void init(){                     
        oldLocation= new Point(0, 0);
        location= new Point(0, 0);
    }
    public void paintLine(){
        if ((location.x!=oldLocation.x) || (location.y!=oldLocation.y)) {         
            repaint(location.x,location.y,1,1);                                   
        } 
    }
    private void addListeners(){
        addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent me){                   
                oldLocation = location;
                location = new Point(me.getX(), me.getY());
                paintLine();
            }
            @Override
            public void mouseReleased(MouseEvent me){                
                oldLocation = location;
                location = new Point(me.getX(), me.getY());
                paintLine();
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent me){                
                oldLocation = location;
                location = new Point(me.getX(), me.getY());
                paintLine();
            }
        });
    }
    @Override
    public void paint(Graphics g){  
        super.paint(g);
        g.setColor(Color.red);       
        g.drawLine(location.x, location.y, oldLocation.x, oldLocation.y);                
    }
    @Override
    public Dimension getMinimumSize() {
        return dimension; 
    }
    @Override
    public Dimension getPreferredSize() {
        return dimension;
    }

}
class CustomFrame extends JPanel {
    JPanel displayPanel = new JPanel(new BorderLayout());
    CustomCanvas canvas = new CustomCanvas(new Dimension(200, 200));        
    public CustomFrame(String titlu) {            
        canvas.setBackground(Color.white);
        displayPanel.add(canvas, BorderLayout.CENTER);            
        this.add(displayPanel);
    }   
}
public class CustomCanvasFrame {
    public static void main(String args[]) {
        CustomFrame panel = new CustomFrame("Test Paint");
        JFrame f = new JFrame();
        f.add(panel);
        f.pack();
        SwingConsole.run(f, 700, 700);
    }
}
4

4 回答 4

5

这是否意味着每次按下或拖动鼠标时我都会绘制所有点(而不是一个)?

是的,但是@Dave 的方法对于数千个节点来说是完全令人满意的,如GraphPanel. 除此之外,请考虑渲染器使用的享元模式,并在此处说明。JTable

附录:针对您的问题,下面的变化可能说明了系统触发绘画和应用触发绘画AWTPainting之间的区别。当鼠标被拖动时,调用,调用; 这是应用程序触发的。当您调整窗口大小时,仅调用(不绘制红色数字);这是系统触发的。请注意,调整大小后释放鼠标时会出现闪烁。repaint()update()paint()paint()

当整个组件的背景被清除并重绘时,通常会发生闪烁:

 4. 如果组件没有覆盖update()默认实现update()清除组件的背景(如果不是轻量级组件),简单调用paint().

AWT绘画

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class AWTPainting {

    public static void main(String args[]) {
        CustomPanel panel = new CustomPanel();
        Frame f = new Frame();
        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.add(panel);
        f.pack();
        f.setVisible(true);
    }
}

class CustomPanel extends Panel {

    public CustomPanel() {
        this.add(new CustomCanvas(new Dimension(320, 240)));
    }
}

class CustomCanvas extends Canvas {

    private MouseAdapter handler = new MouseHandler();
    private List<Point> locations = new ArrayList<Point>();
    private Point sentinel = new Point();
    private Dimension dimension;

    CustomCanvas(Dimension dimension) {
        this.dimension = dimension;
        this.setBackground(Color.white);
        this.addMouseListener(handler);
        this.addMouseMotionListener(handler);
        this.locations.add(sentinel);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.blue);
        Point p1 = locations.get(0);
        for (Point p2 : locations.subList(1, locations.size())) {
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
            p1 = p2;
        }
    }

    @Override
    public void update(Graphics g) {
        paint(g);
        g.clearRect(0, getHeight() - 24, 50, 20); // to background
        g.setColor(Color.red);
        g.drawString(String.valueOf(locations.size()), 8, getHeight() - 8);
    }

    private class MouseHandler extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            if (locations.get(0) == sentinel) { // reference identity
                locations.set(0, new Point(e.getX(), e.getY()));
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            locations.add(new Point(e.getX(), e.getY()));
            repaint();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return dimension;
    }
}
于 2012-04-11T18:58:03.877 回答
5

您没有存储正在绘制的点的状态。重新绘制面板时,它只包含它绘制的最后一个点的信息。


回复评论:

例如,您需要收集积分ArrayList<Point> location = new ArrayList<Point>();

然后,在你的听众中:location.add(new Point(me.getX(), me.getY()));

最后,在paintLine() 中:

for (Point location : locations) {
  repaint(location.x,location.y,1,1); 
}

该集合locations通常称为显示列表。大多数图形程序都使用它们。


回复评论:

是的,我希望如此。我只是根据您的代码提出了一个想法,以便为您提供一个起点。完全按照我的描述去做几乎肯定是个坏主意。

于 2012-04-11T17:13:47.623 回答
2

@Andrew,@Dave,@trashgod 嗨,我对此进行了一些研究,最后,这就是我所拥有的。如果我错了,请纠正我。您不能覆盖paint(),因此每次需要执行应用程序触发的绘画时都调用repaint()。Repaint() 调用 update(),其默认行为是调用 paint()。update() 用于增量绘制;这解释了当paint() 完成所有工作时闪烁的屏幕,这实际上意味着它在每一步都在绘制整个图像。但是,我的问题是,如果我在更新方法中添加“locationsAdded = 0”,这意味着每次拖动鼠标时我都会绘制整个图像(就像在绘制中一样),那么为什么它不像以前那样闪烁呢?我也读过一些关于摇摆绘画的文章,但我不明白为什么从不为摇摆调用 update()。你能解释一下为什么吗?

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

class CustomCanvas extends Canvas{ 
    ArrayList<Point> locations;        
    int locationsAdded;    
    Point oldLocation;
    Point location;
    Dimension dimension;
    CustomCanvas(Dimension dimension){  
        locations = new ArrayList<>();        
        this.dimension = dimension;   
        this.init();
        addListeners();
    }    
    private void init(){                          
        oldLocation= new Point(0, 0);
        location= new Point(0, 0);
    }
    public void paintLine(Graphics g, int x){
        Point p1 = (Point)locations.get(x);
        Point p2 = (Point)locations.get(x+1);
        g.drawLine(p1.x, p1.y, p2.x, p2.y);
        locationsAdded++;
    }
    @Override
    public void paint(Graphics g){          
        locationsAdded = 0;        
        g.setColor(Color.red);                  
        for(int i = locationsAdded; i < locations.size()-1; i++){
            paintLine(g, i);
        }             
    }
    public void update(Graphics g) {        
    //locationsAdded = 0;
        for (int i = locationsAdded; i < locations.size()-1; i++) {            
            paintLine(g, i);
        }
    }
    private void addListeners(){
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent me){                                   
                oldLocation = location;
                location = new Point(me.getX(), me.getY());
                locations.add(location);
                repaint();
            }
        }); 
    }

    @Override
    public Dimension getMinimumSize() {
        return dimension; 
    }
    @Override
    public Dimension getPreferredSize() {
        return dimension;
    }
}
class CustomFrame extends Panel {
    Panel displayPanel = new Panel(new BorderLayout());
    CustomCanvas canvas = new CustomCanvas(new Dimension(700, 700));        
    public CustomFrame(String titlu) {            
        canvas.setBackground(Color.white);
        displayPanel.add(canvas, BorderLayout.CENTER);            
        this.add(displayPanel);
    }  
}
public class AWTPainting {
    public static void main(String args[]) {
        CustomFrame panel = new CustomFrame("Test Paint");
        Frame f = new Frame();
        f.add(panel);
        f.pack();        
        f.setSize(700,700);                    
        f.show();                
    }
}
于 2012-04-13T21:55:38.510 回答
0

将您的布​​局设置为 Null 布局

于 2020-06-11T00:13:34.973 回答