关于双重缓冲解决REPAINT闪烁问题
自己想做个小游戏,但是载入图片多了画面就就开始闪烁,BAIDU过后说双重缓冲能解决问题,但是无奈看不大懂 就高手指教。附上代码,应该怎么改。package Walk;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.Random;
import javax.swing.JFrame;
public class WalkMain extends JFrame implements KeyListener {
Image img;
Image back;
Image monsterimg;
BufferStrategy bstrategy;
int x = 80, y = 450, dir = 3, num = 0;
int index = 0;
int flag = 1;
Random rd = new Random();
boolean run1 = true;
public Image buff;
public Graphics buff_g;
MonsterMain[] monster = new MonsterMain[3];
public static void main(String args[]) {
new WalkMain();
}
// アップデート
public void update(Graphics g){
paint(g);
}
// Constructor
WalkMain() {
super("Character");
img = getToolkit().getImage("walk.png");
back = getToolkit().getImage("back.jpg");
for (int i = 0; i < monster.length; i++) {
monsterimg = getToolkit().getImage("MON.gif");
monster[i] = new MonsterMain(monsterimg, i, rd.nextInt(2));
}
addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.gray);
setSize(800, 610);
setVisible(true);
move();
}
public synchronized void paint(Graphics g) {
//Graphics kg = bstrategy.getDrawGraphics();
//g.clearRect(0, 0, 800, 610);
g.drawImage(back, 0, 0, this);
if (img != null) {
// yoff = 144;
if (dir == 1) {
g.drawImage(img, x, y, x + 144, y + 144, 144 * index, -144
+ 144 * dir, 144 + 144 * index, 144 * dir, this);
} else if (dir == 3) {
g.drawImage(img, x, y, x + 144, y + 144, 432 - 144 * index,
-144 + 144 * dir, 576 - 144 * index, 144 * dir, this);
} else if (dir == 0) {
}
}
}
// KeyEvent Listener
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) { // case KeyEvent.VK_UP : y=y-10; dir=0;
// break;
case KeyEvent.VK_RIGHT:
if (x < 680)
x = x + 10;
index = (index + 1) % 4;
dir = 3;
break;
// case KeyEvent.VK_DOWN : y=y+10; dir=2; break;
case KeyEvent.VK_LEFT:
if (x > -15)
x = x - 10;
index = (index + 1) % 4;
dir = 1;
break;
case KeyEvent.VK_UP:
run1 = true;
while (run1) {
this.getGraphics().drawImage(back, 0, 0, this);
this.getGraphics().drawImage(img, x, y - num, x + 144,
y + 144 - num, 0, -144 + 144 * dir, 144, 144 * dir,
this);
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (flag == 1) {
if (num < 60) {
num = num + 5;
} else {
flag = 0;
}
System.out.println("1111111111");
}
if (flag == 0) {
num = num - 5;
if (num <= 0) {
run1 = false;
flag = 1;
}
System.out.println("00000000000");
}
repaint();
}
break;
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void move() {
while (true) {
// this.getGraphics().drawImage(back, 0, 0, this);
repaint();
for (int i = 0; i < monster.length; i++) {
monster[i].draw(this.getGraphics(), this);
}
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
package Walk;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.util.Random;
public class MonsterMain {
int char_x, char_y;
Image bimage1;
int waittime;
boolean spkey = true;
public static final int SPEED = 4;
int imgIndex;//决定出现的是哪个怪,0,1,2分别代表蜗牛,红怪,蓝怪。
int direction;//方向,1表示从右向左,0表示从左向右
// Random rd;
MonsterMain(Image bi, int index, int imgIndex) {
bimage1 = bi;
syutsugen();
waittime = index * 20;
// this.imgIndex = new Random().nextInt(2);
this.imgIndex = imgIndex;
}
void syutsugen() {
direction = new Random().nextInt(2);
if (direction == 1) {// 1,0表示方向
char_x = 803;
char_y = 540;
} else {
char_x = -55;
char_y = 540;
}
waittime = waittime + (int) (Math.random() * 30);
System.out.println(waittime);
}
void draw(Graphics g, ImageObserver io) {
if (waittime > 0) {
waittime = waittime - 1;
return;
}
if (direction == 1) {
g.drawImage(bimage1, char_x, char_y, char_x + 55, char_y + 38,
55 * imgIndex, 0, 55 * imgIndex + 55, 38, io);
char_x = char_x - SPEED;
if (char_x < 4)
direction = 0;
}
if (direction == 0) {
g.drawImage(bimage1, char_x, char_y, char_x + 55, char_y + 38,
55 * imgIndex, 0, 55 * imgIndex + 55, 38, io);
char_x = char_x + SPEED;
if (char_x > 730)
direction = 1;
}
}
/*
* boolean isAtari(int x, int y){ int ax1 = x+12; int ay1 = y+12; int ax2 =
* x+36; int ay2 = y+36; int bx1 = char_x+12; int by1 = char_y+12; int bx2 =
* char_x+36; int by2 = char_y+36;
*
* if( (ax1<bx2) && (bx1<ax2) && (ay1<by2) && (by1<ay2) ){ return true; }
* else { return false; } }
*/
/*public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(800,610);
}
Graphics gOffScreen = offScreenImage.getGraphics();// 获取图片内的所有图形,形成虚拟窗口
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);// 设置屏幕窗口的颜色
gOffScreen.fillRect(0, 0, 800,610);
gOffScreen.setColor(c);
paint(gOffScreen); // 把虚拟窗口画在图片上
g.drawImage(offScreenImage, 0, 0, null);// 把图片画在窗口上
}*/
}
--------------------编程问答-------------------- JFrame没有实现双缓冲,想不闪就在JFrame上再加一个JPane,图象都画在JPane上,当JFrame调用repaint方法的时候,JPane就会自动更新的了,JPane已经实现了双缓冲的,这样就不闪了 --------------------编程问答-------------------- 请问具体要怎么做
--------------------编程问答-------------------- http://lfp001.iteye.com/blog/851459
看贴出的源码中drawHistogram和paintComponent方法。呵呵~~~
补充:Java , Java SE