极品飞车14闪屏9游戏闪屏。怎么解决

Java解决游戏界面闪屏 - 博客频道 - CSDN.NET
YQS_Love的博客
分类:Java
一、问题描述
我们在做有关于图形绘制方面的问题非常之多。比如,有时我们用普通的方法去绘制图形,会产生闪屏的现象,导致我们所做的游戏或者是别的项目效果非常差,这完全不是我们想要的结果。那么,有没有一种技术,实现不闪屏的效果,特别是在多线程环境下。***是有的,这就是Java的什么技术----双缓冲技术。
图1 双缓冲技术效果图
二、实现方法
1、首先,我们定义一个类继承JFrame,要自定义绘图,我们就要实现JFrame的paint()方法,如下:
public void paint(Graphics g) {
2、创建一个缓冲区画笔;
public BufferedImage(int width,
int height,
int imageType)
缓冲区的宽度
缓冲区的高度
缓冲图形模式
BufferedImage paint = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = paint.getGraphics();
3、将你想要绘制的图形绘制在缓冲区内;
// 将想要绘制的图形绘制到缓冲区
g2.drawImage(GameImage.getInstance().game_main_bg, 0, 0, width, height,
4、将缓冲区的图形绘制到显示面板上;
g.drawImage(paint, 0, 0, this);
经过上面四步之后,我们就解决了闪屏的问题,现在放心大胆的去做你的游戏吧!
三、项目源码
由于项目过大,源码只给出了双缓冲技术得用法,具体的实现并没有做。如果想验证源码的,可以联系我要项目所用到的图片,也可以上网查找。
由于技术和能力有限,文中难免会有错误之处,还望指正,谢谢合作!
1、程序入口类Main.java
* 主函数入口
public class Main {
public static void main(String[] args) {
new MainGameFrame();
2、游戏主界面类MainGameFrame.java
* 游戏主面板
public class MainGameFrame extends JFrame implements Runnable {
private int width = 1200;
private int height = 600;
private BufferedImage clickG
private List&PlantAutoMoveInfo& allP
private boolean isRunnableS
public MainGameFrame() {
isRunnableStart = true;
new Thread(this).start();
newPlant();
* 创造植物
private void newPlant() {
allPlant = new ArrayList&PlantAutoMoveInfo&();
PlantAutoMoveInfo plantObj = new PlantAutoMoveInfo(new Point(300, 100),
Constants.SUNFLOWER, GameImage.getInstance().sunflower);
allPlant.add(plantObj);
plantObj = new PlantAutoMoveInfo(new Point(300, 250),
Constants.SUNFLOWER, GameImage.getInstance().sunflower);
allPlant.add(plantObj);
plantObj = new PlantAutoMoveInfo(new Point(400, 100), Constants.PEA,
GameImage.getInstance().pea);
allPlant.add(plantObj);
plantObj = new PlantAutoMoveInfo(new Point(400, 250), Constants.PEA,
GameImage.getInstance().pea);
allPlant.add(plantObj);
plantObj = new PlantAutoMoveInfo(new Point(500, 100), Constants.NUT,
GameImage.getInstance().nut);
allPlant.add(plantObj);
plantObj = new PlantAutoMoveInfo(new Point(500, 250), Constants.NUT,
GameImage.getInstance().nut);
allPlant.add(plantObj);
* 绘制图像
public void paint(Graphics g) {
BufferedImage paint = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
Graphics g2 = paint.getGraphics();
g2.drawImage(GameImage.getInstance().game_main_bg, 0, 0, width, height,
for (PlantAutoMoveInfo obj : allPlant) {
g2.drawImage(obj.getShowImage(), (int) obj.getPos().getX(),
(int) obj.getPos().getY(), this);
g.drawImage(paint, 0, 0, this);
private void init() {
this.setTitle("植物大战僵尸");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLocation(200, 200);
this.setSize(width, height);
this.setVisible(true);
* 当游戏界面图形变化时,我们要不断的刷新显示
public void run() {
while (isRunnableStart) {
this.repaint();
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
3、图片获取类GameImage.java
* 获取图片
public class GameImage {
public static List&BufferedImage&
public static List&BufferedImage&
public static List&BufferedImage&
public static BufferedImage game_main_
private static GameImage instance = null;
* 提供对外接口,并加上同步锁
public static GameImage getInstance() {
if(instance == null){
synchronized(GameImage.class){
if(instance == null){
instance = new GameImage();
GameImage(){
sunflower = new ArrayList&BufferedImage&();
nut = new ArrayList&BufferedImage&();
pea = new ArrayList&BufferedImage&();
game_main_bg = ImageIO.read(new File("image\\bk1.jpg"));
for(int i=1;i&=11;i++){
nut.add(ImageIO.read(new File("image\\plant\\p_3_0"+i+".png")));
if(i &= 8){
sunflower.add(ImageIO.read(new File("image\\plant\\p_1_0"+i+".png")));
pea .add(ImageIO.read(new File("image\\plant\\p_2_0"+i+".png")));
} catch (IOException e) {
e.printStackTrace();
4、植物信息保存类PlantAutoMoveInfo.java
* 植物信息业务类
public class PlantAutoMoveInfo implements Runnable{
private boolean isS
private BufferedImage showI
private int
private int
private int SUNTIME;
private int tempT
private boolean isP
private List&BufferedImage& plantI
public PlantAutoMoveInfo(Point pos,int type,List&BufferedImage& gameImage) {
this.pos =
this.plantImage = gameI
SUNTIME = Constants.PRODUCTION_SUNNING_TIME;
index = 0;
tempTime = 0;
showImage = plantImage.get(index);
this.type =
isStart = true;
isProduction = false;
new Thread(this).start();
public PlantAutoMoveInfo() {
public void stopSunflowerThread(boolean bool){
public boolean isProduction() {
return isP
public int getType() {
public void setType(int type) {
this.type =
public void setProduction(boolean isProduction) {
this.isProduction = isP
public Point getPos() {
public void setPos(Point pos) {
this.pos =
public boolean isStart() {
return isS
public void setStart(boolean isStart) {
this.isStart = isS
public BufferedImage getShowImage() {
return showI
public void setShowImage(BufferedImage showImage) {
this.showImage = showI
public int getTempTime() {
return tempT
public void setTempTime(int tempTime) {
this.tempTime = tempT
public void run() {
while(isStart){
showImage = plantImage.get(index % plantImage.size());
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
if(type == Constants.SUNFLOWER && !isProduction){
tempTime++;
if((tempTime * 150) &= SUNTIME * 1000){
isProduction = true;
6、常量类Constants.java
public class Constants {
public final static int SUNFLOWER = 0x0002;
public final static int PEA = 0x0004;
public final static int NUT = 0x0008;
public final static int PRODUCTION_SUNNING_TIME = 0;
排名:千里之外
(9)(4)(1)(1)(5)极品飞车9。自己做的漂移视频,还是新手。还闪屏。下次努力_土豆_高清视频在线观看

参考资料

 

随机推荐