简体   繁体   English

Java - 如何获取要显示的图像?

[英]Java - How do I get an image to display?

I have spent a really long time trying to find a way to display an image in a Java program (I'm trying to learn how to make a 2D Java game) an nothing that I've tried works.我花了很长时间试图找到一种在 Java 程序中显示图像的方法(我正在尝试学习如何制作 2D Java 游戏),但我尝试过的任何方法都不起作用。 I'm using Eclipse Mars and the latest of everything else.我正在使用 Eclipse Mars 和最新的其他所有东西。 Here is my code:这是我的代码:

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

public class Main extends JFrame {

    public static void main(String[] args0) {

        JFrame frame = new JFrame();
        ImageIcon image = new ImageIcon("background.bmp");
        JLabel imageLabel = new JLabel(image);
        frame.add(imageLabel);
        frame.setLayout(null);
        imageLabel.setLocation(0, 0);
        imageLabel.setSize(1000, 750);
        imageLabel.setVisible(true);
        frame.setVisible(true);
        frame.setSize(1000, 750);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

}

Please, just tell me how to correct the code so that the image actually displays.请告诉我如何更正代码以便实际显示图像。 Thank you ahead of time.提前谢谢你。

(PS the "background.bmp" file is in the "default package" if that changes anything) (PS“background.bmp”文件在“默认包”中,如果有任何改变的话)

The image with .bmp suffix can't be displayed in your code.带有 .bmp 后缀的图像无法在您的代码中显示。 Try to modify尝试修改

ImageIcon image = new ImageIcon("background.bmp"); ImageIcon image = new ImageIcon("background.bmp");

to

ImageIcon image = new ImageIcon("background.png"); ImageIcon image = new ImageIcon("background.png");

and change the image name to background.png.并将图像名称更改为 background.png。

However, if you just want to use background.bmp, you need to modify your code like this and background image will be displayed.但是,如果您只想使用background.bmp,则需要像这样修改您的代码,背景图像才会显示出来。

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    public static void main(String[] args0) {

        try {
            JFrame frame = new JFrame();
            File imageFile = new File("background.bmp");
            Image i = ImageIO.read(imageFile);
            ImageIcon image = new ImageIcon(i);
            JLabel imageLabel = new JLabel(image);
            frame.add(imageLabel);
            frame.setLayout(null);
            imageLabel.setLocation(0, 0);
            imageLabel.setSize(1000, 750);
            imageLabel.setVisible(true);
            frame.setVisible(true);
            frame.setSize(1000, 750);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Here is the effect with new code:这是新代码的效果: 在此处输入图片说明

Hope it helps.希望能帮助到你。

BTW, I've put the image at the root directory of the project.顺便说一句,我已将图像放在项目的根目录中。

Read the section from the Swing tutorial on How to Use Icons .阅读 Swing 教程中关于如何使用图标的部分 The examples their show you how to load the images as resources.他们的示例向您展示了如何将图像加载为资源。 When you use resources the classpath will be searched to find the file.当您使用资源时,将搜索类路径以查找文件。

//frame.setLayout(null);
//imageLabel.setLocation(0, 0);
//imageLabel.setSize(1000, 750);

Don't use a null layout and attempt to set the size/location of the label.不要使用空布局并尝试设置标签的大小/位置。 Let the layout manager do its job.让布局管理器完成它的工作。 The label will be the size of the image.标签将是图像的大小。

//frame.setSize(1000, 750);
frame.pack();

Don't use frame.setSize(...).不要使用 frame.setSize(...)。 Instead you use frame.pack() then the frame will be the size of the image plus the size of the frame borders and title.相反,您使用frame.pack()则框架将是图像的大小加上框架边框和标题的大小。 Let Swing and its layout managers do the work for you.让 Swing 及其布局管理器为您完成工作。

As a beginer, I found that is easy to see the picture you draw:作为初学者,我发现很容易看到你画的图:

Source code源代码

public class CheckCodeTest  {

    private int width = 100, height = 50;
    private BufferedImage image = new BufferedImage(width, height,
                                      BufferedImage.TYPE_INT_RGB);

    @Test
    public void drawGraphicsTest() throws IOException {

        Graphics graphics = image.createGraphics();

        // draw an orange rectangle
        graphics.setColor(Color.orange);
        graphics.fillRect(0,0,width,height);

        // layout the picture right now!
        graphics.drawImage(image,0,0,null);
        ImageIO.write(image, "png", new File("checkcode.png"));
    }
}

Output输出

It produce a picture file under your projects content.它会在您的项目内容下生成一个图片文件。

output-picture输出图片

Then you can see what change after adding draw code in small window, it is more convenient than closing an jump-out Frame / Label window:然后就可以看到在小窗口中添加draw code后有什么变化,比关闭一个跳出的Frame/Label窗口方便多了:

output-picture-in-editor输出图片编辑器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM