还好,不要拖动。用线程实现,再加上双缓存。
例子,你可以在一个JLabel中加入图片,然后让整个Label平滑移动。效果就出来了。我做过这种类似的动画。

解决方案 »

  1.   

    我是一个扑克游戏,让其中的几张移动该怎么做?
    还有,重画窗口是那个函数?!我是新人,不好意思。
    画图我用的paintComponent。不知道重画用那个。
      

  2.   

    给你个例子,自己看把
    import java.awt.*;
    public class MoveDemo extends java.applet.Applet implements Runnable{
    Image offScreenImg;
    Graphics offScreenG;
    MediaTracker tracker;
    Image walkerImgs[]=new Image[5];
    Image currentImg;
    int xpos,ypos=0;
    int walk_step=20;
    int delay=250;
    Thread runThread;
    Image bgImage;
    int applet_width,applet_height;
    int birdImg_width;
          public void init(){
    tracker=new MediaTracker (this);
    for(int i=0;i<walkerImgs.length;i++){
    walkerImgs[i]=getImage(getCodeBase(),  "images/bu"+i+".gif");
    tracker.addImage(walkerImgs[i],0);
    }
    bgImage=getImage(getCodeBase(),"images/"+"dune.gif");
    tracker.addImage(bgImage,0);
    applet_width=size().width;
    applet_height=size().height;
    try{ offScreenImg=createImage(applet_width,applet_height);
    offScreenG=offScreenImg.getGraphics();
    }catch(Exception e){offScreenG=null;}
    }
    public void start(){
    if(runThread==null){
    runThread=new Thread(this);
    runThread.start();
    }
    }
    public void stop(){
    if(runThread!=null){
    runThread.stop();
    runThread=null;
    }
    }
    public void run(){
    try{tracker.waitForID(0); }catch(InterruptedException e){return;}
    birdImg_width=walkerImgs[0].getWidth(this);
    int i=0;
    while(true){
    for(xpos=birdImg_width;xpos<=applet_width;xpos+=walk_step){
    currentImg=walkerImgs[i]; repaint();
    i=(i+1)%walkerImgs.length;
    try{Thread.sleep(delay);} catch(InterruptedException e){}
    }
    }
    }
    public void paint(Graphics g){g.drawImage(bgImage,0,0,this);
    g.drawImage(currentImg,xpos,ypos,this);
    }
    public void update(Graphics g){
    if(offScreenG!=null){paint(offScreenG);
    g.drawImage(offScreenImg,0,0,this);
    }
    else paint(g);
    }
    }