Java Swing: Graphics to Image?

Discussion in 'Java' started by Noddie, Mar 1, 2006.

  1. Noddie

    Noddie Guest

    Hi.
    I'm trying to get some sort of "screenshot" from a´Graphics object to an
    Image object. In detail I want to copy a tiny area from Graphics to an
    Image, so that can draw this part of the Graphics representation back to
    Graphics after something else was drawn there. (I have to implement a
    marquee which runs over an background image)

    Thanks for any hint, Noddie
    Noddie, Mar 1, 2006
    #1
    1. Advertising

  2. Noddie

    IchBin Guest

    Noddie wrote:
    > Hi.
    > I'm trying to get some sort of "screenshot" from a´Graphics object to an
    > Image object. In detail I want to copy a tiny area from Graphics to an
    > Image, so that can draw this part of the Graphics representation back to
    > Graphics after something else was drawn there. (I have to implement a
    > marquee which runs over an background image)
    >
    > Thanks for any hint, Noddie

    public class ScreenShot
    {
    public static void main(String[] args)
    {
    try
    {
    // Get the screen size
    Dimension screenSize =
    Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle rectangle = new Rectangle(0, 0, screenSize.width,
    screenSize.height);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(rectangle);
    File file;

    // Save the screenshot as a png
    file = new File("screen.png");
    ImageIO.write(image, "png", file);

    } catch (Exception e)
    {
    System.out.println(e.getMessage());
    }
    }
    }


    --

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA
    http://weconsultants.servebeer.com/JHackerAppManager
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
    IchBin, Mar 1, 2006
    #2
    1. Advertising

  3. Noddie

    Oliver Wong Guest

    "Noddie" <> wrote in message
    news:4405e001$0$12642$...
    > Hi.
    > I'm trying to get some sort of "screenshot" from a´Graphics object to an
    > Image object. In detail I want to copy a tiny area from Graphics to an
    > Image, so that can draw this part of the Graphics representation back to
    > Graphics after something else was drawn there. (I have to implement a
    > marquee which runs over an background image)
    >
    > Thanks for any hint, Noddie


    The "Graphics" object is like a pen, and a "Image" object is like a
    sheet of paper. That is, the drawing is stored in the "Image" object, and
    not in the "Graphics" object, just like the drawing is stored on the paper,
    and not in the pen.

    Thus it does not make sense to take drawing data out of a Graphic object
    to put into an Image object, since the Graphic object doesn't contain any
    drawing data.

    You need to get the surface onto which the Graphic draws upon, and get
    the drawing data from that.

    - Oliver
    Oliver Wong, Mar 1, 2006
    #3
  4. Noddie wrote:
    > I'm trying to get some sort of "screenshot" from a´Graphics object


    Since Graphics objects as such don't hold any image or drawing data,
    except from some state, this is not going to happen. A Graphics object
    just provides a drawing context to "something". You need to get that
    "something" and check if/how you can get some image data out of it.

    > Image object. In detail I want to copy a tiny area from Graphics


    Graphics doesn't hold image data.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Mar 2, 2006
    #4
  5. Noddie

    johndude

    Joined:
    Jun 1, 2007
    Messages:
    1
    Buffer it

    Use double buffering, that is, instead of drawing direct to the screen, draw to an Image first, call theImage.getGraphics() then draw to that, dump the image to the screen/panel/whatever when it is drawn. You can then create a new image from any rectangle of your buffer image.
    johndude, Jun 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    489
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    663
    mkrause
    May 6, 2005
  3. lizard
    Replies:
    0
    Views:
    1,753
    lizard
    Jan 30, 2006
  4. S.T
    Replies:
    2
    Views:
    566
  5. Knute Johnson

    Swing is dead! Long live Swing.

    Knute Johnson, Feb 16, 2012, in forum: Java
    Replies:
    32
    Views:
    3,730
    Daniel Pitts
    Feb 29, 2012
Loading...

Share This Page