加载图像到Java与BufferedImage的 - 甲骨文教程

问题描述 投票:-1回答:1

我在Java初学者,我想用这个脚本加载图像:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * This class demonstrates how to load an Image from an external file
 */
public class LoadImageApp extends Component {

BufferedImage img;

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}

public LoadImageApp() {
   try {
       img = ImageIO.read(getClass().getResource("/resources/java.png"));//cannot found image
   } catch (IOException e) {
   }

}

public Dimension getPreferredSize() {
    if (img == null) {
         return new Dimension(100,100);
    } else {
       return new Dimension(img.getWidth(null), img.getHeight(null));
   }
}

public static void main(String[] args) {

    JFrame f = new JFrame("Load Image Sample");

    f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    f.add(new LoadImageApp());
    f.pack();
    f.setVisible(true);
}
}

然后,我把图片上的文件夹资源的“资源”,改变像“/resources/java.png”当我编译图片的位置的名称,也没有像一个空的窗口。

你可以在这里看到的错误:https://ibb.co/ysjNyQw

java image embedded-resource
1个回答
0
投票

你会想要做的第一件事是做一些研究“嵌入的资源”,for example

我不使用Eclipse,我使用NetBeans,但过程应该是相同的

Project

正如你所看到的,我已经把我的形象在项目“源”中的resources包。这将确保它通过类路径搜索机制是在运行时可用(和嵌入式产生的JAR文件中,当我出口的话)。

然后我用一个JLabel显示它...

Example image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    BufferedImage image = ImageIO.read(getClass().getResource("/resources/java.png"));
                    JLabel label = new JLabel(new ImageIcon(image));

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(label);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

现在,如果你想继续使用自定义画的路线,我建议具有读取:

以获得更好的理解画在Swing是如何工作的,你应该如何使用它

© www.soinside.com 2019 - 2024. All rights reserved.