?
Solved

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

Posted on 2003-03-31
6
Medium Priority
?
337 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:babychicken
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:jmfrazier1
ID: 8243588
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8245182
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.
0
 

Author Comment

by:babychicken
ID: 8245944
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
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 1

Accepted Solution

by:
jmfrazier1 earned 200 total points
ID: 8246121
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();
    }
 }
0
 
LVL 1

Expert Comment

by:jmfrazier1
ID: 8246160
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();
    }
 }
0
 

Author Comment

by:babychicken
ID: 8246865
Hi, jmfrazier1

Thanks for your help! It works like a charm.

Babychicken
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question