Results 1 to 4 of 4

Thread: adding images

  1. #1
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default adding images

    Hi, In my JPanel I have some components like this:
    Java Code:
     public void paintComponent(Graphics g) {
                setOpaque(false); 
           
                g.drawRect(0, 0,50, 50);
                g.setColor(Color.yellow);
                g.fillRect(0, 0,49,49);         
                }
    I would like to change it into a image using method g.drawImage(Image img, int i, int i1, ImageObserver io), but I don't know how to declare Images.

    If I use
    Java Code:
     ImageIcon img = new ImageIcon("D:/img.jpeg");
    the drawImage() method doesn't work saying it requires Image not ImageIcon
    If I use
    Java Code:
     Image img = new Image("D:/img.jpeg");
    I get the error: "Image is abstract, cannot be instantiated"

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: adding images

    Check out ImageIO, File, and BufferedImage in the Java API.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: adding images

    Look up the API for Image and go through the direct known subclasses. The first one is the one you want.

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

  4. #4
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: adding images

    Thanks, I had previously tried BufferedImage but I couldn't make it because I was using try catch in a wrong way.

Similar Threads

  1. Replies: 5
    Last Post: 05-21-2012, 11:33 AM
  2. adding different images using if statements
    By JoePenguin in forum New To Java
    Replies: 3
    Last Post: 02-16-2012, 04:54 AM
  3. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 07:19 AM
  4. Adding multiple images to frame
    By 435.mahesh in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2009, 09:31 PM
  5. [new] need help on GUI adding images
    By counterfox in forum AWT / Swing
    Replies: 5
    Last Post: 03-19-2009, 11:29 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
  •