gpt4 book ai didi

Java 在调用 paint 方法时清除屏幕 - 如何避免这种情况?

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:27 25 4
gpt4 key购买 nike

我试图用 Java 在 Canvas 中绘制两条线,分别调用两个方法,但是当我绘制第二条线时,第一条线消失了(Java 清除了屏幕)。我怎样才能避免这种情况?我想看看这两条线。我看过绘画教程(如何在 Windows 上制作类似 Paint 的程序),其中用户使用鼠标绘制线条,当绘制一条线时,另一条线不会消失。他们只是调用 paint 方法,它不会清除屏幕。

如果有人能帮助我,我将不胜感激。谢谢。

查看类

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class CircuitTracePlotView extends JFrame {


private CircuitTracePlot circuitTracePlot;

public CircuitTracePlotView() {


this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.getContentPane().add(circuitTracePlot = new CircuitTracePlot(), BorderLayout.CENTER);
this.pack();
this.setSize(250,250);
this.setLocationRelativeTo(null);

this.setVisible(true);
circuitTracePlot.drawLine();
circuitTracePlot.drawOval();
}


}

class CircuitTracePlot extends Canvas {

private final static short LINE = 1;
private final static short OVAL = 2;
private int paintType;

private int x1;
private int y1;
private int x2;
private int y2;

public CircuitTracePlot() {
this.setSize(250,250);
this.setBackground(Color.WHITE);

}

private void setPaintType(int paintType) {
this.paintType = paintType;
}

private int getPaintType() {
return this.paintType;
}

public void drawLine() {
this.setPaintType(LINE);
this.paint(this.getGraphics());
}

public void drawOval() {
this.setPaintType(OVAL);
this.paint(this.getGraphics());
}

public void repaint() {
this.update(this.getGraphics());
}

public void update(Graphics g) {
this.paint(g);
}

public void paint(Graphics g) {
switch (paintType) {
case LINE:
this.getGraphics().drawLine(10, 10, 30, 30);
case OVAL:
this.getGraphics().drawLine(10, 20, 30, 30);
}


}


}

主类

import javax.swing.SwingUtilities;

import view.CircuitTracePlotView;

public class Main {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CircuitTracePlotView cr = new CircuitTracePlotView();
}
});
}
}

最佳答案

  • 你几乎不应该调用 paint(...)直接地。我一只手就能数出我需要执行此操作的次数。
  • 不要通过调用 getGraphics() 获取 Graphics 对象在一个组件上,因为它将返回一个非持久的 Graphics 对象。相反,要么在 BufferedImage 中绘制并在 paint 方法中显示它,要么在 paint 方法中绘制(如果是 AWT)。
  • 因为这是一个 Swing GUI,所以不要使用 AWT 组件来绘制。使用 JPanel 并覆盖 paintComponent(...)方法,而不是 paint(...)方法。否则,您将失去 Swing 图形的所有优势,包括自动双缓冲。
  • super.paintComponent(g)方法应在 paintComponent(Graphics g) 中调用覆盖,通常作为此方法内部的第一个方法调用。这让组件可以进行自己的内务处理绘画,包括删除需要删除的绘图。
  • 阅读有关 Swing 图形的教程,因为其中的大部分内容都有很好的解释。例如,请看这里:

编辑

  • 为了让您的图像持久保存,我建议您绘制到 BufferedImage,然后在您的 JPanel 的 paintComponent(...) 中显示该图像。方法。
  • 或者另一种选择是创建一个 Shape 对象集合,可能是一个 ArrayList<Shape>并用你想要绘制的形状填充它,然后在 paintComponent(...) 中方法将 Graphics 对象转换为 Graphics2D 对象并遍历 Shape 集合绘制每个形状 g2d.draw(shape)当你迭代时。

自从 Trash 发布了他的代码,...

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class CircuitTracePlot2 extends JPanel {

private static final int PREF_W = 250;
private static final int PREF_H = PREF_W;

private int drawWidth = 160;
private int drawHeight = drawWidth;
private int drawX = 10;
private int drawY = 10;
private PaintType paintType = PaintType.LINE;

public CircuitTracePlot2() {

}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

public void setPaintType(PaintType paintType) {
this.paintType = paintType;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (paintType == null) {
return;
}
switch (paintType) {
case LINE:
g.drawLine(drawX, drawY, drawWidth, drawHeight);
break;
case OVAL:
g.drawOval(drawX, drawY, drawWidth, drawHeight);
break;
case SQUARE:
g.drawRect(drawX, drawY, drawWidth, drawHeight);

default:
break;
}
}

private static void createAndShowGui() {
final CircuitTracePlot2 circuitTracePlot = new CircuitTracePlot2();

JFrame frame = new JFrame("CircuitTracePlot2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(circuitTracePlot);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);

int timerDelay = 2 * 1000;
new Timer(timerDelay , new ActionListener() {
private int paintTypeIndex = 0;

@Override
public void actionPerformed(ActionEvent arg0) {
paintTypeIndex++;
paintTypeIndex %= PaintType.values().length;
circuitTracePlot.setPaintType(PaintType.values()[paintTypeIndex]);
}
}).start();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

enum PaintType {
LINE, OVAL, SQUARE;
}

关于Java 在调用 paint 方法时清除屏幕 - 如何避免这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com