在Jpanel中怎么不让窗口重绘
简单的画图程序,我在画图时记住点,重绘时在画一遍,好像不行,帮忙看看,有什么好办法?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ScribbleDemo extends JFrame {
public ScribbleDemo() {
// Create a PaintPanel and add it to the content pane
getContentPane().add(new ScribblePanel(), BorderLayout.CENTER);
}
/** Main method */
public static void main(String[] args) {
ScribbleDemo frame = new ScribbleDemo();
frame.setTitle( "ScribbleDemo ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
// ScribblePanel for scribbling using the mouse
class ScribblePanel extends JPanel
implements MouseListener, MouseMotionListener {
final int CIRCLESIZE = 20; // Circle diameter used for erasing
private Point lineStart = new Point(0, 0); // Line start point
Point []p1=new Point[200];
int i=0;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Point start=p1[0];
Point end=new Point();
for(int l=1;l <i;i++)
{
end=p1[l];
g.drawLine(start.x,start.y,end.x,end.y);
start=p1[l];
}
}
public ScribblePanel() {
// Register listener for the mouse event
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
lineStart.move(e.getX(), e.getY());
p1[i]=lineStart;
i++;
}
public void mouseDragged(MouseEvent e) {
// Create a Graphics object for drawing
Graphics g = getGraphics();