Results 1 to 9 of 9

Thread: HELP!!! Image Class

  1. #1

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228

    Arrow HELP!!! Image Class

    Can somone please tell me how to load an image file ( .jpg for example ) without extending the Applet class!!!! I know theres a way to get around this... Who has ideas?? Plz help...
    To protect time is to protect everything...

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I could only find this quote from the docs
    The image must be obtained in a platform-specific manner.
    And I could find the getImage/createImage(String) method of java.awt.Toolkit . But it is not static and Toolkit is abstract, so I don't know how to actually use it (besides Sun saying it should not be used directly).
    I could find java.awt.Component.createImage, but those two functions take either a width and a height or an ImageProducer. No ImageProducer I found takes a filename as input.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by CornedBee
    And I could find the getImage/createImage(String) method of java.awt.Toolkit . But it is not static and Toolkit is abstract, so I don't know how to actually use it (besides Sun saying it should not be used directly).
    I think either System or Toolkit has a getDefaultToolkit() method which is static. That is how to get a Toolkit instance.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You could use javax.swing.ImageIcon to load the image, then use it's getImage method to get the image.

    But that's not really great.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    This procedure will load your image for you.

    Code:
      private Image loadImage(String ImageName)
      {
        Image image = null;
    		Toolkit toolkit = Toolkit.getDefaultToolkit();
    		image = toolkit.getImage(ImageName);
    		MediaTracker mediaTracker = new MediaTracker(this);
    		mediaTracker.addImage(image, 0);
    		try
    		{
    			mediaTracker.waitForID(0);
    		}
    		catch (InterruptedException ie)
    		{
    			System.err.println(ie);
    			System.exit(1);
    		}
        return image;
      }
    To display it on a Panel, you'll need to create a paint method for the panel similar to this.

    Code:
    public void paint(Graphics graphics) {
        super.paintComponents (graphics);
               graphics.drawImage(loadImage("Hello.gif"), 140, 330, null);

  6. #6
    Member
    Join Date
    Jul 2002
    Posts
    50

    Question

    I am absolutely going insane over this, I CANNOT load any images into my apps. I tried everthing, I get no errors, but nothing works.

    I want to set the icon of my frame's window, I first tried this:

    myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage( new File("./Images/Bomberman.gif").getCanonicalPath()));

    It didn't work, then I tried the example that was last posted, still no such luck .... what the heck is going on??

  7. #7
    Member
    Join Date
    Jul 2002
    Posts
    50
    Ok, I found this, and it seems to work just fine:

    setIconImage(Toolkit.getDefaultToolkit().createImage(GameFrame.class.getResource("Images/icon.gif")));

    Damn .... thats alot to load just one image!

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You call one long line a lot for loading a gif-encoded image? You might want to try writing your own loading code.


    Always those high-level programmers, hm, hm...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Addicted Member Virtual24's Avatar
    Join Date
    May 2001
    Posts
    228
    The Toolkit.getDefaultToolkit() has many methods for opening images... However, I solved my problem by writing a class that loads images from an archived ( .zip ) file.
    To protect time is to protect everything...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width