Results 1 to 4 of 4
  1. #1
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default How can you get a buffered image to resize to fill a panel?

    So for example you have a picture that is 20x20 pixels in its natural size. Your panel is 100x100 pixels. Resizing the panel is not an option because there is no guarantee the picture will always be 20x20.

    Currently the way I am drawing the picture is:

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
     
    //@SuppressWarnings("serial")
    public class DisplayImage extends JPanel
    {
    	BufferedImage image;
    	public DisplayImage(String picPath)
    	{
    			File input = new File(picPath);
    			image = ImageIO.read(input);
    	}
    	
    	public DisplayImage(LayoutManager layout, String picPath)
    	{
    		this(picPath);
    		setLayout(layout);
    		setDoubleBuffered(true);
    		setOpaque(true);
    		updateUI();  
    	}
    	
    	public void setImage(String picPath)
    	{
    			File input = new File(picPath);
    			image = ImageIO.read(input);
    	}
    	
    	public void paint(Graphics g)
    	{
    		g.drawImage(image,  0, 0, null);
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How can you get a buffered image to resize to fill a panel?

    Isn't there a drawImage() that lets you specify the target size?

  3. #3
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: How can you get a buffered image to resize to fill a panel?

    Yeah there is, thanks that did the trick

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

    Default Re: How can you get a buffered image to resize to fill a panel?

    Moved from New to Java

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

Similar Threads

  1. Resize the popup panel
    By Charles I in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 01-27-2012, 07:02 AM
  2. Buffered Image and DataBuffer
    By ErdyMurphy in forum Java 2D
    Replies: 1
    Last Post: 04-19-2011, 04:55 AM
  3. Volatile image vs buffered image
    By trishtren in forum Java 2D
    Replies: 0
    Last Post: 04-17-2011, 11:31 PM
  4. Drawing onto a buffered image
    By trishtren in forum Java 2D
    Replies: 3
    Last Post: 04-09-2011, 09:23 PM
  5. Rotating Buffered Image distorts image
    By VortexSpin in forum Java 2D
    Replies: 1
    Last Post: 02-13-2011, 05:54 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
  •