Displaying an ImageIcon

做~自己de王妃 提交于 2019-12-17 20:52:34

问题


I am trying to display an image on a JPanel. I'm using an ImageIcon for rendering the image, and the image is in the same directory as the class file. However, the image is not being displayed, and there are no errors occuring. Could anyone please assist in working out what's wrong with my code...

package ev;

import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Image extends JPanel {

    ImageIcon image = new ImageIcon("peanut.jpg");
    int x = 10;
    int y = 10;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        image.paintIcon(this, g, x, y);
    }
}

回答1:


You should use

ImageIcon image = new ImageIcon(this.getClass()
                .getResource("org/myproject/mypackage/peanut.jpg"));



回答2:


This is a common confusion among programmers. The getClass().getResource(path) loads resources from the classpath.

ImageIcon image = new ImageIcon("peanut.jpg");

If we only provide the name of the image file then Java is looking for it in the current working directory. If you are using NetBeans, the CWD is the project directory. You can figure out the CWD at runtime with the following call:

System.out.println(new File("").getAbsolutePath());

The following is a code example, where you can test this for yourself.

package com.zetcode;

import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class DrawingPanel extends JPanel {

    private ImageIcon icon;

    public DrawingPanel() {

        loadImage();
        int w = icon.getIconWidth();
        int h = icon.getIconHeight();
        setPreferredSize(new Dimension(w, h));

    }

    private void loadImage() {

        icon = new ImageIcon("book.jpg");
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        icon.paintIcon(this, g, 0, 0);
    }

}

public class ImageIconExample extends JFrame {

    public ImageIconExample() {

        initUI();
    }

    private void initUI() {

        DrawingPanel dpnl = new DrawingPanel();
        add(dpnl);

        // System.out.println(new File("").getAbsolutePath());         

        pack();
        setTitle("Image");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {                
                JFrame ex = new ImageIconExample();
                ex.setVisible(true);                
            }
        });
    }
}

The following picture shows where to put the book.jpg image in NetBeans if we only provide the image name to the ImageIcon constructor.

We have the same program from the command line. We are inside the ImageIconExample directory.

$ pwd
/home/vronskij/prog/swing/ImageIconExample

$ tree
.
├── book.jpg
└── com
    └── zetcode
        ├── DrawingPanel.class
        ├── ImageIconExample$1.class
        ├── ImageIconExample.class
        └── ImageIconExample.java

2 directories, 5 files

The program is run with the following command:

$ ~/bin/jdk1.7.0_45/bin/java com.zetcode.ImageIconExample

You can find out more at my Displaying image in Java tutorial.



来源:https://stackoverflow.com/questions/10084787/displaying-an-imageicon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!