Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why does getGraphics() return null on my offscreen image?

image null offscreen Return
0
Posted

Why does getGraphics() return null on my offscreen image?

0

Ans : The following code class MyFrame extends Frame { MyFrame() { Image offscreen = createImage(100,100); Graphics offg = offscreen.getGraphics(); } … } will usually not work, since the peer will not exist at this time. Without the peer for the Frame, you cannot succeed in creating offscreen Images. (There’s no problem creating Produced Images without a peer. Trying to draw them, of course, is another matter). One “standard” form of offscreen code looks like this: (note the reuse of the Graphics and Image objects for as long as possible) class Gumble extends java.awt.Something { private Image offi; private Graphics offg; public void update(Graphics g) { if (g == null) return; // Paranoia Dimension size = size(); if ( offi == null || offi.getWidth()!=size.width || offi.getHeight()!=size.height ) { if (offg!=null) offg.dispose(); offi = createImage(size.width, size.height); offg = offi.getGraphics(); // Regenerate offi here… } // If you use getClipBounds() here, // check that for be

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123