FOD
FOD

Reputation: 323

Java Canvas repaint() is flickering

So I finally got a Canvas to work the way I want it but it flickers constantly, repaint() is run 20 times a second and the flicking does lessen when I make it run 10 times a second.

package pd.data;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import pd.areas.MainMenu;

@SuppressWarnings("serial")
public class Main extends JFrame implements Runnable {

    private JPanel contentPane = new JPanel();

    private Thread gameThread = new Thread(this);
    public boolean running = false;



    @SuppressWarnings("unused")
    private int current = PDU.PD_MAIN_MENU;
    private MainMenu mainmenu;

    public Main() {main.setTitle("PD");
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setLocation(SPU.screenWidth / 2 - SPU.windowSize.width / 2,
                SPU.screenHeight / 2 - SPU.windowSize.height / 2);
        main.setResizable(false);
        main.setVisible(true);
        contentPane.setLayout(new BorderLayout(0, 0));
        contentPane.setPreferredSize(SPU.windowSize);
        main.setContentPane(contentPane);
        main.pack();
        mainmenu = new MainMenu();

        contentPane.add(mainmenu, BorderLayout.CENTER);
        this.gameThread.start();
    }

    @Override
    public void run() {
        running = true;
        while (running) {
            {
                mainmenu.repaint();
            }
            try {
                Thread.sleep(SPU.TSU);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {
        new Main();
    }
}

And below is my MainMenu class:

package pd.areas;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

@SuppressWarnings("serial")
public class MainMenu extends Canvas{

    BufferedImage img = null;
    public MainMenu() {
        this.setBackground(Color.BLACK);

    }
    public void paint(Graphics graphics){

        try {
            img = ImageIO.read(this.getClass().getResource(
                    "/image.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        graphics.drawImage(img, this.getWidth() / 2 - img.getWidth()/2, 50, null);
    }

}

Although the flicker is actually a nice effect, it's going to effect the whole canvas and not just the image I'm guessing, how can I fix the flicker?

Upvotes: 2

Views: 4317

Answers (2)

WTRIX
WTRIX

Reputation: 95

Though this thread was opened 5 years ago, it's still going to be an issue for anyone new to Java SE.

I had the same problem with Canvas myself, but wasn't convinced enough to switch to javax components because I recalled a VLC project called JVLC which used Canvas to render videos flawlessly (granted jvlc uses some native code).

Tip 1: perhaps it's better to try JPanel instead.

Tip 2: in gaming systems, it's better to use a game engine (it'll save you lots of time).

Tip 3: in gaming systems, implement a fps mechanism rather than calling repaint() on every change.

Tip 4: if you have to call more than 10 lines of code in your paint() implementation you're going to slow it down. It's better to draw to a BufferedImage for every graphical change you need to make to your app, then have the paint() implementation itself draw just the image itself. This should play nicely with an fps mechanism and will reduce the chances of any flickering.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

  1. Don't use java.awt.Canvas. Use a JPanel instead.
  2. Draw in the JPanel's paintComponent method.
  3. Don't forget to call your super's painting method which for paintComponent would be super.paintComponent(graphics)
  4. Never try to read in an image from within any painting method. That will slow down painting and make your program seem unresponsive. Why keep reading in the same image over and over again anyway? Read it in once and save it to a variable.

Upvotes: 5

Related Questions