# Animation zieht "Spur" hinter sich her



## Shiby (26. Mrz 2014)

Klasse Knowledge


Spoiler





```
/**
 * Write a description of class Knowledge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Knowledge
{
    /**
     * 
     */
    private final Player player = new Player(100, 250);
    
    /**
     * 
     */
    private final Frame frame = new Frame(player);

    /**
     * 
     */
    public Knowledge()
    {        
        frame.makeBufferedStrategy();
        update();
    }

    /**
     * 
     */
    public void update()
    {
        long lastFrame = System.currentTimeMillis();

        while(true)
        {
            long thisFrame = System.currentTimeMillis();
            float timeSinceLastFrame = (float)((thisFrame - lastFrame))/1000f;
            lastFrame = thisFrame;
             
            player.update(timeSinceLastFrame);
            frame.repaint();      
           
            try
            {
                Thread.sleep(15);
            }
            catch(Exception e)
            {
            
            }
        }
    }
}
```





Klasse Player


Spoiler





```
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player
{    
    /**
     * 
     */
    private BufferedImage spriteSheet;

    /**
     * 
     */
    private final KeyHandler keyHandler = new KeyHandler();

    /**
     * 
     */
    private float positionX;

    /**
     * 
     */
    private float positionY;

    /**
     * 
     */
    private final int speed;

    /**
     * 
     */
    public final static int HEALTHPOINTS = 100;

    /**
     * 
     */
    private int currentHealthPoints;

    /**
     * 
     */
    private float animationTime = 0f;

    /**
     * 
     */
    public Player(final float pPositionX, final int pSpeed)
    {
        positionX = pPositionX;
        positionY = Frame.WORLDSIZEY - 40;
        speed = pSpeed;

        try
        {
            spriteSheet = ImageIO.read(getClass().getClassLoader().getResourceAsStream("zombieImages/Men.png"));
        }
        catch(Exception e)
        {

        }
    }

    /**
     * 
     */
    public void update(final float pTimeSinceLastFrame)
    {
        animationTime += pTimeSinceLastFrame;
        if(animationTime >= 1)
        {
            animationTime = 0;
        }

        
        if(keyHandler.getKeyLeft() == true)
        {
            positionX -= speed * pTimeSinceLastFrame;
        }

        if(keyHandler.getKeyRight() == true)
        {
            positionX += speed * pTimeSinceLastFrame;
        }

        if(keyHandler.getKeySpaceBar() == true)
        {

        }        
    }

    /**
     * 
     */
    public int getPositionX()
    {
        return (int)positionX;
    }

    /**
     * 
     */
    public int getPositionY()
    {
        return (int)positionY;
    }

    /**
     * 
     */
    public void setPositionX(final float pPositionX)
    {
        positionX = pPositionX;
    }

    /**
     * 
     */
    public void setPositionY(final float pPositionY)
    {
        positionY = pPositionY;
    }

    /**
     * 
     */
    public KeyHandler getKeyHandler()
    {
        return keyHandler;
    }

    /**
     * 
     */
    public BufferedImage getAppearance()
    {
        if(keyHandler.getKeyLeft() == true)
        {
            if(animationTime < 0.2)
            {
                return spriteSheet.getSubimage(50, 0, 50, 50);
            }

            if(animationTime < 0.4)
            {
                return spriteSheet.getSubimage(50, 50, 50, 50);
            }

            if(animationTime < 0.6)
            {   
                return spriteSheet.getSubimage(50, 100, 50, 50);
            }

            if(animationTime < 0.8)
            {
                return spriteSheet.getSubimage(50, 150, 50, 50);
            }
            else
            {
                return spriteSheet.getSubimage(50, 200, 50, 50);
            }
        }

        if(keyHandler.getKeyRight() == true)
        {
            if(animationTime < 0.2)
            {
                return spriteSheet.getSubimage(150, 0, 50, 50);
            }

            if(animationTime < 0.4)
            {
                return spriteSheet.getSubimage(150, 50, 50, 50);
            }

            if(animationTime < 0.6)
            {   
                return spriteSheet.getSubimage(150, 100, 50, 50);
            }

            if(animationTime < 0.8)
            {
                return spriteSheet.getSubimage(150, 150, 50, 50);
            }
            else
            {
                return spriteSheet.getSubimage(150, 200, 50, 50);
            }
        }
        
        return spriteSheet.getSubimage(50, 0, 50, 50);
    }
}
```





Klasse KeyHandler


Spoiler





```
import java.awt.event.*;

/**
 * 
 */
public class KeyHandler implements KeyListener
{  
    /**
     * 
     */
    private boolean keyLeft = false;

    /**
     * 
     */
    private boolean keyRight = false;    
    
    /**
     * 
     */
    private boolean spaceBar = false;

    /**
     * 
     */
    @Override
    public void keyPressed(KeyEvent pKeyEvent)
    {   
        if(pKeyEvent.getKeyCode() == KeyEvent.VK_A)
        {
            keyLeft = true;
        }

        if(pKeyEvent.getKeyCode() == KeyEvent.VK_D)
        {
            keyRight = true;
        }
        
         if(pKeyEvent.getKeyCode() == KeyEvent.VK_SPACE)
        {
            spaceBar = true;
        }
    }

    /**
     * 
     */
    @Override
    public void keyReleased(KeyEvent pKeyEvent)
    {
        if(pKeyEvent.getKeyCode() == KeyEvent.VK_A)
        {
            keyLeft = false;
        }

        if(pKeyEvent.getKeyCode() == KeyEvent.VK_D)
        {
            keyRight = false;
        }
        
         if(pKeyEvent.getKeyCode() == KeyEvent.VK_SPACE)
        {
            spaceBar = false;
        }
    }

    /**
     * 
     */
    @Override
    public void keyTyped(KeyEvent pKeyEvent)
    {

    }     

    /**
     * 
     */
    public boolean getKeyLeft()
    {
        return keyLeft;
    }

    /**
     * 
     */
    public boolean getKeyRight()
    {
        return keyRight;
    }
    
    /**
     * 
     */
    public boolean getKeySpaceBar()
    {
        return spaceBar;
    }
}
```




Klasse Frame


Spoiler





```
import javax.swing.*;
import java.awt.image.*;
import java.awt.*;

/**
 * Write a description of class Frame here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frame extends JFrame  
{ 
    /**
     * 
     */
    private final Player player;
    
    /**
     * 
     */
    private BufferStrategy bufferStrategy;
   
    /**
     * 
     */
    public static final int WORLDSIZEX = 800;

    /**
     * 
     */
    public static final int WORLDSIZEY = 600; 
    

    /**
     * 
     */
    public Frame(final Player pPlayer)
    { 
        player = pPlayer;
                
        setSize(WORLDSIZEX, WORLDSIZEY);  
        addKeyListener(player.getKeyHandler()); 

        initialize();
    }

     /**
     * 
     */
    public void makeBufferedStrategy()
    {
        createBufferStrategy(2);
        bufferStrategy = getBufferStrategy();
    }

    /**
     * 
     */
    public void initialize()
    {   
        setLocationRelativeTo(null);
        setResizable(false);
        setUndecorated(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);      
        setVisible(true);
    }

    /**
     * 
     */
    public void repaint()
    {   
        Graphics graphics = bufferStrategy.getDrawGraphics();

        draw(graphics);
        graphics.dispose();
        bufferStrategy.show();
    }

    /**
     * 
     */

    public void draw(Graphics pGraphics)
    {
        pGraphics.drawImage(player.getAppearance(), player.getPositionX(), player.getPositionY(), null);      
    }    
}
```




Hallo Leute,
ich wollte eigntlich ein kleines Männchen als Animation durch Tastendruck von a und d am unteren Fensterrand laufen lassen. Leider zieht die Animation immer eine Spur hinter sich her, kann mit jemand sagen warum. 
Im Anhang befidnet sich das SpriteSheet und ein Bild der fehlerhaften Animation.

LG Shiby


----------



## Bananabert (26. Mrz 2014)

Moin,

clearrect


----------



## Androbin (27. Mrz 2014)

Hallo Shiby,
das Problem ist, das du repaint() überschreibst und nicht paint().
Denn repaint() setzt normalerweise das Bild zurück und ruft dann paint() auf!
Benenne einfach die Methode repaint() in paint() um!
Aber nur die Methode an sich, nicht den Aufruf!

Hoffe, ich konnte helfen!


----------

