Results 1 to 10 of 10
  1. #1
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default Converting BufferedImage Into ImageIcon

    How do I do it?
    I have a resize function that yses BufferedImage but I need an ImageIcon to put it into a JLabel?

    ImageIcon image = new ImageIcon("src/backgroundimage.jpg");
    BufferedImage resizedimage = resize(image,width,height); //error! type conversion
    JLabel logo = new JLabel( resizedimage );
    content.add(logo, BorderLayout.CENTER);

    //this class resizes the image

    public BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();
    return resizedImage;
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    And have you gone through the constructors of ImageIcon?

    Ever heard of ImageIO, by the way?

    db

  3. #3
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    I read the constructor and I saw this: ImageIcon(Image image). However, this is unrelated to BufferedImage...


    What is ImageIO? Do you mind giving me the answer...
    Last edited by hitmen; 03-22-2009 at 04:06 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you looked up ImageIO in the API yet? That's what I would do if Darryl had answered my question with this answer. Well, what does the API tell you?

  5. #5
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    BufferedImage resizedimage = (BufferedImage) image.getImage();
    JLabel logo = new JLabel(resizedimage );

    Now what is next?

  6. #6
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    I am converting the BufferedImage back into an ImageIcon. Now can somebody help me? The ImageIO api only specifies the opposite

  7. #7
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    BufferedImage resizedimage = (BufferedImage)image.getImage();
    Image resizedimage2 = (Image)resizedimage;
    ImageIcon resizedimage3 = new ImageIcon(resizedimage2);
    JLabel logo = new JLabel( resizedimage3 ) ;

    runtime error.
    Just give me the code. this is frustrating

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried to create your with ImagIO.read(...). If not, why not show us this code attempt? Also, if you get error, please tell us the error.

  9. #9
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    I can read the BufferedImage using Image.read()
    BUT now I need to convert the BufferedImage back into an ImageIcon. I dunno how to do this.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    BufferedImage extends Image.

    ImageIcon has a constructor that takes a parameter of type Image.

    If you don't understand inheritance of Type, go back to basics and come back to ImageIcon and UI related stuff when you have a decent foundation.
    The Java™ Tutorials

    db

Similar Threads

  1. BufferedImage: Uneven rescale
    By legsmacgee in forum Java 2D
    Replies: 2
    Last Post: 04-24-2009, 10:48 AM
  2. ImageIcon and size
    By wokena in forum Java 2D
    Replies: 3
    Last Post: 12-21-2008, 08:47 AM
  3. Replies: 0
    Last Post: 07-02-2008, 08:06 PM
  4. Convert Byte [] to BufferedImage
    By Smily in forum Advanced Java
    Replies: 3
    Last Post: 04-28-2008, 05:54 PM
  5. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 08:17 PM

Posting Permissions

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