Results 1 to 9 of 9
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default BufferedImage has white padding on top

    What I am doing is loading a new image from resources in my project, so that I can get the size. Using this, I create a new BufferedImage with those dimensions. The following code is what I am using to take the original BufferedImage, and scale it.
    Java Code:
    public ImageIcon getBackImage(){
    		before = new BufferedImage((int)img.getWidth(null), (int)img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    		
    		int w = before.getWidth();
    		int h = before.getHeight();
    		try{
    			URL url = getClass().getResource("/Blue_Back.png");
    			before = ImageIO.read(url);
    		}catch(IOException ioe){
    			ioe.printStackTrace();
    		}
    		after = new BufferedImage((int)(img.getWidth(null)*zoom), (int)(img.getHeight(null)*zoom + (10)), BufferedImage.TYPE_INT_ARGB);
    		AffineTransform at = new AffineTransform();
    		at.scale(zoom, zoom);
    		AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    		op.filter(before, after);
    		
    		ImageIcon ii = new ImageIcon(after);
    			
    		return ii;
    	}
    The scaling seems to be working fine, but what I have noticed is a line of approximately 10 pixels at the top of the image. I took the original image and blew it up to ensure that I wasn't just enlarging undesired portions and this wasn't the case. I then tried to fetch a subImage of the BufferedImage, and that also left the padding at the top. Is there something I am missing that is placing this undesired padding at the top of my bufferedImages ?
    Attached Thumbnails Attached Thumbnails BufferedImage has white padding on top-whitespace.jpg  

  2. #2
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: BufferedImage has white padding on top

    So, I have been able to find the white bar. But still am confused as to how it is there in the first place. It apparently is the background of a custom JPanel. The thing I don't understand is that the size of said custom JPanel is set to the zoomed size of the card. Which means they should be identical in size. Any idea ?

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

    Default Re: BufferedImage has white padding on top

    Did you use print statements to print the image dimensions and the JPanel dimensions once the scaled image was displayed? Are you
    certain you are setting the JPanel dimensions correctly?

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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: BufferedImage has white padding on top

    You can also define your own implementation for the Icon interface for testing purposes; it's extremely easy: you just have to return the dimensions of the image and you must be able to paint the image on the component somewhere; nobody knows (or cares) if you paint the image somewhere else on the component.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: BufferedImage has white padding on top

    For future reference, it might be easier to use the getScaledInstance() method which BufferedImage inherits from Image.

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

  6. #6
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: BufferedImage has white padding on top

    Thanks guys. Here is a little update. I worked backwards from BufferedImage first.. so I will most definetly go back and check out the getScaledInstance() method. Now as for whether I am setting the JPanel correctly or not, I know that I am doing it how I have been. I typically setPrefferedSize() and setSize(). In this case since it was a custom JPanel *(could that be the root of all my problems) I am passing into it the actual zoomCardSize dimension and using that to setPrefferedSize() and setSize() within the JPanel itself. I changed the code around and stopped passing in that variable. Instead, manually setting each panel, and still the same result.

    Putting in printlns showed dimensions were the same.

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

    Default Re: BufferedImage has white padding on top

    So what's the 10 for?

    Java Code:
    (int)(img.getHeight(null)*zoom + (10))
    Can you attach your image so we can try to re-create the problem?

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

  8. #8
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: BufferedImage has white padding on top

    Well, I added the + (10) because without it, the card bottom is pushed off the bottom of the JPanels viewable area. I added the extra 10 so that I was able to view the entire image (until I could ultimately figure out what was causing the card to be shifted down inside it's container panel).

    An image of a card with the above code subtract the " + (10) " .


    Images Uploaded - 1) Image showing the above code minus the "+(10)". This image is also showing an unusual drop on the last card. The method that populates those groups of cards (along with all the other groups) is simply an if else chain and has not been touched since the first picture of Aces. If card 1 !loaded, addCard, else goto next spot and try again. But always a drop on the last image. Regardless of how many I put in.

    2) Single playing card (.png) being used.



    ALSO : I remember now why I didn't use the getScaledInstance(). I remembered reading many things saying the performance wasn't great *(I understand a card game doesn't need huge performance), but also that it was from a Toolkit of days passed, and that there were better options available to the user nowadays as opposed to when that method first came around.
    Attached Thumbnails Attached Thumbnails BufferedImage has white padding on top-dropped.jpg   BufferedImage has white padding on top-.png  
    Last edited by rru96; 07-28-2014 at 03:09 AM.

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

    Default Re: BufferedImage has white padding on top

    Quote Originally Posted by rru96 View Post
    ALSO : I remember now why I didn't use the getScaledInstance(). I remembered reading many things saying the performance wasn't great *(I understand a card game doesn't need huge performance), but also that it was from a Toolkit of days passed, and that there were better options available to the user nowadays as opposed to when that method first came around.
    Java 8 still uses ToolKit in the getScaledInstance() method. And having read a lot of the source code, other
    external documents, and noticed how the authors' document that they strive to achieve good performance
    it surprises me that they wouldn't scale as efficiently as possible. But I have no other basis to argue one
    way or the other.

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

Similar Threads

  1. Help with padding arrays
    By sdee13 in forum New To Java
    Replies: 26
    Last Post: 04-04-2013, 06:23 AM
  2. Replies: 2
    Last Post: 06-29-2011, 10:08 AM
  3. BAd Padding exception
    By Bverly in forum Advanced Java
    Replies: 1
    Last Post: 09-26-2010, 10:48 PM
  4. Padding Problem
    By Srikar in forum Advanced Java
    Replies: 1
    Last Post: 08-20-2010, 09:39 PM
  5. padding informaiton for PKCS#5
    By nanaji in forum Advanced Java
    Replies: 3
    Last Post: 07-31-2008, 06: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
  •