This discussion is archived
1 Reply Latest reply: Jun 14, 2012 2:22 AM by gimbal2 RSS

java 2d image capturing without displaying the jcomponent

943617 Newbie
Currently Being Moderated
My application has a java JComponent where some strings and lines are drawn using the drawString(theStr, xPos, yPos) and drawLine(x1, y1, x2, y2) methods in Graphics2D class. I am able to get this using the createImage(width, height) method in the Component class. This is working fine as long as my JComponent is visible. My requirement is to create the image of the JComponent when it is NOT displayed / visible.

please help me on this issue.
  • 1. Re: java 2d image capturing without displaying the jcomponent
    gimbal2 Guru
    Currently Being Moderated
    Hm, a curious requirement.

    What you could try is to manually invoke the paint() method of the component to let it paint itself onto the BufferedImage.
    BufferedImage bi = new BufferedImage(...); // pass the proper dimensions, what you would pass to createImage()
    Graphics g = bi.createGraphics();
    comp.paint(g);
    g.dispose();
    I have no idea if this will be more successful than what you have already tried.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points