Link to home
Start Free TrialLog in
Avatar of babychicken
babychicken

asked on

Can not load a image with Toolkit.getDefaultToolkit.getImage()

Hi,
I am having trouble loading a image with the following code:

         image = Toolkit.getDefaultToolkit().getImage("image1.gif");
         MediaTracker mediaTracker = new MediaTracker(this);
          mediaTracker.addImage(image, 0);
          try
          {
               mediaTracker.waitForID(0);
          }
          catch (InterruptedException ie)
          {
               System.err.println(ie);
               System.exit(1);
          }
          addKeyListener(this);

          px = 200;
          py = 200;

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(500, 500);
          setVisible(true);
     }
     public void pant(Graphics g)
     {
          g.drawImage(image, px, 0 , this);
     }

     public void keyPressed (KeyEvent e)
     {
          int key = e.getKeyCode();

          switch (key)
          {
               case KeyEvent.VK_LEFT:
                    px += 100;
                    if (px < 0)
                         px = 200;
                    break;

               case KeyEvent.VK_RIGHT:
                    px -= 100;
                    if (px > 500)
                         px= 200;
                    break;

               case KeyEvent.VK_UP:
                    py -= 100;
                    if (py < 0)
                         py = 200;
                    break;

               case KeyEvent.VK_DOWN:
                    py += 100;
                    if (py > 500)
                         py = 200;
                         break;
          }
          repaint();
     }

Since there is no error message, it's really hard to know what's going on.
Thank you for your help.

Babychicken
Avatar of jmfrazier1
jmfrazier1

Babychicken,

I have used your code exactly as you have published it and i have made it display the image exactly as it should. what context are you using this in? I just added it to a JFrame and had no problem. are you sure that you have the correct path for your image file? i am also getting the movement of the image that you are requesting in the keypress event.  Could you give some more context as to how you have implemented this or would you like for me to post my code?

jmfrazier1
Avatar of Mayank S
Might just be that the "image1.gif" image is not in the same folder as the class file which you're running. Try specifying the complete path of the image as:

image = Toolkit.getDefaultToolkit ().getImage( "c:\\MyFolder\\MySubFolder\\image1.gif" ) ;

Hope that helps!

Mayank.
Avatar of babychicken

ASKER

First of all, thank you (jmfrazier1 and Mayank) for your comment.

jmfrazier1, do you mind post your code? I really cannot get it to work. I ran the code and it gave me a blank frame.

Mayank, I tried your suggestion (to specify the complete path) and it does not really work.

Thanks again for your help.

Babychicken
ASKER CERTIFIED SOLUTION
Avatar of jmfrazier1
jmfrazier1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
babychicken,

this code will make the image display and run properly. you can set the y position in the paint method to make the image go up and down.  if you have any problems jsut let me know at jfrazier@resolutions.ws  hope it works for you.

jmfrazier1


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class testfrm extends JFrame
 { public testfrm()
    { try
       { //adds your keylistener
         jbInit();
         //this loads the image and sets everything up for the keylistening
         dspImg();
         //setVisible(true);
       }
      catch(Exception e)
       { e.printStackTrace();
       }
    }
   public static void main(String[] args)
    { testfrm testfrm1 = new testfrm();
    }
   Image image;
   public void dspImg()
    { image = Toolkit.getDefaultToolkit().getImage("C:\\jfrazier\\My Documents\\myimage.gif");
      MediaTracker mediaTracker = new MediaTracker(this);
      mediaTracker.addImage(image, 0);
      try
       { mediaTracker.waitForID(0);
       }
      catch (InterruptedException ie)
       { System.err.println(ie);
         System.exit(1);
       }
      px = 200;
      py = 200;
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setSize(500, 500);
      setVisible(true);
    }
   public int px, py;
   public void paint(Graphics g)
    { g.drawImage(image, px, 0 , this);
    }
   private void jbInit() throws Exception
    { this.addKeyListener(new java.awt.event.KeyAdapter()
       { public void keyPressed(KeyEvent e)
          { this_keyPressed(e);
          }
       });
    }
   void this_keyPressed(KeyEvent e)
    { int key = e.getKeyCode();
      switch (key)
       { case KeyEvent.VK_LEFT:
           px += 100;
           if (px < 0)
            px = 200;
           break;
         case KeyEvent.VK_RIGHT:
           px -= 100;
           if (px > 500)
            px= 200;
           break;
         case KeyEvent.VK_UP:
           py -= 100;
           if (py < 0)
            py = 200;
           break;
         case KeyEvent.VK_DOWN:
           py += 100;
           if (py > 500)
             py = 200;
           break;
       }
      repaint();
    }
 }
Hi, jmfrazier1

Thanks for your help! It works like a charm.

Babychicken