Java-最小化JFrame时,绘图消失了 [英] Java - Drawing dissappears when minimizing JFrame
问题描述
我正在尝试创建绘画软件,但最终出现了图形消失的问题.
I'm trying to create a paint software, but have ended up with the issue of dissappearing graphics.
我的课如下:
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);
}
}
该软件的绘图部分运行正常,但随着问题的发生,在最小化时该绘图消失了.
The drawing portion of the software is working fine, but as the issue informs, the drawing dissappears on minimizing.
我尝试解决覆盖Graphics g的问题,以及将所有鼠标点保存在Array中,但是没有运气.到处搜寻我找不到确切项目的解决方案,所以希望大家能帮忙.
I've tried working around with overriding Graphics g, as well as saving all the mouse points in an Array, but without luck. Searching around I was unable to find a solution to my exact project, so I hope you guys can help.
推荐答案
我修复了您的代码,以便在拖动鼠标时画一条线.释放时会画一条新线,然后再次按鼠标左键.
I fixed up your code so that you draw a line as long as you're dragging the mouse. You draw a new line when you release and press the left mouse button again.
这是我对代码所做的更改.
Here are the changes I made to the code.
-
我将JFrame代码包装在Runnable中,并通过调用SwingUtilities invokeLater方法来调用Swing GUI. invokeLater方法将创建和更新Swing GUI组件放在事件上派遣线程.我和Oracle都要求每个人都在Event Dispatch线程上启动他们的Swing应用程序.
I wrapped the JFrame code in a Runnable, and invoked the Swing GUI with a call to the SwingUtilities invokeLater method. The invokeLater method puts the creation and updating of the Swing GUI components on the Event Dispatch thread. Oracle and I demand that everyone starts their Swing application on the Event Dispatch thread.
我添加了paintComponent方法来绘制位置列表中的点.
I added the paintComponent method to paint the Points in the location List.
我修改了鼠标方法,以将点添加到位置列表中.
I modified the mouse methods to add points to the location List.
我将大小信息从JFrame移到了JPanel.我们不在乎JFrame的大小.我们确实关心绘图面板的尺寸.
I moved the size information from the JFrame to the JPanel. We don't care about the size of the JFrame. We do care about the size of the drawing panel.
代码如下:
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);
}
}
这篇关于Java-最小化JFrame时,绘图消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!