Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Trouble displaying an image..  (Read 2125 times)
0 Members and 1 Guest are viewing this topic.
Offline hyde

Senior Newbie




Java games rock!


« Posted 2003-04-01 04:36:33 »

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!
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2003-04-01 04:50:01 »

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....

Java Game Console Project
Last Journal Entry: 12/17/04
Offline hyde

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-04-01 05:03:59 »

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!!  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2003-04-01 05:10:12 »

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

Java Game Console Project
Last Journal Entry: 12/17/04
Offline hyde

Senior Newbie




Java games rock!


« Reply #4 - Posted 2003-04-01 05:28:33 »

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 !
Offline Backmask

Junior Member




586: The average IQ needed to understand a PC


« Reply #5 - Posted 2003-04-01 05:52:03 »

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

Offline hyde

Senior Newbie




Java games rock!


« Reply #6 - Posted 2003-04-01 06:11:27 »

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!  Smiley

arghh...this is driving me insane...why cant i do something as simple (supposedly) as this... Cry
Offline Backmask

Junior Member




586: The average IQ needed to understand a PC


« Reply #7 - Posted 2003-04-01 07:04:29 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
 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);
 }
 
}

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #8 - Posted 2003-04-01 14:10:06 »

Quote
use
 
 Image tempImage = new ImageIcon("javalogo.gif");  
instead  Grin


you mean:

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


Smiley

Offline hyde

Senior Newbie




Java games rock!


« Reply #9 - Posted 2003-04-01 21:12:26 »

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...

Cry
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hyde

Senior Newbie




Java games rock!


« Reply #10 - Posted 2003-04-01 22:40:26 »

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~~ Smiley

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?
Offline Backmask

Junior Member




586: The average IQ needed to understand a PC


« Reply #11 - Posted 2003-04-02 05:49:10 »

swpalmer

I suck Tongue

Quote

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...


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.

Offline hyde

Senior Newbie




Java games rock!


« Reply #12 - Posted 2003-04-02 06:33:43 »

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.


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  Cry

can u elaborate on BufferStrategy and LWJGL?
thx again
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #13 - Posted 2003-04-02 14:30:27 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!