麻烦各位大虾帮我分析下下面的代码,就是运行程序后在界面画了几条线,然后把它最小化后又最大化,怎么原先画的几条线没有重绘出来
import java.awt.event.*;import java.awt.*;
import javax.swing.*;
import java.util.*;
public class DrawLine1 extends Frame
{
Image oimg = null;
Graphics og = null;
int orgX;
int orgY;
int endX;
int endY;
public void paint(Graphics g)
{
g.drawImage(oimg,0,0,this);
}
public DrawLine1()
{
setSize(400,400);
setTitle("DrawLine1");
setVisible(true);
Dimension d = getSize();
Image oimg = createImage(d.width,d.height);
Graphics og = oimg.getGraphics();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
Graphics og = getGraphics();
og.setColor(Color.RED);
og.setFont(new Font("null",Font.ITALIC|Font.BOLD,30));
og.drawLine(orgX,orgY,e.getX(),e.getY());
og.drawString(orgX + "," + orgY,orgX,orgY);
og.drawString(e.getX() + "," + e.getY(),e.getX(),e.getY());
}
});
}
public static void main(String[] args) {
DrawLine1 mainFrame = new DrawLine1();
}
}
--------------------编程问答-------------------- 1,窗口的一些动作是需要重绘的,所以我们需要保存某些状态
--------------------编程问答-------------------- 每次都要完全重绘界面上所有的对象,如果漏了,就会看不到
/**
* CSND-画线
* @version 2012-2-25
*/
public class DrawLine1 extends Frame {
/** 存储LineModel */
private List<LineModel> lineModelArr = new ArrayList<LineModel>();
private Image oimg = null;
private Graphics og = null;
private int orgX;
private int orgY;
/**
* Constructor
*/
public DrawLine1() {
setSize(400, 400);
setTitle("DrawLine1");
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e) {
Graphics og = getGraphics();
og.setColor(Color.RED);
og.setFont(new Font("null", Font.ITALIC | Font.BOLD, 30));
LineModel lmInst = new LineModel(orgX, orgY, e.getX(), e.getY());
lmInst.drawLine(og);
lineModelArr.add(lmInst);
// og.drawLine(orgX, orgY, e.getX(), e.getY());
og.drawString(orgX + "," + orgY, orgX, orgY);
og.drawString(e.getX() + "," + e.getY(), e.getX(), e.getY());
}
});
}
/*
* (non-Javadoc)
* @see java.awt.Container#paintComponents(java.awt.Graphics)
*/
@Override
public void paint(Graphics g) {
super.paint(g);
for (Iterator<LineModel> iterator = lineModelArr.iterator(); iterator.hasNext();) {
iterator.next().drawLine(g);
}
}
/**
* 测试入口
* @param args 参数列表
*/
public static void main(String[] args) {
DrawLine1 mainFrame = new DrawLine1();
}
/**
* 抽象画线模型
* @author WangYanCheng
* @version 2012-2-25
*/
private class LineModel {
/** 存储坐标 */
private int fX, fY, sX, sY;
/**
* Constructor
* @param orgX
* @param orgY
* @param endX
* @param endY
*/
public LineModel(int orgX, int orgY, int endX, int endY) {
this.fX = orgX;
this.fY = orgY;
this.sX = endX;
this.sY = endY;
}
/**
* 绘制
* @param g Graphics
*/
public void drawLine(Graphics g) {
g.drawLine(fX, fY, sX, sY);
}
}
}
补充:Java , Java SE