最近我一直在尝试利用我在 CS 课上获得的知识来构建一个小游戏(2d,没什么大不了的)。在阅读了这些与图形相关的类的文档大约 2 周后,我最终遇到了这种情况:我有一个正在运行的系统,它以每秒 60 次游戏逻辑更新/60 帧的速度运行(这个系统运行得非常好:D)。作为第一个小测试,我想让图像在屏幕上移动。这就是代码(部分是我的,部分来自一些教程):
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final String NAME= "PokeCraft PRE-ALPHA";
public static final int HEIGHT=720;
public static final int WIDTH=HEIGHT*16/9;
public static final int SCALE=1;
private int fps=0;
private int tps=0;
private boolean running;
private int tickCount;
public void start(){
running = true;
new Thread(this).start();
}
public void stop(){
running = false;
}
public void render(){
BufferStrategy bufferStrategy =getBufferStrategy();
if(bufferStrategy==null){
this.createBufferStrategy(3);
return;
}
/* render function */
Graphics g = (Graphics) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, super.getWidth(), super.getHeight());
Image img = null;
try{
String imgPath = "data/MF.png";
img = ImageIO.read(getClass().getResourceAsStream(imgPath));
} catch(Exception e){
System.out.println(e);
}
g.drawImage(img, tickCount, 0, null);
Font font = new Font("Verdana",0,11);
g.setFont(font);
g.setColor(Color.RED);
g.drawString(NAME+" / "+fps+" fps, "+tps+"tps", 5, 15);
g.dispose();
bufferStrategy.show();
}
public void run() {
long lastTime= System.nanoTime();
double unprocessed = 0;
double nsPerTick = 1000000000.0/60.0;
int frames = 0;
int ticks = 0;
long lastTimer1 = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
unprocessed += (now-lastTime)/nsPerTick;
lastTime= now;
boolean shouldRender= false;
while(unprocessed >= 1){
ticks++;
tick();
unprocessed -= 1;
shouldRender = true;
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis()-lastTimer1 > 1000){
lastTimer1 += 1000;
System.out.println(ticks+" ticks, "+frames + " fps");
fps=frames;
tps=ticks;
ticks = 0;
frames = 0;
}
}
}
public void tick(){
tickCount++;
}
public static void main(String[] args){
Game game= new Game();
game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
JFrame frame = new JFrame(Game.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
ImageIO.read(...) 真的很难达到性能(根据 VisualVM,它需要 ~200 毫秒/运行)。我该如何解决这个问题?