Trouble displaying an image..

Hi, I’m a beginner to Java graphics, and all I’m trying to do now is to display an image, but I have no idea why the image is not showing, all it shows is the default grey background of the JFrame object…

Here is the simple code:

import java.awt.;
import javax.swing.
;

public class Test extends JPanel{

  public Test()
  {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image map = toolkit.getImage("map.jpg");
    }
  
  
  public void paintComponent(Graphics g) 
  {
             g.drawImage(map, 0, 0, this);    
    }

  
  public static void main(String args[])
  {
        Test thing1 = new Test();
        
        JFrame window = new JFrame("Testing");
        
        Container container =        window.getContentPane();
        container.setLayout(new FlowLayout());
        container.add(thing1);
        
        //JButton butt = new JButton("Butt");
        //container.add(butt);
        
        window.pack();
        window.setVisible(true);
        window.setSize(800,1100);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

}

I think the component that draws the image (thing1) is not showing, since I have no probelms showing a button…
Can someone please point out for me what I’m missing…I’ve spent 4 hrs already on the problem…your help would be much appreciated…thank you!

How big is the image? If it’s larger than 512x512 you need to use the ImageIO API (javax.imageio). The reason is that images loaded with Toolkit.getImage() are automatically cached in video ram, most likely using a 3D pipeline like Direct3D or OpenGL. Since the largest texture accepted by many 3D cards is 512x512, you’re kind of stuck with this limitation. If you want to do any animation with large images tho, make sure you copy them to a VolatileImage or your code will be SLLLLOOOOWWWWW…

thank you very much for ur reply jbanes!
well the original image i intended to display is quite large…but the file size is small…
but thats not really the issue here, since I’ve already tried displaying a very small jpg file, and nothing is still showing, it’s just all grey.
I really think my code has some problem…can u kindly try my code with an arbitrary image on ur machine to see if it loads? thanks again!! :slight_smile:

Comment out the container.setLayout() and you should be fine with images less than 512x512.

I just tried what u said, taking out the setLayout, but it still doesnt work, it is still all grey.
I even just tried to load the small Java logo on top of this page, which I saved onto my PC, but it is still the same… :-/
thx again for ur reply though !

Two things. When you load the image make sure it´s being loaded directly by using MediaTracker. Search the site for code example, or use this code

ImageIcon tempImage = new ImageIcon(filename).getImage();

it uses MediaTracker

And use
public void paint(Graphics g){}
instead of
public void paintcomponents(Graphics g){}

not sure about this but that was the way I coded when I displayed images in ordinary awt/swing panels etc.

Hope this helps

Backmask:
I’ve tried using ImageIcon now…but it doesnt work still.
this is what i’ve done:

I added
ImageIcon tempImage = new ImageIcon(“javalogo.gif”);
I cannot add the .getImage() at the end as u’ve stated…

and then I’ve added
container.add(new JLabel(thing1.tempImage));
in the main

still not showing…
and also, I dont think MediaTracker is really needed in this case since I’m only loading an extremely small java logo gif file…but thx for the recommendation, I’ll keep that in mind. Thx again for ur reply Backmask! :slight_smile:

arghh…this is driving me insane…why cant i do something as simple (supposedly) as this… :’(

Of course you can not use the code line I wrote before

ImageIcon tempImage = new ImageIcon(“javalogo.gif”);

use

Image tempImage = new ImageIcon(“javalogo.gif”);
instead ;D

But anyway… I tried to play around with you code alittle. Unfortunate I am at work and does not have so much time. So I didn’t have the time to get it to work on a JPanel as your code was written at first. I am not sure if you need the image to be in an JPanel or if it is ok to display it on the JFrame? On a JFrame I have no trouble to display the image. The code for the image display on JFrame is below.


 import java.awt.*;
import javax.swing.*;
 
public class Test extends JFrame{
 
  Image map;
   
 public Test()
 {
  map = (new ImageIcon("map.jpg")).getImage();
  
  System.out.println("image size: " + map.getWidth(this) + ", " + map.getHeight(this));
   }
 
 
      public void paint(Graphics g){
            super.paint(g);
            g.drawImage(map, 0, 0, this);
      }
 
 
 public static void main(String args[])
 {
  Test thing1 = new Test();
  thing1.setSize(800,600);
  thing1.setLocation(0,0);
  thing1.setBackground(Color.black);
   
//  JFrame window = new JFrame("Testing");
   
  //Container container = window.getContentPane();
  //container.setLayout(null);//new FlowLayout());
  //container.add(thing1);
   
  
  //window.pack();
  thing1.setVisible(true);
  thing1.setSize(800,600);
  thing1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 
}

[quote]use

Image tempImage = new ImageIcon(“javalogo.gif”);
instead ;D
[/quote]
you mean:


  Image tempImage = new ImageIcon("javalogo.gif").getImage();   

:slight_smile:

Backmask:

thx for the code, but…it still doesnt show anything, not even the black blackground that u’ve set, it’s still grey.

but I’ve discovered something, thx to
System.out.println("image size: " + map.getWidth(this) + ", " + map.getHeight(this));
it’s that when the code runs, it returns : -1, -1

This means the image isnt loaded properly…right?
But I’m sure my path to the filename is 100% correct…since I’m storing it in same folder…

:’(

I can finally get it to work!!! By adding it as a JLabel!!
and it can support images of size larger than 512x512!!

this is how I implemented:

public class Game1 extends JFrame{

  public Game1()
  {
        super("Game1");
        ImageIcon icon = new ImageIcon("map.jpg");
        JLabel l = new JLabel(icon);
        this.getContentPane().add(l);
        setSize(1600,1000);
        setVisible(true);
  }
        
  public static void main(String args[])
  {
        Game1 application = new Game1();
        
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

}

I can finally display an image~thx to all who have helped me out during the last 12 hrs :slight_smile:

But now I have a simple question
what is the difference between using paint to display an image (which i thought was required for all this time) compared to using JLabel? advantages…disadvantages?

swpalmer

I suck :stuck_out_tongue:

Your path could not be correct if you get -1, -1… beacuse this states that the image has not been loaded…

But if you got it to work with JLabel you could use that to.

If you are going to make a game or some sort of graphical applikation that doesn’t need buttons, list from AWT or Swing I strongly recommened that you take a look at BufferStrategy or LWJGL.

[quote]If you are going to make a game or some sort of graphical applikation that doesn’t need buttons, list from AWT or Swing I strongly recommened that you take a look at BufferStrategy or LWJGL.
[/quote]
yeah…I’m trying to make the GUI for a Risk type of game…in a very very short period…and I’m still stuck at things like this…im pathetic :’(

can u elaborate on BufferStrategy and LWJGL?
thx again

Go read the tutorials at java.sun.com They explain how image loading works, how to wait to make sure the image is completely loaded beffore you try to use it etc. All of your problems will be solved if you just go to the source and read the docs.