最小化时窗口的内容消失 [英] Window's content disappears when minimized

查看:114
本文介绍了最小化时窗口的内容消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple class that draws a line when mouse dragging or a dot when mouse pressing(releasing).

当我最小化应用程序然后还原它时,窗口的内容消失了,最后一个点(像素)除外.我了解每次窗口更改时,方法super.paint(g)都会重新绘制背景,但是无论是否使用它,结果似乎都是相同的.两者之间的区别在于,当我不使用它时,在窗口上绘制的像素多于一个像素,但并不是我的全部绘画.我该如何解决?

When I minimize the application and then restore it, the content of the window disappears except the last dot (pixel). I understand that the method super.paint(g) repaints the background every time the window changes, but the result seems to be the same whether I use it or not. The difference between the two of them is that when I don't use it there's more than a pixel painted on the window, but not all my painting. How can I fix this?

这是课程.

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);
    }
}

推荐答案

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

Doesn't that mean I will draw all the points(instead of one) everytime I press or drag the mouse?

是的,但是@Dave的方法对于数千个节点而言完全令人满意,如 GraphPanel .除此之外,请考虑 flyweight模式 ,如 JTable渲染器,并说明了

Yes, but @Dave's approach is perfectly satisfactory for thousands of nodes, as may be seen in GraphPanel. Beyond that, consider the flyweight pattern, as used by JTable renderers and illustrated here.

附录:关注您的 AWTPainting 问题,以下变化可能说明

Addendum: Focusing on your AWTPainting questions, the variation below may illustrate the difference between System- and App-triggered Painting. As the mouse is dragged, repaint() invokes update(), which calls paint(); this is app-triggered. As you resize the window, only paint() is called (no red numbers are drawn); this is system-triggered. Note that there is a flicker when the mouse is released after resizing.

通常在清除并重绘整个组件的背景后才会发生闪烁:

Flickering typically occurs when the entire component's background is cleared and redrawn:

  4.如果该组件没有没有覆盖update(),则默认实现update()会清除该组件的背景(如果它不是轻量级组件),并只需调用paint().

 4. If the component did not override update(), the default implementation of update() clears the component's background (if it's not a lightweight component) and simply calls paint().

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;
    }
}

这篇关于最小化时窗口的内容消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆