一个简单的绘图程序 ,但是激活其他窗口 ,再点绘图窗口 ,原来的图像就不见了,
如何处理能实现 ,保留已经绘制的图形! 求高手指点:
--------------------------------------------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SimplePaint{
public static void main(String[] args){
JFrame frame=new JFrame("简单绘图");
Container pane=frame.getContentPane();
DrawingCanvas canvas=new DrawingCanvas();
PaintListener listener=new PaintListener();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
frame.setSize(200,200);
frame.pack();
frame.show();
}
}
class DrawingCanvas extends JComponent{
public Dimension getMinimumsize(){
return new Dimension(SIZE,SIZE);
}
public Dimension getPreferredsize(){
return new Dimension(SIZE,SIZE);
}
private int SIZE=1000;
}
class PaintListener implements MouseMotionListener{
public void mouseDragged(MouseEvent e){
DrawingCanvas canvas=(DrawingCanvas)e.getSource();
Graphics g=canvas.getGraphics();
g.fillOval(e.getX()-r,e.getY()-r,d,d);
};
public void mouseMoved(MouseEvent e){};
private int r=8,d=2*r;
}
-----------------------------------------------------------------------------------------------------