0

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.

matvey-tk
  • 641
  • 7
  • 18
  • 1
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Oct 06 '16 at 09:18
  • @trashgod could you please explain ? – matvey-tk Oct 06 '16 at 09:20
  • Possible duplicate of [*Stop flickering in simple Java animation*](http://stackoverflow.com/q/19774720/230513). – trashgod Oct 06 '16 at 09:20
  • @trashgod Just tried using JPanel and calling other methods for the painting of images and it did not fix the issue – matvey-tk Oct 06 '16 at 11:24
  • 1
    `could you please explain ?` - that is what the link is for. Did you follow the link and read all the information in 2 minutes? `Just tried using JPanel and calling other methods` - post your [SSCCE](http://sscce.org/) that demonstrates the problem. And don't forget to read the link this time. – camickr Oct 06 '16 at 14:37
  • @camickr for one thing I did follow the link and the flickering problem caused there was caused because of the drawing being done in the paint method itself without any soft of buffering and was done in applet mine is done without applet. Next I did follow the "learning custom painting" and it did not help all it did was increase the amount of code I have, next I tried only repainting when the playing actually moves and that did not help and after doing some testing the problem only occurs during the movement of the player and when the player is using animated gif which is changing to another – matvey-tk Oct 06 '16 at 22:38
  • @camickr eg: moving left to still left to moving up player sprites but does not occur when the player image is changing between still images and does not occur when it is just one animated gif that does not change. I also noticed that when drawing all images once before doing the player animations it reduces the amount of flickering but does not fix the issue. I attempted many buffering techniques and it did not affect it at all I think it is something about the changing of the use of Image between still Image and animated Gif but the flickering did happen one or so times when the player was – matvey-tk Oct 06 '16 at 22:40
  • @camickr moving in a straight line without the image changing so I thought it was because the image was being imported too many times or being changed constantly so I checked and both were false, the images were imported once and the current image of the player only changed when the player changed direction or stopped moving or started moving. Before I was scaling the images through – matvey-tk Oct 06 '16 at 22:43
  • @camickr .getScaledImage which I also thought was a cause of the flickering but manually externally changing their size so that java does not have to do it did not fix the issue. Something to also note is that images such as the background image do not flicker but if I use other gifs for the player they do flicker. – matvey-tk Oct 06 '16 at 22:43
  • @camickr I also did some research into gifs flickering and found a post (this one: http://stackoverflow.com/questions/16698821/learning-java-why-on-earth-is-this-image-flickering-flashing) which had a solution which said that photoshop had corrupted their image which created redundant frames. Looking into the "corrupted Image" that the OP poster I checked it in several programs and non showed any redundant frames so either the person who fixed the issue knew something I dont or is hard to find or the OP changed the gif that was posted to one of the "fixed" ones that the other person posted – matvey-tk Oct 06 '16 at 22:47
  • @camickr also did a quick test to see if the program was too inefficient and was using too much computer power resulting in it not being able to carry out tasks like image changing but no that was not the issue, super.paintComponent(g); did not affect it at all too. I think the issue is the corrupted gif thing that the post I linked mentioned but I cannot find any traces of how to determine it or how to fix it, tried to check if the solver was online recently to contact him but sadly he has been off for a long time. – matvey-tk Oct 06 '16 at 22:52
  • @camickr thought that maybe the amount of classes is causing it to slow down as it has to constantly call and receive data from them so I recreated the program all in one class except the mouse listener and it not do anything also tried to optimize the repaint a bit more by making it repaint after the player image is changed and added a if/else statements so it repaints more nicely but it also did not fix the flickering issue. I am starting to run out of ideas of what to do to fix the issue – matvey-tk Oct 07 '16 at 00:40

0 Answers0