java绘制缓存图像示例

 用java绘制图像时候,为了速度,需要缓存图像。写一个示例代码,供参考。

 

import  java.awt.Color;
import  java.awt.Container;
import  java.awt.Graphics;
import  java.awt.Graphics2D;
import  java.awt.Image;
import  java.awt.Transparency;
import  java.awt.event.WindowAdapter;
import  java.awt.event.WindowEvent;
import  java.awt.image.BufferedImage;

import  javax.swing.JFrame;
import  javax.swing.JPanel;

class  MyPanel  extends  JPanel {
    
private int px = 400;
    
private Image newImage = null;
    
    
public MyPanel(){
        
super();
        
this.setSize(400400);
        
final MyPanel t = this;
        
//内部线程绘制图,具体应用中,一般由外部控制
        new Thread(){
            
public void run(){
                
try{
                    
while(true){
                        t.addPX();
                        t.repaint();
                        
this.sleep(10);
                    }

                }
catch(Exception e){
                    e.printStackTrace();
                }

            }

        }
.start();
    }

    
    
public void addPX(){
        
        
if(px>400){
            px 
= 0;
            
//随几绘制背景图
            newImage = new BufferedImage(
                    
400,
                    
400,
                Transparency.BITMASK);
            Graphics2D g 
= (Graphics2D)newImage.getGraphics();
            g.setColor(Color.red);
            
int t1 = (int)(Math.random()*300);
            
int t2 = (int)(Math.random()*300);
            
int t3 = (int)(Math.random()*300);
            
int t4 = (int)(Math.random()*300);
            
int t5 = (int)(Math.random()*300);
            
int t6 = (int)(Math.random()*300);
            
int t7 = (int)(Math.random()*300);
            
int t8 = (int)(Math.random()*300);
            g.fill3DRect(t1, t2, t3, t4, 
true);
            g.setColor(Color.green);
            g.fillOval(t5, t6, t7, t8);
            g.dispose();
        }

        px 
++;
    }

    
    
public void paint(Graphics gt)
    
{
        
super.paint(gt);
        
if(newImage!=null){
            
//绘制缓存图片
            gt.drawImage(newImage,0,0,this);
        }

        gt.setColor(Color.blue);
        gt.drawLine(
0,0,px,400);
    }

    
    
}


public   class  Main  extends  JFrame {
    
public Main(){
        
super("测试页面");
        
this.setSize(400,400);
        
this.setLocation(100,100);
        
        Container con
=this.getContentPane();
        con.setLayout(
null);
        
        MyPanel mp 
= new MyPanel();
        mp.setLocation(
00);
        con.add(mp);
        
        
//添加关闭退出
        this.addWindowListener(
                
new WindowAdapter(){
                    
public void windowClosing(WindowEvent event)
                    
{
                        System.exit(
0);
                    }

                }

            );
    }

    
    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        Main view = new Main();
        view.setVisible(
true);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值