Java Canvas repaint()闪烁 [英] Java Canvas repaint() is flickering

查看:505
本文介绍了Java Canvas repaint()闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我终于有了一个Canvas可以按照我想要的方式工作,但是它不断闪烁,repaint()每秒运行20次,而当我使其每秒运行10次时,轻拂现象也会减少。

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();
    }
}

下面是我的MainMenu类:

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. 不要使用java.awt.Canvas。

  2. 在JPanel的paintComponent方法中绘制。

  3. 不要忘了调用您的super的绘画方法,该方法的费用为 paintComponent 将是 super.paintComponent(graphics)

  4. 从不尝试从任何绘画方法中读取图像。这将减慢绘画速度,并使您的程序显得无响应。为什么仍然要一遍又一遍地读取同一张图片? 一次读取它,并将其保存到变量中。

  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.

这篇关于Java Canvas repaint()闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆