【发布时间】:2020-07-07 10:17:14
【问题描述】:
public class Menu extends JFrame {
private static Frame frame;
private static Canvas canvas;
private int width;
private BufferedImage testImage;
private int height;
private Graphics g;
private static int canvasWidth = 0;
private static int canvasHeight = 0;
private static final int GAME_WIDTH = 400;
private static final int GAME_HEIGHT = 250;
private static int gameWidth = 0;
private static int gameHeight = 0;
public static void getBestSize() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
boolean done = false;
while (!done) {
canvasWidth += GAME_WIDTH;
canvasHeight += GAME_HEIGHT;
if (canvasWidth > screenSize.width || canvasHeight > screenSize.height) {
canvasWidth -= GAME_WIDTH;
canvasHeight -= GAME_HEIGHT;
done = true;
}
}
int xDiff = screenSize.width - canvasWidth;
int yDiff = screenSize.height - canvasHeight;
int factor = canvasWidth / GAME_WIDTH;
gameWidth = canvasWidth / factor + xDiff / factor;
gameHeight = canvasHeight / factor + yDiff / factor;
canvasWidth = gameWidth * factor;
canvasHeight = gameHeight * factor;
}
public Menu (int w, int h) {
getBestSize();
height = h;
width = w;
frame = new Frame();
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
frame.add(canvas);
makeFullscreen();
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
this.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Menu.quit();
}});
startRendering();
}
private static void startRendering() {
Thread thread = new Thread() {
public void run() {
GraphicsConfiguration gc = canvas.getGraphicsConfiguration();
VolatileImage vImage = gc.createCompatibleVolatileImage(gameWidth, gameHeight);
while (true) {
if (vImage.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
vImage = gc.createCompatibleVolatileImage(gameWidth, gameHeight);
}
Graphics g = vImage.getGraphics();
g.clearRect(0, 0, gameWidth, gameHeight);
g.dispose();
g = canvas.getGraphics();
g.dispose();
g = canvas.getGraphics();
}
}
};
thread.start();
}
public static void quit() {
System.exit(0);
}
private static void makeFullscreen() {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
(frame).setUndecorated(true);
gd.setFullScreenWindow(frame);
}
}
public void setUpMenu() {
this.setSize(width, height);
this.setTitle("Masters");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
testImage = ImageLoader.loadImage("menu.png");
g.drawImage (testImage, 20, 20, null);
makeFullscreen();
}
public static void main(String[] args) {
Menu m = new Menu(1920, 1080);
m.setUpMenu();
}
}
这只是一个全屏显示的窗口,但问题是我的 testImage 没有显示在屏幕上。有任何想法吗?我把我的图片放在了正确的位置,所以这应该不是问题。
public class ImageLoader {
public static BufferedImage loadImage(String path) {
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}
这是我的第二个主要课程,它只是从我的文件夹中获取图像。我已经观看了一些 youtube 指南,了解如何做到这一点并遵循每一步(请注意,我完全误解了这些视频并将它们混合在一起,绝不是说这些视频不好 https://www.youtube.com/watch?v=oXmUp4ZTW2Q 来自此视频我得到了图形,https://www.youtube.com/watch?v=TQEEsR559QQ&t=2s 这个视频我得到了全屏图像),但它没有用。该代码没有显示错误,只是不会显示图像。
【问题讨论】:
-
老实说,您写道“观看了一些 youtube 指南”。如果您发布指向该视频的链接,它肯定会有所帮助。这是有价值的研究,你做到了:“跟随每一步”。现在让我们也按照这些步骤进行操作。将链接/URL 添加到视频(您的研究)并写下您遵循的步骤(您尝试过的)。然后我们可以让你重回正轨??????️
标签: java swing bufferedimage