首页 > 解决方案 > 在 JFrame 中显示文件中的图像

问题描述

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 这个视频我得到了全屏图像),但它没有用。该代码没有显示错误,它只是不会显示图像。

标签: javaswingbufferedimage

解决方案


你在你的问题中写道......

我看过一些 youtube 指南

要么这些指南不好,要么你误解了它们。

你似乎让事情变得比他们需要的复杂得多。如果您只想在全屏窗口中显示图像,下面的代码是如何做到这一点的最小示例。

import java.awt.EventQueue;
import java.awt.Frame;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

public class Menu implements Runnable {
    private JFrame frame;

    public void run() {
        showGui();
    }

    private void showGui() {
        frame = new JFrame("Menu");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        ImageIcon ico = new ImageIcon(getClass().getResource("menu.png"));
        JLabel label = new JLabel(ico);
        frame.add(label);
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Menu());
    }
}

在我看来,书面文字正走向灭绝。就学习而言,我个人更喜欢书面文字而不是观看视频,但我猜设备屏幕生成不会。

如果您愿意通过阅读来学习,我推荐使用 JFC/Swing 创建 GUI教程。如果您愿意阅读一本书,那么我建议以下一种(或多种)(不分先后):

  1. Kim Topley 的核心 JFC(第 2 版)
  2. James Elliott(和其他人)的Java Swing(第 2 版)
  3. John Zukowski的 Java Swing 权威指南

推荐阅读