程序不会重新绘制

时间:2017-12-22 14:14:39

标签: java animation repaint

我正在尝试制作简单的步行动作/动画,但它只显示动画的最后一步。以下是涉及的代码:

这是测试keyPress事件的类(未显示):

public class Game extends JFrame{ //Contains the game's panel
    public static MainPanel mp = new MainPanel();
    public static Player player = new Player();
    private int wait = 100;

    public Game(){
        //initialize everything
        getContentPane().add( mp );
    }

    private void Wait(){
        try{
            Thread.sleep(wait);
        }catch(InterruptedException ex){
            Thread.currentThread().interrupt();
        }
    }

    private void up(){
        player.Up1();
        mp.Repaint();
        Wait();
        player.Up2();
        mp.Repaint();
        Wait();
        player.Up1();
        mp.Repaint();
        Wait();
        player.Up3();
        mp.Repaint();
        Wait();
        player.Up1();
        mp.Repaint();
    }
}

这是玩家类:

public class Player{
    public static BufferedImage pic=null;
    private static int characterNum;
    public Player(){
        //initializes player
        int characterNum;
    }

    public static void setChar( int character ){
        try {
            pic = ImageIO.read(new File("pics/character final/forwardsprite.png"));
        } catch (IOException e) {
            System.out.println ( "No boyDown image in Player" );
        }
        characterNum = character;
    }

    public static void Up1(){
        if( characterNum==1 ){
            try {
                pic = ImageIO.read(new File("pics/character final/backsprite.png"));
            } catch (IOException e) {
                 System.out.println ( "No boyDown image in Player" );
            }
        }
    }
    public static void Up2(){
        try {
            pic = ImageIO.read(new File("pics/character final/backspriteLeftstep.png"));
        } catch (IOException e) {
            System.out.println ( "No boyDown image in Player" );
        }
    }
    public static void Up3(){
        try {
            pic = ImageIO.read(new File("pics/character final/backspriteRightstep.png"));
        } catch (IOException e) {
            System.out.println ( "No boyDown image in Player" );
        }
    }

我认为问题在于重新粉刷MainPanel,但我花了数小时试图解决这个问题。我想我只需要另外一双眼睛来看它。

0 个答案:

没有答案