package showShape;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class WinApp extends Frame{
public static final int MAP_WIDTH = 800;
public static final int MAP_HEIGHT = 600;
private Image offScreenImage = null;
public static void main(String[] args) {
WinApp app = new WinApp();
app.lauchFrame();
}
/**初始化窗体信息*/
private void lauchFrame(){
this.setTitle("j2se图形显示模板");
this.setSize(MAP_WIDTH, MAP_HEIGHT);
this.setResizable(false); //是否可以调整大小
this.setBackground(new Color(187, 201, 229));
this.setVisible(true);
this.setLocation(800, 500);
new Thread(new ThreadPaint()).start();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
/**重绘屏幕*/
class ThreadPaint implements Runnable{
@Override
public void run() {
while(true){
try {
Thread.sleep(41);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void paint(Graphics g) {
}
/**加入缓冲区*/
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(MAP_WIDTH, MAP_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(new Color(187, 201, 229));
gOffScreen.fillRect(0, 0, MAP_WIDTH, MAP_HEIGHT);
gOffScreen.setColor(c);
this.paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
}