求解~为什么界面repaint后之前画的东西都不见了,我想让它在之前画的基础上继续画
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import java.awt.Frame;
import org.eclipse.swt.awt.SWT_AWT;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.Point;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import com.jgoodies.forms.factories.DefaultComponentFactory;
public class showarea extends JPanel {
JRootPane jframe=null;Point point =null;
Drawing draw;
public showarea(JRootPane jr) {
super();
setBackground(Color.CYAN);
setSize(782, 526);
setLayout(null);
CoordinateArea coordinateArea = new CoordinateArea(jr);
coordinateArea.setBounds(0, 0, 782, 526);
add(coordinateArea);
validate();
}
}
class CoordinateArea extends Canvas{
Point point =null;
JRootPane controller;
public CoordinateArea(JRootPane controller){
super();
this.controller=controller;
System.out.println("2");
}
public boolean mouseDown(Event event ,int x, int y){
if(point==null){
point =new Point(x,y);
}else{
point.x=x;
point.y=y;
}
repaint();
return false;
}
void draw(Graphics2D g2d , Drawing i)
{
i.draw(g2d);
}
public void paint(Graphics g){
super.paint(g);
if(point!=null){
Graphics2D g2d=(Graphics2D) g;
Drawing draw=new fillCircle();
draw.x1=point.x;
draw.y1=point.y;
draw(g2d,draw);
}
}
}
class Drawing implements Serializable {
int x1,y1,x2,y2; //定义坐标属性
String s1; //定义字体的风格
void draw(Graphics2D g2d ){}//定义绘图函数
}
class Line extends Drawing//直线类
{
void draw(Graphics2D g2d) {
g2d.setPaint(Color.pink);// 为 Graphics2D 上下文设置 Paint 属性。
g2d.drawLine(x1, y1, x2, y2);// 画直线
super.draw(g2d);
}
}
class fillCircle extends Drawing{//实心圆类
void draw(Graphics2D g2d ){
g2d.setPaint(Color.pink);
g2d.fillOval(x1-15,y1-15,30,30);
}
}
class Word extends Drawing{//输入文字类
void draw(Graphics2D g2d ){
g2d.setPaint(Color.pink);
g2d.setFont(new Font("微软雅黑", Font.BOLD, 15));//设置字体
if(s1 != null)
g2d.drawString( s1, x1,y1);
}
}
java
把所有画的数据保留下来,在paint方法中重画一遍。