Results 1 to 4 of 4
  1. #1
    cel0x is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Loading a image with Image and ImageIcon

    Hey guys,

    I am trying to load a picture and i really don't know what i am doing wrong. I have been reading up on Image and ImageIcon but i still can't oad the image.

    Here is my code:

    Java Code:
    package oop_po2;
    
    import java.awt.Image;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    import javax.swing.ImageIcon;
    
    public class OOP_PO2_Test {
    
        public Image getImage(String filename) {
            Image image = null;
            ImageIcon tattoo = new ImageIcon(filename);
            return image;
        }
    
        public static void main(String args[]) throws FileNotFoundException {
    
            OOP_PO2_Test app = new OOP_PO2_Test();
            
            
            
            Image image = app.getImage("tattoo.jpeg"); // TODO OOP_PO2_A2: Fill in the path to an existing image
            if (image != null && image instanceof Image) {
                System.out.println("Successfully loaded the image file.");
            } else {
                System.out.println("Nothing loaded. Try again.");
            }
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Loading a image with Image and ImageIcon

    I have been reading up on Image and ImageIcon
    Have you gone through How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) ?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    cel0x is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Loading a image with Image and ImageIcon

    Yes i have also been reading Image : Java Glossary

    It seems i just don't get it..... and yes i am still a beginner with JAVA :)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: Loading a image with Image and ImageIcon

    What do you think this method returns?
    Java Code:
        public Image getImage(String filename) {
            Image image = null;
            ImageIcon tattoo = new ImageIcon(filename);
            return image;
        }
    Also note that it's superfluous to construct an ImageIcon only for the sake of retrieving its Image; either use the ImageIcon as an Icon or load the Image using ImageIO#read(...).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  2. Help needed setting ImageIcon image
    By ben1989 in forum Java Applets
    Replies: 20
    Last Post: 05-05-2010, 03:16 AM
  3. ImageIcon won't load my image
    By JaiRaj in forum AWT / Swing
    Replies: 5
    Last Post: 03-04-2010, 06:35 PM
  4. How to insert Image using ImageIcon
    By anjali.wadhwa in forum New To Java
    Replies: 5
    Last Post: 01-15-2010, 11:08 PM
  5. ImageIcon : Image not displayed
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:36 AM

Posting Permissions

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