j2se可视化图形绘制模板(带缓冲区)

 

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);

    }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值