所以我在Youtube上关注Java的教程。然而,“老师”只是将他的JFrame标注为大约400x400px。我希望我的全屏。所以我试着愚弄我的代码(不只是想在尺寸上做1920x1280,因为我希望它在大多数分辨率上都可行)。
然而,当我尝试更改数字时,我只是对“JFrame.MAXIMIZED_BOTH”感到疯狂,因为我确信这将解决我所有的麻烦。猜猜看,它没有。由于我改变了很多,而且我不完全确定如何回到可以工作的地步,我正在向您展示我的完整主类代码。主要包含绘图,循环和缓冲:
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = JFrame.MAXIMIZED_BOTH, HEIGHT = JFrame.MAXIMIZED_BOTH, SCALE = 3;
public static boolean running = false;
public Thread gameThread;
private BufferedImage spriteSheet;
private Player player;
public void init(){
ImageLoader loader = new ImageLoader();
spriteSheet = loader.load("/Grass_road.png");
SpriteSheet ss = new SpriteSheet(spriteSheet);
player = new Player(0, 0, ss);
}
public synchronized void start(){
if(running)return;
running = true;
gameThread = new Thread(this);
gameThread.start();
}
public synchronized void stop(){
if(!running)return;
running = false;
try {
gameThread.join();
} catch (InterruptedException e) {e.printStackTrace();}
}
public void run(){
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60D;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1){
tick();
delta--;
}
render();
}
stop();
}
public void tick(){
player.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//RENDER HERE
g.fillRect(0, 0, JFrame.MAXIMIZED_BOTH, JFrame.MAXIMIZED_BOTH);
player.render(g);
//END RENDER
g.dispose();
bs.show();
}
public static void main(String[] args){
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
JFrame frame = new JFrame("Battle Beasts");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.add(game);
frame.setVisible(true);
game.start();
}
我确实成功获得了全屏并删除了边框。就像现在一样,我在角落里有一个非常小的盒子,只有几个像素。休息是前所未有的闪烁。
底线,我的屏幕闪烁,因为我从“g.fillRect(0,0,JFrame.MAXIMIZED_BOTH,JFrame.MAXIMIZED_BOTH)”这一行中提取的内容,现在只填充了一小部分屏幕。如何让它在所有分辨率上全屏显示?
编辑:安德鲁的步骤;
JFrame frame = new JFrame("Battle Beasts");
frame.add(game);
frame.pack();
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
game.start();
答案 0 :(得分:2)
如何让它在所有分辨率上全屏显示?
首先,摆脱这些界限:
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
请参阅Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是)。游戏应覆盖getPreferredSize()
以返回理想值,然后处理所提供的任何内容。
然后改变:
JFrame frame = new JFrame("Battle Beasts");
// ..
frame.add(game);
要:
JFrame frame = new JFrame("Battle Beasts");
frame.add(game); // add this early on, then..
frame.pack(); // immediately call pack()
// ..