Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 非故意的黑白闪烁_Java_Swing - Fatal编程技术网

Java 非故意的黑白闪烁

Java 非故意的黑白闪烁,java,swing,Java,Swing,老实说,我不知道为什么会发生这种情况。我的java项目中唯一的“呈现”代码(目前相当小)如下所示: public void render(){ if (bs == null){ bs = getBufferStrategy(); if (bs == null){ createBufferStrategy(3); return; }

老实说,我不知道为什么会发生这种情况。我的java项目中唯一的“呈现”代码(目前相当小)如下所示:

public void render(){
        if (bs == null){
            bs = getBufferStrategy();
            if (bs == null){
                createBufferStrategy(3);
                return;
            }
        }
        g2d = (Graphics2D) bs.getDrawGraphics();

        bs.show();
        g2d.dispose();
    }
当我运行项目时,帧在黑白之间快速闪烁。我甚至没有改变背景颜色或图形2D颜色的代码

以下是引擎和窗口类:

package io.shparki.cyberphunk;

import io.shparki.cyberphunk.gfx.Window;

import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

public class Engine extends Canvas implements Runnable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final int WIDTH = 1200;
    public static final int HEIGHT = WIDTH / 16 * 9;
    public static final String TITLE = "Cyberphunk";
    public static final String VERSION = "Alpha v1.0.0";

    public static final int TARGET_UPS = 60;
    public static final long SECOND = 1_000_000_000L;
    public static final long PERIOD = SECOND / TARGET_UPS;
    private int currentUPS = 0, currentFPS = 0;
    private int UPS = 0, FPS = 0;
    private double deltaTime = 0;

    private volatile boolean running = false;
    private Thread animator;

    private Game game;
    private BufferStrategy bs;
    private Graphics2D g2d;


    public Engine(){
        Window.create(WIDTH, HEIGHT, TITLE, this);
    }

    public synchronized void start(){
        if (!running || animator == null){
            animator = new Thread(this, "Animator");
            animator.start();
        }
    }
    public synchronized void stop(){
        running = false;
    }

    public void run(){
        long currentTime = System.nanoTime();
        long beforeTime = currentTime;
        long upsCounter = 0, secCounter = 0;

        initialize();

        running = true;
        while (running){
            beforeTime = currentTime;
            currentTime = System.nanoTime();
            deltaTime = currentTime - beforeTime;

            upsCounter += deltaTime;
            if (upsCounter >= PERIOD){
                upsCounter -= PERIOD;
                currentUPS ++;
                update();
            }

            currentFPS ++;
            render();

            secCounter += deltaTime;
            if (secCounter >= SECOND){
                secCounter -= SECOND;
                UPS = currentUPS; FPS = currentFPS;
                currentUPS = 0; currentFPS = 0;
                System.out.println("UPS: " + UPS + " | FPS: " + FPS);
                }
            }
            System.exit(0);
        }

        public void initialize(){
            game = new Game();
        }
        public void update(){
            game.update();
        }
        public void render(){
            if (bs == null){
                bs = getBufferStrategy();
                if (bs == null){
                    createBufferStrategy(3);
                    return;
                }
            }
            g2d = (Graphics2D) bs.getDrawGraphics();

            bs.show();
            g2d.dispose();
        }

    }

package io.shparki.cyberphunk.gfx;

import io.shparki.cyberphunk.io.Input;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Window {
    private static JFrame frame;
    private static Canvas content;
    private static Input input;

    public static void create(int width, int height, String title, Canvas content){
        Window.content = content;
        content.setPreferredSize(new Dimension(width, height));
        content.setMinimumSize(new Dimension(width, height));
        content.setMaximumSize(new Dimension(width, height));

        frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(new BorderLayout());
        frame.add(content, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        input = new Input();
        content.setFocusable(true);
        content.requestFocus();
        //content.addKeyListener(input);
        //content.addMouseListener(input);
        //content.addMouseMotionListener(input);
    }

    public static int getWidth() { return content.getWidth(); }
    public static int getHeight() { return content.getHeight(); }

    public static String getTitle() { return frame.getTitle(); }
    public static void setTitlte(String title) { frame.setTitle(title); }
}

是的,很可能一页是白色的,一页是黑色的,这正是我所想的,但我以前写过很多次这段代码,它实际上只是一个烤肉盘代码。我在30分钟前改变了一件事,它做了一些奇怪的事情,所以我删除了它,现在这一切都发生了。我重写了整个引擎和窗口类,这仍然在发生。还没有其他类可以进行渲染。你需要用你想要的颜色填充每个页面,否则图形上下文在上下文中是“未初始化”的,你将得到任何API默认值。我不确定你是否应该处理Graohics上下文,但我可能错了,但我个人会在放映之前这样做,但这是梅斯温默认为白色。应该发生的是,屏幕一直保持白色,直到我告诉它该做什么。相反,它是在白色和黑色之间切换。黑色不是任何值的默认值,所以一定有什么东西将其设置为黑色,我不知道是什么。是的,很可能一页是白色的,一页是黑色的,这正是我所想的,但我以前写过很多次这段代码,它实际上只是一个烤肉盘代码。我在30分钟前改变了一件事,它做了一些奇怪的事情,所以我删除了它,现在这一切都发生了。我重写了整个引擎和窗口类,这仍然在发生。还没有其他类可以进行渲染。你需要用你想要的颜色填充每个页面,否则图形上下文在上下文中是“未初始化”的,你将得到任何API默认值。我不确定你是否应该处理Graohics上下文,但我可能错了,但我个人会在放映之前这样做,但这是梅斯温默认为白色。应该发生的是,屏幕一直保持白色,直到我告诉它该做什么。相反,它是在白色和黑色之间切换。黑色不是任何值的默认值,所以一定有什么东西将其设置为黑色,我不明白是什么。