Java Canvas repaint() is flickering

佐手、 提交于 2019-12-08 03:51:06

问题


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?


回答1:


  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.


来源:https://stackoverflow.com/questions/29424960/java-canvas-repaint-is-flickering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!