Problems with loading an Image (using Eclipse)

It seems that I wasn't able to load an Image to my Java application.

When running the programme, my Frame looks empty. Instead that it should include a picture named "tausta.jpg".

I have succeed to load an Image when using in command prompt but in my new project with Eclipse i can't do that.

Image "tausta.jpg" is in the same folder as the other classes.

When running the programme, the text "it should work correctly right now - but doesn't do that!' appears to my console but picture isn't anywhere. Is this error acciociated with Eclipse IDE maybe?

package taistelupeli;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

publicclass Peliextends JFrame{

publicfinalint resox = 800;

publicfinalint resoy = 600;

Image screeni;

Pinta pinta;

/**

* @param args

*/

/**

* @param args

*/

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

Peli peli =new Peli();

}//P滗metodin loppu.

Peli(){

this.setSize(resox,resoy);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.addKeyListener(new Nappaimisto());

screeni = Toolkit.getDefaultToolkit().getImage("tausta.jpg");

pinta =new Pinta();

Container kehys = getContentPane();

kehys.add(pinta);

repaint();

}

class Nappaimistoimplements KeyListener{

publicvoid keyPressed(KeyEvent e){

// TODO Auto-generated method stub

System.out.println("toimii");

}

publicvoid keyReleased(KeyEvent e){

// TODO Auto-generated method stub

}

publicvoid keyTyped(KeyEvent e){

// TODO Auto-generated method stub

}

}

//Screeni

class Pintaextends JPanel

{

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

if(screeni !=null){g.drawImage(screeni, 0,0,this); System.out.println("it should work correctly right now - but doesn't do that!");}

}

}

[4151 byte] By [Pawa] at [2007-11-27 5:36:49]
# 1
Try using full page to load the image c:/...If you use relative path you have to make sure the file is placed in the working directory which in eclipse will be the project folder.
Rodney_McKaya at 2007-7-12 15:08:03 > top of Java-index,Security,Cryptography...
# 2

A couple of points..

- The JMF forum is not well suited to solving problems

related to loading image resources. Getting access to

resources is a much more common problem. (And this

forum is more geared to the JMF API - which is not

required for loading and displaying images). Some

beter forums might be..

http://forum.java.sun.com/forum.jspa?forumID=54

..or..

http://forum.java.sun.com/forum.jspa?forumID=31

- The problem does not relate to Eclipse, the code

would fail when being compiled and run from the

command line.

- It is better to avoid the String based forms of

getting resources, unless you check the resource

after it is supposedly loaded. Here is an alternate

approach that provides more feedback to the

developer. What does it print for you?

package taistelupeli;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.File;

import java.net.*;

public class Peli extends JFrame {

public final int resox = 400;

public final int resoy = 300;

Image screeni;

Pinta pinta;

public static void main(String[] args) {

Peli peli = new Peli();

}//P滗metodin loppu.

Peli(){

this.setSize(resox,resoy);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.addKeyListener(new Nappaimisto());

// the String based methods to get a resource

// are notoriously 'unreliable' in that they do

// not report problems!

// screeni = Toolkit.getDefaultToolkit().getImage("tausta.jpg");

File file = new File( "tausta.jpg" );

System.out.println( "File: '" + file.getName() +

"' exists '" + file.exists() + "'" );

try {

screeni = Toolkit.getDefaultToolkit().getImage(

file.toURI().toURL());

} catch(MalformedURLException murle) {

murle.printStackTrace();

}

pinta = new Pinta();

Container kehys = getContentPane();

kehys.add(pinta);

repaint();

}

class Nappaimisto implements KeyListener{

public void keyPressed(KeyEvent e) {

// TODO Auto-generated method stub

System.out.println("toimii");

}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

//Screeni

class Pinta extends JPanel

{

public void paintComponent(Graphics g){

super.paintComponent(g);

if(screeni !=null){

g.drawImage(screeni, 0,0,this);

System.out.println(

"it should work correctly right" +

" now - but doesn't do that!");

}

}

}

}

AndrewThompson64a at 2007-7-12 15:08:03 > top of Java-index,Security,Cryptography...
# 3
I tested your code and added to whole path and it works again! thank you. I just wonder why it doesn't work if the whole path isn't written...
Pawa at 2007-7-12 15:08:03 > top of Java-index,Security,Cryptography...
# 4

The basic problem is that you and the JVM

have different ideas about what is the 'current

directory'.

To find what the JVM takes as the current directory,

try changing the print statement from the earlier code to..

try {

System.out.println( "File: '" + file.getCanonicalPath() +

"' exists '" + file.exists() + "'" );

} catch(Exception e) {

e.printStackTrace();

}

There are a number of ways to ensure that

the code does not need to hard code the

entire path. One of the best ways is to ensure

that the image is available on the application's

classpath, and then use Class.getResource()

to get an URL that points to it.

Another advantage of using Class.getResource()

is that unless the JVM can actually locate the file,

it will return 'null' - easily checked in the code.

AndrewThompson64a at 2007-7-12 15:08:03 > top of Java-index,Security,Cryptography...