Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Post "BufferedImage" Modifies My Images' Pixels

    Hello, fellow programmers!

    As implied by the title, when I am rendering images of the type "BufferedImage" unto a Swing application, the images' pixels are not consistent in size. Some might be larger than other, and some might be smaller than other.

    Here is a screenshot of what I am talking about (you might need to zoom in a bit because the image is so small): http://i.imgur.com/u1loEpD.png

    And here is also some code for you. The images are actually from a sprite sheet, which I first load in its entirety and then split up in an array.

    Java Code:
    public static BufferedImage sprites[];
    
    ...
    
    public void loadSprites(){
    	try{
    		BufferedImage bigImage = ImageIO.read(new File("SOURCE/BLA/BLA/THIS/IS/ACTUALLY/NOT/THE/REAL/SOURCE/IN/CASE/YOU'RE/WONDERING/I/JUST/DON'T/WANT/YOU/TO/FIND/ME/AND/RAPE/ME"));
    			
    		sprites = new BufferedImage[16 * 16];
    			
    		for(int x = 0; x < 16; x++){
    			for(int y = 0; y < 16; y++){
    				sprites[(x * 16) + y] = bigImage.getSubimage(y * 16, x * 16, 16, 16);
    			}
    		}
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    }
    
    ...
    
    g.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), (int)player.getWidth(), (int)player.getHeight(), null);
    So, how do I make the pixels equally small?

    Thanks in advance!
    Last edited by Zelaine; 06-27-2014 at 01:44 AM.

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

    Default Re: "BufferedImage" Modifies My Images' Pixels

    What does the original image look like? Perhaps you should attach the file so we can see the difference. I do not believe it is a pixel problem.

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

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Yes, of course. Here you go: imgur: the simple image sharer

    Maybe it looks that way because it is extracted from such a big image?

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

    Default Re: "BufferedImage" Modifies My Images' Pixels

    I can read the single image just fine. But if I am to try and figure out what is wrong you need to send me the bigImage of sprites.

    Regards,
    Jim
    Last edited by jim829; 06-27-2014 at 02:24 AM. Reason: Correction
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Yes.
    &quot;BufferedImage&quot; Modifies My Images' Pixels-spritesheet.png
    Last edited by Zelaine; 06-27-2014 at 11:47 AM.

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

    Default Re: "BufferedImage" Modifies My Images' Pixels

    I am not certain what you are doing but your supplied code reads them fine. Here is your code incorporated into a simple JFrame for display.

    Java Code:
    public class Snippet extends JPanel {
       JFrame frame;
       BufferedImage bigImage;
       public Snippet() {
          frame = new JFrame();
          frame.add(this);
          frame.setPreferredSize(new Dimension(500,500));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setVisible(true);
       }
       public static BufferedImage sprites[] = new BufferedImage[256];
       
       public static void main(String [] args) {
          Snippet s = new Snippet();
          s.loadSprites();
          s.repaint();
       }
       public void loadSprites(){
           try{
               bigImage = ImageIO.read(new File("f:/spritesheet.png"));
               sprites = new BufferedImage[16 * 16];
               for(int x = 0; x < 16; x++){
                   for(int y = 0; y < 16; y++){
                       sprites[(x * 16) + y] = bigImage.getSubimage(y * 16, x * 16, 16, 16);
                   }
               }
           }catch(IOException e){
               e.printStackTrace();
           }
       }
        
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (sprites[0] == null) {
             return;
          }
          g.drawImage(sprites[0],100,100,300,300,null);
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Methinks the code of the OP has some additional bits and bops not shown, such as setting antialiassing and/or resampling hints
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: "BufferedImage" Modifies My Images' Pixels

    I was wondering if the OP is doing anything (eg scaling) with the images prior to display.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    No, I do not do any scaling prior to display, however, I do actually set antialiasing hints. Am I setting them incorrectly, maybe?

    Java Code:
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    g = (Graphics2D) image.getGraphics();
    		
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    
    ...
    
    public void draw(){
    	Graphics g2 = this.getGraphics();
    	g2.drawImage(image, 0, 0, null);
    	g2.dispose();
    }
    EDIT: I tried disabling the rendering hints, but it didn't make any difference.
    Last edited by Zelaine; 06-27-2014 at 11:38 AM.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Try not doing that at all and see if it makes a difference. Probably not, but at least it will exclude it from the list of possibilities.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Yeah, it didn't... :(

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "BufferedImage" Modifies My Images' Pixels

    What you describe -sounds- like rescaling/resizing artifacts. But with only a snippet of your code, that's impossible to prove.

    Did you try running Jim's code? Do you see the same there?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    I tried running his code, and there it rendered without any issues whatsoever. So I'm not sure what's so special about my code.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Where do you draw a sprite onto the 'image' that is then used in the draw() method?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Before, in another method.

    Java Code:
    g.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), (int)player.getWidth(), (int)player.getHeight(), null);
    Should I send you the whole project? Maybe it'll be easier finding the issue then.
    Last edited by Zelaine; 06-27-2014 at 12:02 PM.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Well I'd next suspect the drawing code rather than the loading code. What happens when you reduce it to only:

    Java Code:
    g.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), null);
    Quote Originally Posted by Zelaine View Post
    Should I send you the whole project?
    No. The best I'm going to do is help you so you figure it out in the time slots I allow myself to do that while I'm at work, I'm not going to be digging through your code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Do you mean if I remove the draw function and only use the code you posted? Nothing gets drawn at all if I do that. The draw function shows what I've drawn so far on the screen, attaching the image to the screen if you will. So that's probably why nothing gets shown.
    Last edited by Zelaine; 06-27-2014 at 12:08 PM.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Are you sure the player getWidth and getHeight values match the width and height of the sprite being drawn?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: "BufferedImage" Modifies My Images' Pixels

    Yes.

    Wait a second! When I made the image four times bigger, before it was only 40 by 40 pixels, it rendered completely finely. Maybe the picture was too small for its own good?

    EDIT: Even only enlarging both sides by one pixel was enough for some reason.
    Last edited by Zelaine; 06-27-2014 at 12:25 PM.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: "BufferedImage" Modifies My Images' Pixels

    What do you mean by "enlarging"?
    The actual physical image , or the values passed around for width and height?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 2
    Last Post: 11-10-2012, 11:27 AM
  3. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  4. Replies: 0
    Last Post: 12-29-2009, 04:10 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •