Results 1 to 5 of 5
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Resize BufferedImage

    Java Code:
    BufferedImage img;
        
    public Background() {
            img=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
    
            try {
                img = ImageIO.read(new File("src\\projectone\\pic\\redBox.png"));
            } catch (IOException e) {
            }
    I want to resize this image. I've searched google, but I couldn't find a solution that I understand (after all, the goal is to learn, not just to finish). How do I do this? Do I need a new image? Quality is not important now, but I would prefer to learn a method that I will be able to use in the future when quality does matter.

    Is there a simple solution?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Resize BufferedImage

    Since BufferedImage subclasses Image, you can use the getScaledInstance method. Check out the JDK API for the usage.

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

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Resize BufferedImage

    Java Code:
    imgScaled=(BufferedImage) img.getScaledInstance(300, 300, ?????);
    Would the above work? What do I put for the third parameter? (Image.SCALE_SMOOTH didn't work).


    I have seen multiple sites warn against using the getScaledInstance method. Is there a good reason not to use it?

    Thanks.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Resize BufferedImage

    Well, I have never had a problem with it and have used it quite a bit. You cannot do what you are doing above because the getScaledInstance() method returns an Image reference which cannot be cast to BufferedImage. However, you can use a graphics context to write an image into a BufferedImage. As you do that you can scale it as well. Check out the Graphics API as well as the Image and BuffereImage API. The methods are well documented.

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

  5. #5
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Resize BufferedImage

    Awesome thanks. I finally settled on this code:
    Java Code:
        BufferedImage img,image;
        Graphics2D pnt;
        
        public Background() {
            img=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
            pnt=img.createGraphics();
            try {
                image = ImageIO.read(new File("src\\mariowun\\pic\\redBox.png"));
            } catch (IOException e) {
            }
            
            pnt.drawImage(image.getScaledInstance(500, 500, Image.SCALE_SMOOTH),0,0,this);
    It doesn't feel like I am doing everything right, but it works.

Similar Threads

  1. GUI resize issue
    By dhazwa in forum AWT / Swing
    Replies: 1
    Last Post: 08-25-2011, 12:21 AM
  2. can't resize my JLabel
    By gib65 in forum AWT / Swing
    Replies: 10
    Last Post: 06-25-2010, 06:30 AM
  3. how to resize an image
    By marodia in forum AWT / Swing
    Replies: 2
    Last Post: 08-21-2009, 05:10 AM
  4. Image Resize
    By jithan in forum New To Java
    Replies: 4
    Last Post: 11-07-2008, 10:26 AM
  5. Resize frame
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-30-2007, 12:18 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
  •