简体   繁体   English

在Java中将图像加载到背景中

[英]Loading an image into a background in java

How do you load an image into a background in Java? 如何在Java中将图像加载到背景中? I have tried many different methods and none of them work so I am asking if anyone knows how to upload a png image into a jpannel 我尝试了许多不同的方法,但都没有用,所以我问是否有人知道如何将png图像上传到jpannel中

Make your own JPanel. 制作自己的JPanel。 Add an object of PicPanel in your GUI's constructor. 在GUI的构造函数中添加PicPanel对象。

class PicPanel extends JPanel{

    private BufferedImage image;
    private int w,h;
    public PicPanel(String fname){ //Pass picture's filename as a parameter.

        //reads the image
        try {
            image = ImageIO.read(getClass().getResource("/"+fname));
            w = image.getWidth();
            h = image.getHeight();

        } catch (IOException ioe) {
            System.out.println("Could not read in the pic");
            //System.exit(0);
        }

    }

    public Dimension getPreferredSize() {
        return new Dimension(w,h);
    }
    //this will draw the image
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(image,0,0,this);
    }
}

There are any number of ways this might be achieved. 有许多方法可以实现。

You Could... 你可以...

Create a JLabel , apply the image to it's icon property and set this as the frames content pane. 创建一个JLabel ,将图像应用于其icon属性,并将其设置为框架内容窗格。 You would then need to set the layout manager appropriately, as JLabel doesn't have a default layout manager 然后,您将需要适当地设置布局管理器,因为JLabel没有默认的布局管理器

JFrame frame = ...;
JLabel background = new JLabel(new ImageIcon(ImageIO.read(...)));
frame.setContentPane(background);
frame.setLayout(...);
frame.add(...);

The problem with this is the JLabel won't resize the image when the frame is resized 问题是调整框架大小时, JLabel不会调整图像大小

You Could... 你可以...

Create a custom component, extending from something like JPanel and override it's paintComponent method, painting the background as you see fit. 创建一个自定义组件,从诸如JPanel东西扩展并覆盖它的paintComponent方法,并根据需要绘制背景。

This provides you with the ability to decide how best the image should be scaled when it's available space changes. 这使您能够决定在可用空间变化时应如何最佳缩放图像。 While there are a number of ways this might be achived, you should read through The Perils of Image.getScaledInstance() to understand the pros and cons of them. 尽管有多种方法可以实现此目的,但您应该通读Image.getScaledInstance()的风险,以了解它们的优缺点。

This raises a bunch of new questions, to you want to scale them and preserve the aspect ratio? 这就提出了一堆新问题,您想缩放它们并保持纵横比吗? If so, do you want to fit the image to available area or fill it (so it will always cover the available space)? 如果是这样,您是否要使图像适合可用区域或填充它(这样它将始终覆盖可用空间)?

Take a look at Java: maintaining aspect ratio of JPanel background image for more details. 看一看Java:维护JPanel背景图像的长宽比以获得更多详细信息。

Other considerations 其他注意事项

Images are generally best loaded through the ImageIO API, as it's capable of loading a wide range of images, but will also throw an IOException when something goes wrong. 通常,最好通过ImageIO API加载图像,因为它可以加载各种图像,但是在出现问题时也会抛出IOException

See Reading/Loading an Image for more details. 有关更多详细信息,请参见读取/加载图像

The location of the image is also important. 图像的位置也很重要。 If the image is external to the application (somewhere on the file system), you can use ImageIO.read(new File("/path/to/image")) . 如果映像在应用程序外部(文件系统上的某个位置),则可以使用ImageIO.read(new File("/path/to/image")) However, if the the image is embedded within your application (stored within the Jar for example), you will need to use something more like ImageIO.read(getClass().getResource("/path/to/image")) instead... 但是,如果图像嵌入在您的应用程序中(例如存储在Jar中),则需要使用类似ImageIO.read(getClass().getResource("/path/to/image")) 。 ..

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

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