怎么保留以前的图形
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics2D;
public class DrawLine extends JPanel {
JFrame frame;
int i=1;
public DrawLine()
{
frame=new JFrame ( "²âÊÔ ");
frame.setBounds(160,100,600,610);
frame.getContentPane().add(this);
frame.setBackground(Color.black);
frame.setVisible(true);
}
public void paint(Graphics g)
{
if(i==1)
{
g.setColor(Color.white);
g.drawLine(300,20,300,570);//ÊúÏß
g.drawLine(20,300,580,300);//ºáÏß
g.drawLine(300,20,290,40);
g.drawLine(300,20,310,40);//ÉϼýÍ·
g.drawLine(580,300,560,290);
g.drawLine(580,300,560,310);
g.drawString( "(0,0) ",310,290);
i=0;
}
}
public void update(Graphics g)
{
this.paint(g);
System.out.println( "dd ");
}
public static void main(String[] arg){
DrawLine dl = new DrawLine();
}
}
当窗口变大的时候, 不能保留以前的东西了 怎么办 ? 很着急 希望大家帮帮忙
------解决方案--------------------用JPanel里边的protected paintComponent(Graphics g)这个函数,在里面不要调用super.paintComponent(g);就可以了。
------解决方案--------------------把画板上的图形都保存到Vector中,每次画时都遍历一下