14 Replies - 4462 Views - Last Post: 26 December 2012 - 12:20 PM Rate Topic: -----

#1 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Java and Images

Posted 25 December 2012 - 11:16 AM

The subject just eludes me. In various projects that I've worked on for school, I have used several different methods for using images in Java. However, this time, none of the methods I've used prior and nothing I've seen during my searches on Google is working.

I was hoping that some of the more intermediate to advanced Java programmers can show me how to properly utilize images with Java. More specifically, I would like to be able to show an image within a JPanel (or custom panel that extends JPanel). My issue is that no matter how many searches on Google I do, and no matter how many approaches I try, I can't seem to get images to work easily.

Here's some code that I tried to use to test using images (because I was being driven crazy trying to get them to work from within my current project).

package testimages;

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

import javax.imageio.ImageIO;
import javax.swing.*;

public class TestImageProgram extends JFrame{

	BufferedImage image;
	
	public TestImageProgram(){
		super("Test");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		try {                
	          image = ImageIO.read(getClass().getResource("imagefile.jpg"));
	       } catch (IOException ex) {
	            ex.printStackTrace();
	       } 
		
		ImagePanel ip = new ImagePanel();
		
		setSize(600, 400);
		ip.setVisible(true);
		getContentPane().add(ip);
		setVisible(true);
	}
	
	public static void main(String[] args){
		new TestImageProgram();
		
	}

	public class ImagePanel extends JPanel{

	    public ImagePanel() {
	       
	       
	       JLabel picLabel = null;
	       
	       if (image == null){
	    	   System.out.println("ERROR");
	       }else{
	    	   picLabel = new JLabel(new ImageIcon(image));
	       }
	       this.add(picLabel);
	       repaint();
	    }

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

	}
	
}




This gives me an exception:

Quote

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at testimages.TestImageProgram.<init>(TestImageProgram.java:20)
at testimages.TestImageProgram.main(TestImageProgram.java:34)


Just as a note, I put the image file in almost every directory within the project because I'm not sure which directory is considered the "root". However, it doesn't matter because it still doesn't work!

Is This A Good Question/Topic? 0
  • +

Replies To: Java and Images

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • Posts: 3,121
  • Joined: 05-April 11

Re: Java and Images

Posted 25 December 2012 - 11:29 AM

haha so you managed to place the image in every folder except the right one =D

The image needs to be in the same folder as the TestImageProgram.class file

Also if you call System.getProperty("user.dir"); then you will get the user's current working directory :)
Was This Post Helpful? 1
  • +
  • -

#3 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Java and Images

Posted 25 December 2012 - 11:36 AM

The problem is, the image file IS in the same folder as the TestImageProgram.class file. Thanks for the tip on the user's current working directory, but the image file is where it belongs.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • Posts: 3,121
  • Joined: 05-April 11

Re: Java and Images

Posted 25 December 2012 - 11:42 AM

View Postaxnjxn, on 25 December 2012 - 06:36 PM, said:

The problem is, the image file IS in the same folder as the TestImageProgram.class file. Thanks for the tip on the user's current working directory, but the image file is where it belongs.


I don't believe you have modified the classloader, so I still think you haven't put the image file together with the .class file
Was This Post Helpful? 1
  • +
  • -

#5 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Java and Images

Posted 25 December 2012 - 12:24 PM

Wait... modified the classloader? I'm not sure what that means. It is likely that I haven't modified the classloader. How does one do that? As for the image being with the .class file, it is most certainly in the same directory without a doubt.

This post has been edited by axnjxn: 25 December 2012 - 12:25 PM

Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • Posts: 3,121
  • Joined: 05-April 11

Re: Java and Images

Posted 25 December 2012 - 12:30 PM

View Postaxnjxn, on 25 December 2012 - 07:24 PM, said:

Wait... modified the classloader? I'm not sure what that means. It is likely that I haven't modified the classloader. How does one do that? As for the image being with the .class file, it is most certainly in the same directory without a doubt.


I give you + rep if you take a screenshot of your directory with the TestImageProgram.class file and the image. The file extension must be visible

^^ sorry but I just want to make sure

I have tried running your code and it shows the image here
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,732
  • Joined: 20-September 08

Re: Java and Images

Posted 25 December 2012 - 01:28 PM

The following will show you how to arrange resource loading

http://technojeeves....a-with-netbeans

http://technojeeves....va-with-eclipse
Was This Post Helpful? 1
  • +
  • -

#8 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Java and Images

Posted 25 December 2012 - 09:32 PM

This was the problem:

    try {               
        image = ImageIO.read(getClass().getResource("imagefile.jpg"));
        } catch (IOException ex) {
              ex.printStackTrace();
        }



It should be:

image = ImageIO.read(getClass().getClassLoader().getResource("imagefile.jpg"));



I was missing getClassLoader(). Thanks for those links, g00se.

This post has been edited by axnjxn: 25 December 2012 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,732
  • Joined: 20-September 08

Re: Java and Images

Posted 26 December 2012 - 05:46 AM

Quote

I was missing getClassLoader().
Not sure that would make a difference
Was This Post Helpful? 0
  • +
  • -

#10 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: Java and Images

Posted 26 December 2012 - 09:08 AM

It made all the difference, though. I'm not sure why, but when I put the getClassLoader() in, the image showed perfectly.
Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • Posts: 3,121
  • Joined: 05-April 11

Re: Java and Images

Posted 26 December 2012 - 09:39 AM

View Postaxnjxn, on 26 December 2012 - 04:08 PM, said:

It made all the difference, though. I'm not sure why, but when I put the getClassLoader() in, the image showed perfectly.


I still have my doubts looking at the implementations of the two methods

This is the implementation of the getResource method in Class
    public java.net.URL getResource(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResource(name);
        }
        return cl.getResource(name);
    }



And the getClassLoader
    public ClassLoader getClassLoader() {
        ClassLoader cl = getClassLoader0();
        if (cl == null)
            return null;
        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            ClassLoader ccl = ClassLoader.getCallerClassLoader();
            if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
            }
        }
        return cl;
    }

    // Package-private to allow ClassLoader access
    native ClassLoader getClassLoader0();


Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,732
  • Joined: 20-September 08

Re: Java and Images

Posted 26 December 2012 - 10:30 AM

Quote

It made all the difference, though. I'm not sure why, but when I put the getClassLoader() in, the image showed perfectly.
Coincident with a change that did make a difference ;)
Was This Post Helpful? 0
  • +
  • -

#13 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,591
  • Joined: 29-July 11

Re: Java and Images

Posted 26 December 2012 - 11:55 AM

View Postaxnjxn, on 26 December 2012 - 04:32 AM, said:

This was the problem:

    try {               
        image = ImageIO.read(getClass().getResource("imagefile.jpg"));
        } catch (IOException ex) {
              ex.printStackTrace();
        }



It should be:

image = ImageIO.read(getClass().getClassLoader().getResource("imagefile.jpg"));



I was missing getClassLoader(). Thanks for those links, g00se.


Let me guess, you're using eclipse, right? If so, your problem had nothing to do with either method of loading a url via getResource() - They both mean the exact same thing. You problem is an issue with eclipse itself. It's all about the "bin" directory. You cannot just open the project folder you're working on with edxplorer, nautilus, or whichever file manager in the os you're using and start pasting images. They won't be copied to bin, where they need to be, unless you project -> clean. OR, you must make sure you either import the image into your src folder through eclipse, or paste it there in the package explorer in eclipse; otherwise, it won't be copied into bin, and when you click run, and you'll get the exception.

Try it for yourself. Close eclipse and go to your project folder in workspace and delete the images from bin and from src. Open eclipse and make sure project -> build automatically is selected, then try to run the program. You will get the exception. Now through the eclipse package explorer(on the left), paste the image into your src with the class. Click run project. You still get the error because eclipse has not copied the image to the bin folder, where it is executing the class. You either have to copy the image there yourself via your OS file manager, or click project -> clean. This is confusing to many, but this is your problem.

I find it better to just create an "images" folder in my base project folder and access its images with

ImageIO.read(new File("images/image.jpg"));



As long as the images folder is in the project folder, you can out images in it any way you like, either through eclipse or via your OS file manager. Much easier.

It's better to know what really happened, instead of thinking that getClassLoader() actually made a difference when it did not.

This post has been edited by farrell2k: 26 December 2012 - 12:00 PM

Was This Post Helpful? 1
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,732
  • Joined: 20-September 08

Re: Java and Images

Posted 26 December 2012 - 12:13 PM

Quote

Let me guess, you're using eclipse, right? If so, your problem had nothing to do with either method of loading a url via getResource() - They both mean the exact same thing ...

Yes, all that's dealt with in the link i posted HERE
Was This Post Helpful? 1
  • +
  • -

#15 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,591
  • Joined: 29-July 11

Re: Java and Images

Posted 26 December 2012 - 12:20 PM

View Postg00se, on 26 December 2012 - 07:13 PM, said:

Quote

Let me guess, you're using eclipse, right? If so, your problem had nothing to do with either method of loading a url via getResource() - They both mean the exact same thing ...

Yes, all that's dealt with in the link i posted HERE


Oh. Maybe I should read links. It'll probably save me time. Ha ha. ;-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1