Results 1 to 11 of 11
  1. #1
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default copyArea from 1 image to another

    Okay so. Lets say we have image1, now we want image2 to equal a specified area of image1.

    I know the graphics class has a method called copyArea which I thought was going to be very useful, but it turned out that it only copies an area and translates it to another position on the same pane that the Graphics class was painting on.

    so how do I get the copied area onto the new image?

    Also, when i get that i'm going to want it to stretch to the width of the applet, how do you stretch an image?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Create a BufferedImage, then you can use its Graphics object to draw from the original image.

    Background Panel shows how you can scale an image.
    Last edited by camickr; 04-30-2010 at 02:59 AM.

  3. #3
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    How do you use Graphics objects to draw from the original image? lol... that was my question, how can i draw to image 2 using image 1's graphics, and you said use Graphics object to draw from the original image....

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Create a BufferedImage using one of its constructors. You'll need to get the size of the original image for two of the parameters and decide on a BufferedImage graphics type. Then get the Graphics object from the BufferedImage via its getGraphics() method. You should then be able to draw an image on this with the Graphics#drawImage(....) method. Experiment. Search the forum for examples. Have fun!

    edit: I see that you want to draw a specified area of the original image. So your BufferedImage width and height will be the width and height of this specified area. I believe that drawImage will have an overload that will allow you to specify a region of the original image.
    Last edited by Fubarable; 04-30-2010 at 02:03 AM.

  5. #5
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    What do you mean a BufferedImage graphics type?

    when i look through the API i end up getting into a bunch of differant classes.

    Is there no simple way to take what's been custom painted to a JLabel(have it all in an image as well as it paints to the image before painting the image to the label), take an area of it(100*100 rectangle around where mouse is clicked), save that into an image, and scale that to the width and height of the JLabel and paint it on top of it? Like zooming in kind of thing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cruncher View Post
    What do you mean a BufferedImage graphics type?
    Please look up the BufferedImage API to see the public static final fields available for use as an int parameter for its constructor. Then experiment and see what happens. You can't break your computer! :)


    Is there no simple way to take what's been custom painted to a JLabel(have it all in an image as well as it paints to the image before painting the image to the label), take an area of it(100*100 rectangle around where mouse is clicked), save that into an image, and scale that to the width and height of the JLabel and paint it on top of it? Like zooming in kind of thing.
    I'm not pro at this, but what we're recommending here isn't that hard. Again, play with it. Again, search the forum as there are plenty of examples of use of BufferedImages.

    Luck!

  7. #7
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    I got it.

    Thanks for pointing me towards BufferedImage, wasn't aware of it's existance.

    By the way, just a side note, is there any specific reason to use Graphics2D over Graphics?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Thanks for pointing me towards BufferedImage, wasn't aware of it's existance.
    Really? What did the first reply suggest?

  9. #9
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Really? What did the first reply suggest?
    you said BufferImage, i thought you were refering to using an image as an offscreen buffer, i didn't realise you were referring to a class

  10. #10
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    And i also couldn't just take the graphics from the image, i just said bufferedImage = bufferedImage.getSubimage(x, y, w ,h);

    Regards...

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    BufferImage, i thought you were refering to using an image as an offscreen buffer
    And when you read the API, what class did you find? Is it such a leap to think a typo was made? Or better yet why did you not question it?

    And i also couldn't just take the graphics from the image,
    I didn't suggest that.

    i just said bufferedImage = bufferedImage.getSubimage(x, y, w ,h);
    No idea what you are suggesting a BufferedImage is created empty, so there is no sub image to get, that is why I suggested you need to draw the original image onto it.

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 01:25 PM
  4. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •