Java 要使用jfilechooser或JPanel中的类似方法加载图像。目前程序甚至没有从本地加载图像;res";文件夹

Java 要使用jfilechooser或JPanel中的类似方法加载图像。目前程序甚至没有从本地加载图像;res";文件夹,java,paintcomponent,Java,Paintcomponent,我试图在JPanel中加载图像,之后我还想写一个图像标题。目前我无法加载图像。我不想使用JLabel。相反,我想使用油漆组件。这是密码 import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.geom.AffineTransform; import java.io.IOException; import java.util.logging.Level; import

我试图在JPanel中加载图像,之后我还想写一个图像标题。目前我无法加载图像。我不想使用JLabel。相反,我想使用油漆组件。这是密码

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


 public class imageCaptio extends JFrame {



 private JPanel caption = new JPanel(){

    ClassLoader cl = getClass().getClassLoader();
    Image i;
    {
        try {
            i = ImageIO.read(cl.getResourceAsStream("res/car.jpg"));
        } catch (IOException ex) {

Logger.getLogger(imageCaptio.class.getName()).log(Level.SEVERE, null,  
ex);
        }
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g.create();
        // get scaling instance to scale the image to the component
    AffineTransform imageAdjust =  
AffineTransform.getScaleInstance((float)getWidth()/i.getWidth(this),  
(float)getHeight()/i.getHeight(this));
    // draw the image using the separate transform
    if (i != null)
        g2.drawImage(i, imageAdjust, this);   
    }
};


 private imageCaptio() {
    // Invoke the JFrame superclass constructor and configure the frame
    super("image caption");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800,800);
    this.setVisible(true);
    // Add a panel to the frame and draw the face within it.
    this.add(caption);
}

public static void main(String[] args) {
    // Create on the Event Dispatch Thread.
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new imageCaptio();
        }
    });
}
}