0

I've started to code a small and simple engine with Java (for games). When I repaint my screen it sometimes flickers. I've looked up answers to this question, and an answer suggested to use the swing timer (which is what I'm doing). Here are the relevant pieces of code (I added in some comments):

public class Game extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;

private static Window window;
private static ObjectUpdater objects;
private static Timer timer;

private static boolean init = false;


public Game(){
    timer = new Timer(30,this);
    timer.start();
}

public static void main (String[] self){
    new Game();
}

private static void initialize(){
    //Tests
    //Setting up the window
    window = new Window();

    //Setting up the updater
    objects = new ObjectUpdater();

    new Picture(Path.images+Path.img_black,10);
    // These are just some objects for the game...

    Entity r = new Entity(new Picture(Path.images+Path.img_lapras,0));
    r.setVelocity(0.5,0);
    Entity r2 = new Entity(new Picture(Path.images+Path.img_lapras,1));
    Vector i = new Vector(0,0.5);
    r2.setVelocity(i.values()[0],i.values()[1]);
}
// This is where repaint(); is called.
public void actionPerformed(ActionEvent e) {
    setDoubleBuffered(true);
    if (init == false){
        initialize();
        init = true;

    }

    objects.update(); // This updates all the game object's information
    window.update(); // This updates the window itself, it's literally: repaint();

}
}

Here is the window object's code because this is where stuff is also happening.

public class Window extends JFrame{
private static final long serialVersionUID = 1L;

public static int refresh_rate = 25;
public static Picture[] image_list =  new Picture[0]; // All the images I want to render
private static String win_title = "Window"; // The name of the window
private static int[] win_xy = {640,360}; // The size of the window

public Window(){
    //initializes window 
    setTitle(win_title);
    //setUndecorated(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(win_xy[0],win_xy[1]);
    setLocationRelativeTo(null); 
    setVisible(true);
    setResizable(false);

}

public static int[] getWinSize(){
    return win_xy;
}

// Here I'm just "painting" everything in the image_list array...
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    for (Picture i:image_list){
        g2d.drawImage(i.getPic(), i.getXY()[0], i.getXY()[1], null); 
    }
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}


public static void addPicture(Picture element){// This adds images to the image_list
    Picture[] result = Arrays.copyOf(image_list, image_list.length +1);
    result[image_list.length] = element;
    image_list = result;
    Arrays.sort(image_list);

}

public void update() {
    repaint();

}

}

That's it... Thank you!

Linkxgl
  • 161
  • 4
  • 14
  • 3
    Calling `super.paint(g)` in `paint` method in the `Window` class is the cause of the flicker, and moreover you should override `paintComponent` not `paint` for swing based apps. – Extreme Coders Feb 09 '13 at 14:32
  • Is there any reason why I should be using `paintComponent` and not `paint`? I mean more specifically? – Linkxgl Feb 09 '13 at 14:59
  • Check ***[this](http://stackoverflow.com/questions/9389187/difference-between-paint-paintcomponent-and-paintcomponents-in-swing)*** page. – Extreme Coders Feb 09 '13 at 15:05

1 Answers1

1

Top level contains (such as JFrame) are not double buffered. This is one of the reasons why we don't recommend extending from top level containers.

Instead, create your self a custom component (from JPanel for example) and override it's paintComponent method (don't forget to call super.paintComponent)

Check out

For some examples

Community
  • 1
  • 1
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • If you found the answer useful, you can mark it as correct (the little tick on the right side), otherwise, remember me when you do have some more reputation ;) – MadProgrammer Feb 15 '13 at 01:02