
-----------------------------------
seymournexus
Thu Jun 08, 2006 11:40 pm

Ahh, image won't display
-----------------------------------

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class GameCanvas extends Canvas {

    Image title = Toolkit.getDefaultToolkit().getImage("startmenu.jpg");
    Image tokenImage[] = new Image [8];
    String section = "";

    public GameCanvas() {
        super();
        section = "start menu";
        delay (2000);
        
        section = "token selection";
        for (int i = 0; i < tokenImage.length; i++){
            tokenImage[i] = Toolkit.getDefaultToolkit().getImage((i+1) + ".gif");
        }
    }
    
    public void paint(Graphics g) {
        if (section.equals("start menu")){
            g.drawImage(title, 0, 0, null);
        } else if (section.equals("token selection")){
        for (int i = 0; i < tokenImage.length; i++)
            g.drawImage(tokenImage[i], i * 30, 0, null);
        } else {
        }
    }
    
    public static void delay (int millisecs){
        try{
            Thread.currentThread ().sleep (millisecs);
        } catch (InterruptedException e){
        }
    }
}


well i used somebody's delay method which i "borrowed" from this site ;)
for some reason, it won't load startmenu.jpg.  The only thing I can think of right now, the file is way too big and it takes time to load, but the delay is only 2 seconds...

All help is appreciated :D

-----------------------------------
HellblazerX
Fri Jun 09, 2006 6:52 am


-----------------------------------
I've never seen that method used to load up images.  I only know two ways to load up an image.  First of course, is to use Swing's ImageIcon class, and the way you do this is like this:
Image title = new ImageIcon ("startmenu.jpg").getImage ();
The other is to use the ImageIO class,  which is supposedly be much more efficient.  You'll have import it in first:
import javax.ImageIO.*;

Image title = ImageIO.read (new File ("startmenu.jpg"));
However, I'm not sure ImageIO will work with normal Image objects, so you might have to switch to BufferedImages.  Also, it would probably best if you did the initialization of your Image objects inside the constructor.  That way, the program won't continue until the image has been loaded.

-----------------------------------
rizzix
Fri Jun 09, 2006 9:38 am


-----------------------------------
However, I'm not sure ImageIO will work with normal Image objects, so you might have to switch to BufferedImages.  Also, it would probably best if you did the initialization of your Image objects inside the constructor.  That way, the program won't continue until the image has been loaded.

ImageIO returns a BufferedImage. Since a BufferedImage is a subclass of Image, your above code will work.. and yes ImageIO can handle large images.

-----------------------------------
seymournexus
Fri Jun 09, 2006 6:14 pm


-----------------------------------
k this can be closed.  answer solved at school by changing a bit of code.  thanks to those who tried to help
