#Animation Problems

1 messages · Page 1 of 1 (latest)

tardy nexus
#

Hi im learning swing and i get an error that sometimes the animation flikers. I dont really know the reason so if someone could help ?

lean meadowBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

There are a few common reasons why animations may flicker in Java Swing:

  1. Inefficient rendering: Make sure that you are using double buffering to reduce flickering. Double buffering involves drawing the animation off-screen and then displaying it all at once, which can help prevent flickering.

  2. Improper timing: Make sure that you are updating and repainting your animation at a consistent rate. If the updates are happening too quickly or too slowly, it can cause flickering.

  3. Overlapping components: If you have multiple components overlapping each other in your animation, it can cause flickering as the components are redrawn.

  4. Hardware acceleration: Sometimes hardware acceleration settings on your computer can cause issues with animations in Java Swing. Try disabling hardware acceleration to see if it improves the flickering.

  5. Memory issues: If your animation is using a lot of memory, it can slow down the rendering process and cause flickering. Make sure to optimize your code and use resources efficiently.

By addressing these common issues, you should be able to reduce or eliminate the flickering in your Java Swing animations.

placid cove
tardy nexus
#

package entities;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;

import static utilz.Constants.PlayerConstants.;
import static utilz.Constants.Directions.
;

public class Player extends Entity {

private BufferedImage[][] playerAnimation;
private int animationIndex, animationTick, animationSpeed = 25;
private int playerAction = IDLE;
private int playerDirection = -1;
private boolean moving = false;

public Player(float x, float y) {
    super(x, y);
    loadAnimation();
}

public void update() {
    updateAnimation();
    setAnimation();
    updatePosition();
    
}

public void render(Graphics g) {
    g.drawImage(playerAnimation[playerAction][animationIndex], (int) x, (int) y, 168, 168, null);
    
}

private void updateAnimation() {
    animationTick++;

    if (animationTick >= animationSpeed) {
        animationTick = 0;
        animationIndex++;
        if (animationIndex >= getSpriteAmount(playerAction)) {
            animationIndex = 0;
        }
    }
}

public void setDirection(int direction) {
    this.playerDirection = direction;
    moving = true;
}

public void setMoving(boolean moving) {
    this.moving = moving;
}

private void setAnimation() {
    if (moving) {
        playerAction = WALKING;
    } else {
        playerAction = IDLE;
    }
}
#

public void setMoving(boolean moving) {
this.moving = moving;
}

private void setAnimation() {
    if (moving) {
        playerAction = WALKING;
    } else {
        playerAction = IDLE;
    }
}

private void updatePosition() {
    if (moving) {
        switch (playerDirection) {
            case UP:
                y -= 1;
                break;
            case DOWN:
                y += 1;
                break;
            case LEFT:
                x -= 1;
                break;
            case RIGHT:
                x += 1;
                break;
            default:
                break;
        }
    }
}

private void loadAnimation() {
    InputStream is = getClass().getResourceAsStream("/player_animations.png");
    try {
        BufferedImage image = ImageIO.read(is);
        playerAnimation = new BufferedImage[15][12];

        for (int j = 0; j < playerAnimation.length; j++) {
            for (int i = 0; i < playerAnimation[j].length; i++) {
                playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}

placid cove
lean meadowBOT
#

Please use this format for posting code:

```java
// Example java program
int value = 5;
System.out.println(value);
```

Which results in:

// Example java program
int value = 5;
System.out.println(value);

For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.

tardy nexus
#

oh okey

#
package entities;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;

import static utilz.Constants.PlayerConstants.;
import static utilz.Constants.Directions.;

public class Player extends Entity {

    private BufferedImage[][] playerAnimation;
    private int animationIndex, animationTick, animationSpeed = 25;
    private int playerAction = IDLE;
    private int playerDirection = -1;
    private boolean moving = false;

    public Player(float x, float y) {
        super(x, y);
        loadAnimation();
    }

    public void update() {
        updateAnimation();
        setAnimation();
        updatePosition();

    }

    public void render(Graphics g) {
        g.drawImage(playerAnimation[playerAction][animationIndex], (int) x, (int) y, 168, 168, null);

    }

    private void updateAnimation() {
        animationTick++;

        if (animationTick >= animationSpeed) {
            animationTick = 0;
            animationIndex++;
            if (animationIndex >= getSpriteAmount(playerAction)) {
                animationIndex = 0;
            }
        }
    }

    public void setDirection(int direction) {
        this.playerDirection = direction;
        moving = true;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    private void setAnimation() {
        if (moving) {
            playerAction = WALKING;
        } else {
            playerAction = IDLE;
        }
    }
public void setMoving(boolean moving) {
        this.moving = moving;
    }

    private void setAnimation() {
        if (moving) {
            playerAction = WALKING;
        } else {
            playerAction = IDLE;
        }
    }
lean meadowBOT
tardy nexus
#
private void updatePosition() {
        if (moving) {
            switch (playerDirection) {
                case UP:
                    y -= 1;
                    break;
                case DOWN:
                    y += 1;
                    break;
                case LEFT:
                    x -= 1;
                    break;
                case RIGHT:
                    x += 1;
                    break;
                default:
                    break;
            }
        }
    }

    private void loadAnimation() {
        InputStream is = getClass().getResourceAsStream("/player_animations.png");
        try {
            BufferedImage image = ImageIO.read(is);
            playerAnimation = new BufferedImage[15][12];

            for (int j = 0; j < playerAnimation.length; j++) {
                for (int i = 0; i < playerAnimation[j].length; i++) {
                    playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
lean meadowBOT
placid cove
#

Try to log the current image from playerAnimation which is used and view the logs when the flicker occur, maybe there is an empty image or else. I cant really see anything obvious in ur code unless fully debugging it

tardy nexus
#

okey

#

nope but i see that is inconsistent sometimes it flikers sometimes works fine

#

with a simpler sprite works just fine

wraith drift
#

@tardy nexus share the sprite

tardy nexus
#

It's shared alongside the video

wraith drift
#
    private void loadAnimation() {
        InputStream is = getClass().getResourceAsStream("/player_animations.png");
        try {
            BufferedImage image = ImageIO.read(is);
            playerAnimation = new BufferedImage[15][12];

            for (int j = 0; j < playerAnimation.length; j++) {
                for (int i = 0; i < playerAnimation[j].length; i++) {
                    playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Also... you know that try-with-resources is a thing ?
And don't catch Exception

tardy nexus
#

The first message

wraith drift
wraith drift
tardy nexus
#

Okey ty

wraith drift
#

@tardy nexus are you using swing or awt ?

tardy nexus
#

Swing

wraith drift
tardy nexus
#

But it's only sometimes

#

I tried another sprite simpler 16 bits 7 images per animation

#

And works perfect

tardy nexus
#

i think i fixed it

private void setAnimation() {
        int startAnimation = playerAction;
        if (moving) {
            playerAction = WALKING;
        } else {
            playerAction = IDLE;
        }

        if (playerAction != startAnimation) {
            resetAnimationTick();
        }

    }

    private void resetAnimationTick() {
        animationTick = 0;
        animationIndex = 0;
    }

with this

lean meadowBOT
tardy nexus
#

and modified the loadAnimation:

private void loadAnimation() {
    try (InputStream is = getClass().getResourceAsStream("/player_animations.png")) {
        BufferedImage image = ImageIO.read(is);
        playerAnimation = new BufferedImage[15][12];

        for (int j = 0; j < playerAnimation.length; j++) {
            for (int i = 0; i < playerAnimation[j].length; i++) {
                playerAnimation[j][i] = image.getSubimage(i * 56, j * 56, 56, 56);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
lean meadowBOT
tardy nexus
#

you meant like this right ?

wraith drift
#

also, you probably want to crash if it doesn't find it

tardy nexus
#

with a if (is == null) ?

weary halo
#

You would normally do a if (foo != false) {your code} else { var is null }

tardy nexus
#

okey tysm for the help