Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:38:32+00:00 2026-05-26T19:38:32+00:00

I’m trying to add a JPanel with a picture in it. I’m using ImageIO.read

  • 0

I’m trying to add a JPanel with a picture in it. I’m using ImageIO.read to get the path but i get an IOException saying : can’t read input file

The picture is called TCHLogo. It’s a PNG inside a ‘res’ folder inside my project.

If there is any better way of displaying this image please also mention it!

Here is the code for my JPanel:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
        try {                
           //THIS LINE BELLOW WAS ADDED
           image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
        } catch (IOException ex) {
             // handle exception...
            System.out.println(ex);
        }
     }

     @Override
     public void paintComponent(Graphics g) {
         super.paintComponent(g);  //THIS LINE WAS ADDED
         g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
     }

}

Here is how i add the JPanel in my Applet:

ImagePanel appletRunningPanel;
appletRunningPanel = new ImagePanel();
appletRunningPanel.setSize(300, 300);
appletRunningPanel.validate();
add(appletRunningPanel);

EDIT
I created a folder inside the bin which the application starts to look in currently..
the folder is called res and the picture is inside..

Now i get the following IOException when i run the line:

image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));

Here is the error log:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1338)
    at surprice.applet.ImagePanel.<init>(ImagePanel.java:17)
    at surprice.applet.MainClass.init(MainClass.java:41)
    at sun.applet.AppletPanel.run(AppletPanel.java:436)
    at java.lang.Thread.run(Thread.java:679)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:38:33+00:00Added an answer on May 26, 2026 at 7:38 pm

    Likely your image’s file path isn’t correct relative to the user directory. To find out where Java is starting to look, where the user directory is, place something like this line of code somewhere in your program:

    System.out.println(System.getProperty("user.dir"));
    

    Perhaps you’d be better off getting the image as an InputStream obtained from a resource and not as a file. e.g.,

    image = ImageIO.read(getClass().getResourceAsStream("res/TCHLogo.png"));
    

    This will look for the image at the path given relative to the location of the class files, and in fact this is what you must do if your image is located in your jar file.

    Edit 2
    As an aside, often you need to first call the super’s paintComponent method before doing any of your own drawing so as to allow necessary house-keeping can be done, such as getting rid of “dirty” image bits. e.g., change this:

     public void paintComponent(Graphics g) {
         g.drawImage(image, 0, 0, null); 
     }
    

    to this:

     public void paintComponent(Graphics g) {
         super.paintComponent(g); // **** added****
         g.drawImage(image, 0, 0, null); 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I need to clean up various Word 'smart' characters in user input, including but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.