2 qq 31674229 qq_31674229 于 2016.04.11 18:26 提问

用java写了一个键盘监听,实现一张图片的上下左右移动,为什么图片会出现一闪一闪的,大神求解

package Fly;

import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**

  • 1.设置游戏窗口
  • 2.
  • @author q * */ public class OperateFly extends JFrame{ protected static int WIDTH=1000; protected static int HEIGHT=600; private Timer timer; private static int intervel=10; private Flyings flys=new Flyings(); public static BufferedImage fly1; public static BufferedImage background; public static BufferedImage begin_one; public static BufferedImage gameover; public static BufferedImage begin_two; public static BufferedImage begin_three;

static{
try{
fly1=ImageIO.read(new File("images/fyls1.png"));
background=ImageIO.read(new File("images/background.png"));
begin_one=ImageIO.read(new File("images/firstbegin.png"));
begin_two=ImageIO.read(new File("images/SecondBegin.png"));
begin_three=ImageIO.read(new File("images/ThreeBegin.png"));
}catch(Exception e){
e.printStackTrace();
}
}

public OperateFly(){
//窗口
JPanel panel=new JPanel();
this.setSize(WIDTH, HEIGHT);
this.add(panel);//将面板添加到窗口中
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);

this.addKeyListener(new KeyAdapter(){
    @Override
    public void keyPressed (KeyEvent e) {
            switch(e.getKeyCode()){
            //上移
            case KeyEvent.VK_W:
            case KeyEvent.VK_UP:
                System.out.println(e.getKeyCode());
                flys.moveup();
                break;
            //下移
            case KeyEvent.VK_S:
            case KeyEvent.VK_DOWN:
                flys.movedown();
                break;
            //左移
            case KeyEvent.VK_A:
            case KeyEvent.VK_LEFT:
                flys.moveleft();
                break;
            case KeyEvent.VK_D:
            case KeyEvent.VK_RIGHT:
                flys.moveright();
                break;
            default:
                System.out.println(e.getKeyCode());
            }   
    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }
});

}
/*

  • 画图
    /
    public void paint(Graphics g){
    g.drawImage(background,0,0,WIDTH,HEIGHT,null);
    paintfly(g);
    }
    /
    画飞行器*/
    public void paintfly(Graphics g){
    g.drawImage(fly1,flys.getX(),flys.getY(),flys.getWidth(),flys.getHeight(),null);
    }
    public static void main(String[] args){

    JFrame frame=new JFrame("FLY");
    OperateFly operatefly=new OperateFly();
    operatefly.action();
    }
    public void action(){
    timer=new Timer();
    timer.schedule(new TimerTask(){

    @Override
    public void run() {
    
        repaint();       //重画
    
    }},intervel,intervel);
    

    }
    }

下面是Flyings类

package Fly;

import java.awt.image.BufferedImage;

import FlyingObject.FlyingsObject;

/*

  • 飞行工具类
    */
    public class Flyings extends FlyingsObject{
    private static final int Speedx=2;
    private static final int Speedy=2;
    private int life;
    public Flyings(){
    image=OperateFly.fly1;
    life=3;
    width=600;
    height=400;
    x=598;
    y=356;
    }
    @Override
    public void moveup() {
    this.x=x;
    this.y-=Speedy;
    if(OutofBoundsY()){
    this.y=getY();
    }

    }
    @Override
    public void movedown() {
    this.x=x;
    this.y+=Speedy;
    if(OutofBoundsY()){
    this.y=getY();
    }
    }
    @Override
    public void moveleft() {
    this.x-=Speedx;
    this.y=y;
    if(OutofBoundsY()){
    this.x=getX();
    }
    }
    @Override
    public void moveright() {
    this.x+=Speedx;
    this.y=y;
    if(OutofBoundsY()){
    this.x=getX();
    }
    }
    /*上下越界判断*/
    public boolean OutofBoundsY(){
    return this.y>OperateFly.HEIGHT-getHeight()||this.y }
    /*左右越界判断*/
    public boolean OutofBoundsX(){
    return this.x>OperateFly.WIDTH-getWidth()||this.x<0;
    }
    }

4个回答

Meditator_hkx
Meditator_hkx   Rxr 2016.04.11 21:54

我没写过图片类的监听,不过原理应该都是一样的。我想问下,全部源代码都是你自己写的吗?还是借鉴了别的什么?

bbwolfcool
bbwolfcool   2016.04.11 22:48

图片的移动就会窗体重绘,怎么可能不闪,如果要不闪,要么用direct draw 2D,要么用矢量模型,比如flash,要么微软的slivelight

qinjisheng_11
qinjisheng_11   2016.04.11 23:30

在repaint和paint之间的update中写入双缓冲,先在一个图片画完所有所有该画的,然后再把图片上所有内容copy到界面上,消除闪烁现象。可以参考我的坦克大战内容,里面也有消除闪烁现象坦克大战

u013189927
u013189927   2016.04.13 22:12

用双缓冲技术可以解决闪烁问题,具体请看博文:http://blog.csdn.net/u013189927/article/details/51007304

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!