Java - 最小化JFrame时绘图消失

时间:2016-04-11 13:50:08

标签: java jframe

我正在尝试创建一个绘图软件,但最终出现了消失的图形问题。

我的课程如下:

public class CanvasFrame extends JPanel {

    private Point lastMousePoint;
    ArrayList<Point> location = new ArrayList<Point>();


    public CanvasFrame() {


        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                lastMousePoint = new Point (e.getX(), e.getY());
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                Graphics g = getGraphics();
                g.drawLine(lastMousePoint.x, lastMousePoint.y, e.getX(), e.getY());
                lastMousePoint = new Point(e.getX(), e.getY());
                g.dispose();
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Drawing with friends");
         frame.getContentPane().add(new CanvasFrame(), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }
}

该软件的绘图部分工作正常,但问题通知时,绘图在最小化时消失。

我尝试过重写图形g,以及保存数组中的所有鼠标点,但没有运气。搜索我无法找到我的确切项目的解决方案,所以我希望你们能帮忙。

1 个答案:

答案 0 :(得分:0)

我修改了你的代码,只要你拖动鼠标就画一条线。在释放时绘制一条新线并再次按下鼠标左键。

Drawing With Friends

以下是我对代码所做的更改。

  1. 我将JFrame代码包装在Runnable中,并通过调用SwingUtilities invokeLater方法调用Swing GUI。 invokeLater方法在Event Dispatch thread上创建和更新Swing GUI组件。 Oracle和我要求每个人在Event Dispatch线程上启动他们的Swing应用程序。

  2. 我添加了paintComponent方法来绘制位置List中的Points。

  3. 我修改了鼠标方法,将点添加到位置List。

  4. 我将大小信息从JFrame移动到JPanel。我们不关心JFrame的大小。我们关心绘图面板的大小。

  5. 以下是代码:

    package com.ggl.testing;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class CanvasFrame extends JPanel {
    
        private static final long serialVersionUID = -2454929744982913302L;
    
        private List<Point> location = new ArrayList<Point>();
    
        public CanvasFrame() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    location.clear();
                    location.add(e.getPoint());
                }
            });
    
            addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    location.add(e.getPoint());
                    repaint();
                }
            });
    
            setPreferredSize(new Dimension(800, 600));
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            if (location.size() <= 0) {
                return;
            }
    
            Point p = location.get(0);
            for (int i = 1; i < location.size(); i++) {
                Point q = location.get(i);
                g.drawLine(p.x, p.y, q.x, q.y);
                p = q;
            }
        }
    
        public static void main(String[] args) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Drawing with friends");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new CanvasFrame(), BorderLayout.CENTER);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            };
            SwingUtilities.invokeLater(runnable);
        }
    }