3

How do I paint a Component, like a JPanel, into a Graphics of my choice, as if it were teh screen?

Had no luck on google.

What I have tried:

JPanel p = new JPanel();
p.setSize(32, 32);
p.setLocation(8, 8);

p.add(new JLabel(...));
p.add(new JLabel(...));
p.add(new ImageIcon(...));

Graphics g = getSomeGraphics();
p.paint(g);

however, this last instruction causes

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.RepaintManager.getVolatileOffscreenBuffer(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    ...
pudiva
  • 274
  • 5
  • 15
  • This answer might help you..http://stackoverflow.com/questions/14506485/jpanel-not-displaying-in-jframe-after-paint-is-called/14508757#14508757 – Vishal K Feb 22 '13 at 19:32
  • How are you getting the `Graphics`? Generally one would use a `BufferedImage` for this type of thing, and paint onto that (using its `Graphics`). I assume you're basically trying to paint a component into an image, although I'm not quite sure. – Eric Galluzzo Feb 22 '13 at 19:33
  • 2
    Chances are you're using `getGraphics` - don't do this. See [this post](http://stackoverflow.com/questions/14966134/java-2d-api-null-pointer-exception/14966445#14966445) – Reimeus Feb 22 '13 at 19:33
  • 1
    possible duplicate of [Java Swing : Obtain \`Image\` of JFrame](http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe) – Andrew Thompson Feb 22 '13 at 19:35
  • Not a JFrame, just a Component! I must paint only teh JPanel, not teh whole Frame. And you could assume that I'm drawing into a BufferedImage, which I do sometimes, in fact. – pudiva Feb 22 '13 at 19:41
  • You mean for example draw a rectangle on JPanel ? – Azad Feb 22 '13 at 19:50
  • Unless the component is realised on the screen, use printAll instead. Calling paint assumes that the component is, on one way, attached to a native peer – MadProgrammer Feb 22 '13 at 20:17

2 Answers2

2

This example illustrates using panel.paint(g2d) to render an existing JPanel of JLabel instances in a BufferedImage. The image is then scaled and displayed below the panel for reference.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I just realized this works the same way as @Andrew's [example](http://stackoverflow.com/a/5853992/230513). – trashgod Feb 22 '13 at 19:54
  • And what if I can't make a JFrame to put my component into? – pudiva Feb 22 '13 at 20:36
  • Your example works because it add() teh Component to another Container, a JPanel, but, in my app, I have no JFrame(), nor any other Container. – pudiva Feb 22 '13 at 20:39
  • @camickr's [approach](http://stackoverflow.com/a/15035642/230513) may work in this context. – trashgod Feb 23 '13 at 04:50
1

And you could assume that I'm drawing into a BufferedImage, which I do sometimes, in fact.

See Screen Image, which can also create a component image.

camickr
  • 321,443
  • 19
  • 166
  • 288