图片加载路径

发布于 2024-12-28 15:38:57 字数 1818 浏览 2 评论 0原文

我想将图像添加到框架中,但我无法理解应该将图像文件保存在哪里以及应该如何给出图像的路径?

我已将图像放置在 My Document 文件夹中并给出了该路径,但它给出了错误消息。

我有以下代码。

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import javax.imageio.ImageIO; 
import javax.swing.*;   

public class Test extends JPanel {
    BufferedImage image;

    public Test(BufferedImage image) {
        this.image = image;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Draw image centered.
        int x = (getWidth() - image.getWidth())/2;
        int y = (getHeight() - image.getHeight())/2;
        g.drawImage(image, x, y, this);
    }

    public static void main(String[] args) throws IOException {
        String path = "images.jpeg";
        BufferedImage image = ImageIO.read(new File(path));
        Test contentPane = new Test(image);
        // You'll want to be sure this component is opaque
        // since it is required for contentPanes. Some
        // LAFs may use non-opaque components.
        contentPane.setOpaque(true);
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5,5,5,5);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        // Add components.
        for(int j = 0; j < 8; j++) {
            gbc.gridwidth = ((j+1)%2 == 0) ? GridBagConstraints.REMAINDER
                                           : GridBagConstraints.RELATIVE;
            contentPane.add(new JButton("button " + (j+1)), gbc);
        }
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(contentPane);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    } }

I want to add an image to a frame but I am not able to understand where I should keep the image file and how should I give the path of the image?

I have placed an image in My Document folder and given that path but it gave an error message.

I have the following code.

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.*; 
import javax.imageio.ImageIO; 
import javax.swing.*;   

public class Test extends JPanel {
    BufferedImage image;

    public Test(BufferedImage image) {
        this.image = image;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Draw image centered.
        int x = (getWidth() - image.getWidth())/2;
        int y = (getHeight() - image.getHeight())/2;
        g.drawImage(image, x, y, this);
    }

    public static void main(String[] args) throws IOException {
        String path = "images.jpeg";
        BufferedImage image = ImageIO.read(new File(path));
        Test contentPane = new Test(image);
        // You'll want to be sure this component is opaque
        // since it is required for contentPanes. Some
        // LAFs may use non-opaque components.
        contentPane.setOpaque(true);
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5,5,5,5);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        // Add components.
        for(int j = 0; j < 8; j++) {
            gbc.gridwidth = ((j+1)%2 == 0) ? GridBagConstraints.REMAINDER
                                           : GridBagConstraints.RELATIVE;
            contentPane.add(new JButton("button " + (j+1)), gbc);
        }
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(contentPane);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    } }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

メ斷腸人バ 2025-01-04 15:38:57

您的图像的位置很少。

  • 相对路径,在您的示例中,启动的应用程序在启动它的文件夹中查找图像(请检查应用程序是否已启动)。您可以为图像创建文件夹“resources”并通过“resources\\images.jpeg”

  • 绝对路径,例如 “C:\\Documents and settings\\< username>\\My Documents\\images.jpeg"

  • classpath - 将图像放在放置测试类的同一文件夹/包中。该资源可以打包为.jar文件。

    BufferedImage image = ImageIO.read(Test.class.getResourceAsStream("images.jpeg"));

  • 任何有效的 URL,例如您的头像

    BufferedImage image = ImageIO.read(URI.create("http://www.gravatar.com/avatar/d5f91983a9d9cfb69981b6108a63b412?s=32&d=identicon&r=PG").toURL()) ;

There are few places for your image.

  • relative path, in your example the started application look up the image in folder there you start it(please check there the application was started). You can create folder "resources" for image and access via "resources\\images.jpeg"

  • absolute path, something like "C:\\Documents and settings\\<username>\\My Documents\\images.jpeg"

  • classpath - put the image in same folder/package there are your Test class is placed. This resource can be packed to .jar file.

    BufferedImage image = ImageIO.read(Test.class.getResourceAsStream("images.jpeg"));

  • any valid URL, your avatar in example

    BufferedImage image = ImageIO.read(URI.create("http://www.gravatar.com/avatar/d5f91983a9d9cfb69981b6108a63b412?s=32&d=identicon&r=PG").toURL());

梦回梦里 2025-01-04 15:38:57

我应该在哪里保存图像文件..

由于它看起来像“应用程序资源”,我会说将其放在应用程序的运行时类路径上。这通常可以通过将其添加到 Jar 中(添加到目录,例如 images)来实现。

..我应该如何给出图像的路径?

通过从以下位置获取的 URL 访问它:

URL urlToImage = this.getClass().getResource("/images/the.png");

ImageIO.read() 也可以接受 URL 或更通用的 InputStream

where should I keep the image file ..

Since it seems like an 'application resource', I'd say put it on the run-time class-path of the app. That would usually be achieved by adding it (to a directory e.g. images) in a Jar.

..and how should I give the path of the image?

Access it via an URL obtained from:

URL urlToImage = this.getClass().getResource("/images/the.png");

ImageIO.read() can also accept URL or even more versatile, InputStream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文