Results 1 to 8 of 8
  1. #1
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default Capturing the drawing area

    Hi! I am not sure if this is the right place to ask this. I have a jpanel in which I can draw. What I want to do is to save an image of my drawing, when I click the snapshot button. I don't want to capture my whole panel, just the area that has been drawn. I have attached a pic of what I'm trying to do. Could you please help me?
    Attached Thumbnails Attached Thumbnails Capturing the drawing area-capturearea.jpg  
    Last edited by George R.; 10-18-2010 at 04:30 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Instead of drawing directly to the panel you could draw to a BufferedImage, then you would draw that image to the panel on repaint. This allows you to save the image to a file or to build other, smaller images from the bigger one.
    My Hobby Project: LegacyClone

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default

    Or read about the Robot class; it has a method that can capture (part of) the screen.

    kind regards,

    Jos

  4. #4
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Instead of drawing directly to the panel you could draw to a BufferedImage, then you would draw that image to the panel on repaint. This allows you to save the image to a file or to build other, smaller images from the bigger one.
    I already use a bufferedimage to draw in, I'm sorry that I didn't mention it. It has a predefined size.

    Quote Originally Posted by JosAH View Post
    Or read about the Robot class; it has a method that can capture (part of) the screen.

    kind regards,

    Jos
    Thanks for the info. I read it, and I see that you select the area that you want to capture, correct? I want to "auto-select" the drawing area. I don't know if it's possible. I'm thinking that I'll go with the "hand-selection" solution.

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

    Default

    I don't think you need to use Robot to capture a part of the screen of your own program/GUI.

    You can use BufferedImage#getSubimage(...) to get a part of the image. Note that the subimage uses the same backing data as the original image, so if you're frequently constructing images and extracting their subimages, you should paint the subimage to a new BufferedImage of smaller size and let the original image and its subimage become eligible for GC, or you may end up with a OOME.

    I want to "auto-select" the drawing area.
    You need to explain this better. I can think of several ways in which I might interpret it.

    db

  6. #6
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Thanks Darryl!
    Well, I have a bufferedImage and a toolbar with two options, pen or eraser. Let's say that the background color of the buffered image is black. When the user selects the pen, he/she draws and the color of the drawing is what user has selected (there is a color chooser too). When the user selects the eraser, he/she actually paints the bufferedImage black (the background color) in those points where mouse is dragged.

    What I want to do is this: when the user clicks the snapshot button, the program understands what the drawing area is, where the color is not only black (the background color), and it saves the image. The area is a rectangle, as I show on the pic on my first post of this thread.

    PS: I hope you understand what I mean! Sorry for my english!

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

    Default

    You'll need to iterate over the pixel array using BufferedImage#getRGB to determine the bounds of the non-black pixels.

    db

  8. #8
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Capturing a screenshot of a another pc?
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 06-08-2010, 05:07 PM
  2. capturing image
    By java_bond in forum Advanced Java
    Replies: 5
    Last Post: 03-04-2010, 07:45 AM
  3. help capturing input from drop down
    By javalearner1234 in forum New To Java
    Replies: 5
    Last Post: 08-17-2009, 05:00 AM
  4. MAC address capturing
    By viswa.tk in forum Java Applets
    Replies: 0
    Last Post: 12-13-2007, 08: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
  •