码迷,mamicode.com
首页 > 编程语言 > 详细

Java双缓冲绘图

时间:2015-11-30 20:18:53      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

import java.awt.*;
import javax.swing.*;

public class skyplane {
    public static void main(String[] args) {

        JFrame w = new JFrame();
        w.setSize(824, 446);

        MyPanel mp = new MyPanel();
        w.add(mp);

        Thread t = new Thread(mp);
        t.start();

        w.show();

    }
}

class MyPanel extends JPanel implements Runnable {
    Graphics g1;
    Image buffer, plane, sky;
    int x = 10;
    int y = 400;

    public void paint(Graphics g) {
        super.paint(g);

        buffer = createImage(getWidth(), getHeight());// 创建图像缓冲区
        g1 = buffer.getGraphics(); //创建绘图区
        plane = new ImageIcon("image/plane.png").getImage(); //获取图像
        sky = new ImageIcon("image/sky.png").getImage(); //获取图像

        g1.drawImage(sky, 0, 0, this); //将图像先画到缓冲区
        g1.drawImage(plane, x, y, this); //将图像先画到缓冲区
        g.drawImage(buffer, 0, 0, this); //将缓冲区里的图像画出
    }

    public void run() {
        while (true) {
            if (x == 800)
                x = 10;
            else
                x += 2;
            if (y == 0)
                y = 400;
            else
                y--;
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }
            repaint();
        }
    }

}

 

Java双缓冲绘图

标签:

原文地址:http://www.cnblogs.com/zuferj115/p/5008154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!