在 JFrame 中显示来自文件的图像 [英] Displaying an image from file in JFrame

查看:42
本文介绍了在 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 没有显示在屏幕上.有任何想法吗?我把我的图片放在了正确的位置,所以这应该不是问题.

This is just a window that goes full-screen, but the problem is my testImage does not display on the screen. Any ideas? I put my image in the right place, so that should not be a problem.

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 指南,了解如何操作并遵循每一步,但没有奏效.代码没有显示错误,只是不显示图像.

This is my 2nd main class, which just gets the images from my folder. I have watched some youtube guides on how to do it and followed every step, but it didn't work. The code shows no errors, it just wont display images.

推荐答案

您在问题中写道...

我看过一些 YouTube 指南

I have watched some youtube guides

要么那些指南不好,要么你误解了他们.

Either those guides were bad or you misunderstood them.

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

You seem to be making things much more complicated than they need to be. If all you want to do is display an image in a full screen window, the below code is a minimal example of how to do that.

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());
    }
}

在我看来,书面文字即将灭绝.就学习而言,我个人更喜欢书面文字而不是观看视频,但我猜设备屏幕一代不会.

It appears to me that the written word is headed for extinction. Personally I prefer the written word to watching videos when it comes to learning but I guess that the device screen generation does not.

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

If you are willing to learn by reading, I recommend the tutorial Creating a GUI With JFC/Swing. If you are willing to read a book, then I suggest one (or more) of the following (in no particular order):

  1. 核心 JFC(第 2 版)作者:金托普利
  2. Java Swing(第 2 版)作者:James Elliott(和其他人)
  3. Java Swing 权威指南 作者:约翰·祖科夫斯基
  1. Core JFC (2nd edition) by Kim Topley
  2. Java Swing (2nd edition) by James Elliott (and others)
  3. The Definitive Guide to Java Swing by John Zukowski

这篇关于在 JFrame 中显示来自文件的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆