【问题标题】:Displaying an image from file in JFrame在 JFrame 中显示文件中的图像
【发布时间】: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


【解决方案1】:

您在问题中写道...

我看过一些 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());
    }
}

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

如果你愿意通过阅读学习,我推荐教程Creating a GUI With JFC/Swing。如果您愿意阅读一本书,那么我建议您选择以下一种(或多种)(排名不分先后):

  1. Core JFC(第 2 版),作者:Kim Topley
  2. Java Swing(第 2 版),作者 James Elliott(和其他人)
  3. The Definitive Guide to Java Swing by John Zukowski

【讨论】:

  • 对于好的问题和答案,我必须强调 3 件事(如此处所示):(1) 书面指南 是多用途的:您可以搜索(以数字方式或使用书籍' 索引)和复制粘贴文本摘录(至少指向一个 URL、页面)。给个链接,大家可以关注! (2) 遵循一些步骤(来自公共文本/视频或私人书籍):请列出步骤(用人类语言),以便每个人都可以遵循并分享他们的理解。 (3)Code-only通常不能单独说话。它遭受解释。 How-To-Do 配方只能用伴随代码的人类语言表达。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多