java问题,用update()函数后,绘制移动的小人会残留轨迹
用applet写的一个小程序,目的是想实现一个吃豆人移动时的动画效果原本是想用update()函数消除图片的闪烁但是使用后运行applet,吃豆人移动后无法消除移动过后的...
用applet写的一个小程序,目的是想实现一个吃豆人移动时的动画效果
原本是想用update()函数消除图片的闪烁
但是使用后运行applet,吃豆人移动后无法消除移动过后的图像了
源码如下 ,忘高人指点
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class MutiThreadApplet extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thisThread = null, kicker = null;
String Message = "HellO";
Font font = new Font("TimesRoman", Font.BOLD, 18);
int x, y, m, n;
Image[] imgs;
int nimgs = 4, loopslot = 0, off;
public void init() {
x = getSize().width / 2;
y = getSize().height / 2;
imgs = new Image[nimgs];
for (int i = 1; i < nimgs; i++) {
imgs[i] = getImage(getDocumentBase(), "../img/" + i + ".png");
}
Image offScreen = createImage(getSize().width, getSize().height);
Graphics offScreenGC = offScreen.getGraphics();
offScreenGC.drawImage(imgs[loopslot], x, y, this);
}
public void KEY() {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_A:
m = 5;
n = 0;
System.out.println("左");
break;
case KeyEvent.VK_D:
m = -5;
n = 0;
System.out.println("右");
break;
case KeyEvent.VK_W:
n = 5;
m = 0;
System.out.println("上");
break;
case KeyEvent.VK_S:
n = -5;
m = 0;
System.out.println("下");
break;
}
}
});
}
public void start() {
if (thisThread == null) {
thisThread = new Thread(this);
thisThread.start();
}
if (kicker == null) {
kicker = new Thread(this);
kicker.start();
}
}
public void run() {
KEY();
while (true) {
y = y - n;
x = x - m;
if (x == -50)
x = getSize().width;
else if (x == getSize().width)
x = -50;
else if (y == getSize().height)
y = -50;
else if (y == -50)
y = getSize().height;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
loopslot = (loopslot + 1) % imgs.length;
}
}
public void stop() {
thisThread.stop();
thisThread = null;
kicker.stop();
kicker = null;
}
public void paint(Graphics g) {
g.setFont(font);
g.drawString(Message, x, y);
if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] != null)) {
g.drawImage(imgs[loopslot], x, y, this);
}
}
public void update(Graphics g) {
paint(g);
}
public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
if ((flags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0) {
if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] == img)) {
repaint();
}
}
return (flags & (ALLBITS | ERROR)) == 0;
}
} 展开
原本是想用update()函数消除图片的闪烁
但是使用后运行applet,吃豆人移动后无法消除移动过后的图像了
源码如下 ,忘高人指点
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class MutiThreadApplet extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
Thread thisThread = null, kicker = null;
String Message = "HellO";
Font font = new Font("TimesRoman", Font.BOLD, 18);
int x, y, m, n;
Image[] imgs;
int nimgs = 4, loopslot = 0, off;
public void init() {
x = getSize().width / 2;
y = getSize().height / 2;
imgs = new Image[nimgs];
for (int i = 1; i < nimgs; i++) {
imgs[i] = getImage(getDocumentBase(), "../img/" + i + ".png");
}
Image offScreen = createImage(getSize().width, getSize().height);
Graphics offScreenGC = offScreen.getGraphics();
offScreenGC.drawImage(imgs[loopslot], x, y, this);
}
public void KEY() {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_A:
m = 5;
n = 0;
System.out.println("左");
break;
case KeyEvent.VK_D:
m = -5;
n = 0;
System.out.println("右");
break;
case KeyEvent.VK_W:
n = 5;
m = 0;
System.out.println("上");
break;
case KeyEvent.VK_S:
n = -5;
m = 0;
System.out.println("下");
break;
}
}
});
}
public void start() {
if (thisThread == null) {
thisThread = new Thread(this);
thisThread.start();
}
if (kicker == null) {
kicker = new Thread(this);
kicker.start();
}
}
public void run() {
KEY();
while (true) {
y = y - n;
x = x - m;
if (x == -50)
x = getSize().width;
else if (x == getSize().width)
x = -50;
else if (y == getSize().height)
y = -50;
else if (y == -50)
y = getSize().height;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
loopslot = (loopslot + 1) % imgs.length;
}
}
public void stop() {
thisThread.stop();
thisThread = null;
kicker.stop();
kicker = null;
}
public void paint(Graphics g) {
g.setFont(font);
g.drawString(Message, x, y);
if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] != null)) {
g.drawImage(imgs[loopslot], x, y, this);
}
}
public void update(Graphics g) {
paint(g);
}
public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
if ((flags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0) {
if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] == img)) {
repaint();
}
}
return (flags & (ALLBITS | ERROR)) == 0;
}
} 展开
展开全部
去找一个马士兵的坦克大战视频,有一张有讲怎么消除移动时候的拖影,大概思路是使用Graphics 在一个image层上画图,等画完后再把整个image层显示出来。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
centerPanel.removeAll();
centerPanel.setVisible(false);
add(...);
centerPanel.repaint();
centerPanel.setVisible(true);
centerPanel.setVisible(false);
add(...);
centerPanel.repaint();
centerPanel.setVisible(true);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询