Results 1 to 3 of 3
  1. #1
    Cain is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Copy part of an image to another

    EDIT: I guess this should have been posted under the Java 2D forum. I'll repost there so this one can be deleted.

    Hello,

    I seem to be having some difficulty copying part of one image to another image.

    BufferedImage source // 160 pixels x 224 pixels
    BufferedImage dest // 16 pixels by 16 pixels

    What I want to do is copy a 16x16 tile from source into dest. Lets say I want to copy tile 2,2 (pixels 32-48, 32-48)

    example :

    Graphics g = dest.getGraphics();
    g.drawImage(source, 0, 0, 16, 16, 32, 32, 16, 16, null);

    This bit of code doesn't work because it seems to try to squish the entire source image into the destination image. Then I found some code that says use Graphics.setClip():

    Graphics g = dest.getGraphics();
    g.setClip(32,32,16,16)
    g.drawImage(source, 0, 0, null)

    But that doesn't work either.

    I must be missing something simple because it can't be that hard to pick out a 16 x 16 tile from an image thats 160 x 224 pixels big.
    Last edited by Cain; 04-16-2009 at 08:25 PM.

  2. #2
    cwill8469 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Possible solution

    I'm not sure how efficient this would be, but I would create another BufferedImage that is the size of the tile you want:

    BufferedImage tile = new BufferedImage(16,16,BufferedImage.<whatever type>);

    Then I would draw to it with an offset that would result in only the correct region being drawn:

    Graphics g = tile.getGraphics();
    g.drawImage(source,-32,-32);

    tile should now contain the region you're looking for.

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

Similar Threads

  1. Object copy
    By Oktam in forum New To Java
    Replies: 6
    Last Post: 07-21-2010, 08:09 AM
  2. Attachment referencing image part
    By jissondennis in forum Advanced Java
    Replies: 0
    Last Post: 03-16-2009, 06:56 AM
  3. USB Device Copy
    By Mir in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 11:44 AM
  4. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  5. copy image/imageicon into a file on disk
    By archanajathan in forum Advanced Java
    Replies: 2
    Last Post: 11-22-2007, 06:21 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
  •