After using a double buffer which I will show below after using imported images they still occasionally flicker. The flickering occurs only in the animated GIF sprites and not in solid tiles such as the ground of the program. I thought it may be the fact that when changing image from a standing position to the moving position of the player there is a moment with no image so it disappears but that cannot be right since the player flickers even when not changing image and just moving.
Base code of the program (compressed a lot of it and removed a bunch for simplicity of reading) :
import javax.swing.*;
import java.awt.*;
public class FrontHand extends JFrame implements Runnable{
public static void main(String []args){
// Start Main Thread
FrontHand Thread1 = new FrontHand();
Thread1.run();
}
// Define Frame Properties
private FrontHand(){
ImageImport.main();
setSize(1600, 900);
setResizable(false);
setFocusable(true);
setVisible(true);
requestFocus();
addKeyListener(new AL());
addMouseListener(new ML());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
// Frame Double Buffer
public void paint(Graphics g){
Image dbImage = createImage(getWidth(), getHeight());
Graphics dbg = dbImage.getGraphics();
paintComponents(dbg);
g.drawImage(dbImage, 0, 0, this);
}
// Call paint functions
public void paintComponents(Graphics g){
// draw GUI
InVar.GUI.paintIcon(this, g, 0, 0);
// draw Ground Tiles
for (int i = 0; i != 10; i++) {
for (int n = 0; n != 22; n++) {
InVar.grass_tile.paintIcon(this, g, n * 62 + 118, 106 + i * 62);
// g.drawRect(n * 62 + 118, 106 + i * 62, 62, 62);
}
}
// Draw Player
InVar.player.paintIcon(this, g, InVar.player_x, InVar.player_y);
repaint();
}
// player movement
private void move(){PRETEBD THERE IS ADDITION AND SUBTRACTION TO THE PLAYER X AND Y HERE}
// check if there is any tiles left to move too and if null then redefine as non moving
if (InVar.p_m_x_counter == 0 && InVar.p_m_y_counter == 0){
InVar.player_moving = false;
// change the sprite of the player to the corresponding stationary one to the current player's sprite
if (InVar.player == InVar.player_m_up){
InVar.player = InVar.player_s_up;
}
else if (InVar.player == InVar.player_m_down){
InVar.player = InVar.player_s_down;
}
else if (InVar.player == InVar.player_m_left){
InVar.player = InVar.player_s_left;
}
else if (InVar.player == InVar.player_m_right){
InVar.player = InVar.player_s_right;
}
}
else{
InVar.player_moving = true;
}
}
@Override
public void run(){
while (InVar.game_running){
try{
Thread.sleep(5);
move();
}catch (InterruptedException e){System.out.println("Interrupted Exception Caught, ignore");}
}
}
}
The code that is used to import images :
ImageIcon a_1 = new ImageIcon(FrontHand.class.getResource("/Sprites/Tiles/GrassTile1.jpg"));
a_1.setImage(a_1.getImage());
InVar.grass_tile = a_1;
Before I was using .getScaledInstance for the animated GIF's which is what I also thought was causing the flickering but not using it and just externally changing the size of them did not fix the issue of the flickering.