두 개의 행을 별도로 호출하는 Java의 Canvas에 두 개의 선을 그려야하지만 두 번째 선을 그릴 때 첫 번째 선이 사라집니다 (Java는 화면을 지 웁니다). 어떻게 피할 수 있습니까? 나는 두 줄을보고 싶다. 필자는 사용자가 마우스를 사용하여 선을 그리고 한 선을 그리면 다른 선이 사라지지 않는 페인트 자습서 (Windows에서 그림판 같은 프로그램을 만드는 방법)를 보았습니다. 그들은 단지 paint 메소드를 호출하고 화면을 지우지 않습니다.페인트 방법을 호출 할 때 Java 화면이 지워집니다 - 어떻게 피할 수 있습니까?
누구든지 나를 도와 주시면 감사하겠습니다. 감사합니다. .
보기 클래스
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);
}
}
}
Main 클래스
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();
}
});
}
}
+1 [sscce] (http://sscce.org/). – trashgod