我有一个简单的类,当鼠标拖动时画一条线,或者当鼠标按下(释放)时画一个点。
当我将应用程序最小化,然后将其还原时,除了最后一个点(像素)外,窗口的内容都消失了。我理解的方法是 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);
}
}
你没有存储你正在绘制的点的状态。当面板重新绘制时,它只有最后绘制的点的信息。
对评论的回应。
你需要有一个点的集合,例如: 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
通常被称为显示列表。大多数图形程序都使用它们。
对评论的回应。
是的,我希望如此。我只是根据你的代码抛出一个想法,给你一个出发点。几乎可以肯定的是,完全按照我所描述的那样做是一个坏主意。
这不是意味着我每次按动或拖动鼠标时都会画出所有的点(而不是一个)吗?
是的,但是 @Dave 的方法对于成千上万的节点来说是完全令人满意的,这一点可以在 GraphPanel
. 除此之外,还要考虑 蝇头小楷用的 JTable
渲染器 图文并茂 此处.
增编。注重你的 AWTPainting
的问题,下面的变化可能会说明两者之间的区别。系统和应用程序触发的绘画. 随着鼠标的拖动。repaint()
调用 update()
,这就要求 paint()
;这是应用程序触发的。当你调整窗口大小时,只有 paint()
被调用(不画红色数字);这是系统触发的。请注意,有 是 当调整大小后释放鼠标时,会出现闪烁。
闪动通常发生在清除整个组件的背景并重新绘制时。
4. 如果该组件没有 不 凌驾于...之上
update()
的默认实现,即update()
清除组件的背景(如果它不是一个轻量级组件),并简单地调用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;
}
}
@Andrew, @Dave, @trashgod 嗨,我做了一些研究,最后,这是我得到的。如果我说的不对,请纠正我。你不能覆盖paint(),所以每次你需要做应用触发的绘画时,你都要调用repaint().Repaint()调用update(),它的默认行为是调用paint().update()用于增量绘画;这解释了当paint()做所有工作时屏幕闪烁的原因,这实际上意味着它在每一步都在画整个图像。然而,我的问题是,如果我在update方法中添加 "lovesAdded = 0",这意味着每次我拖动鼠标时,我都会绘制整个图像(就像在paint中一样),那么为什么它不像以前那样闪烁呢?我也读过一些关于在swift中绘制的东西,我不明白为什么update()对于swift从来不被调用。你能给我解释一下为什么吗?
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();
}
}
将你的布局设置为Null布局