老实说,我不确定为什么会这样。唯一的#34;渲染"我的java项目中的代码,目前很小,如下:
public void render(){
if (bs == null){
bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(3);
return;
}
}
g2d = (Graphics2D) bs.getDrawGraphics();
bs.show();
g2d.dispose();
}
当我运行我的项目时,框架在黑白之间快速闪烁。我甚至没有代码改变背景颜色和Graphics2D颜色。
以下是引擎和窗口类:
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); }
}