Improved error handling when creating offscreen image for

double-buffering.  Don't paint the panel if it's not yet been laid out.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@845 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2002-01-07 23:05:39 +00:00
parent be4b5c3801
commit b13cdabeb0
@@ -1,9 +1,12 @@
// //
// $Id: AnimatedPanel.java,v 1.4 2001/12/16 08:04:25 mdb Exp $ // $Id: AnimatedPanel.java,v 1.5 2002/01/07 23:05:39 shaper Exp $
package com.threerings.media.sprite; package com.threerings.media.sprite;
import java.awt.*; import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JPanel; import javax.swing.JPanel;
@@ -31,20 +34,19 @@ public class AnimatedPanel extends JPanel implements AnimatedView
// documentation inherited // documentation inherited
public void paintComponent (Graphics g) public void paintComponent (Graphics g)
{ {
// create the offscreens if they don't yet exist // create the offscreens if they don't yet exist
if (_offimg == null) { if (_offimg == null && !createOffscreen()) {
createOffscreen(); return;
} }
// give sub-classes a chance to do their thing // give sub-classes a chance to do their thing
render(_offg); render(_offg);
// Rectangle bounds = getBounds(); // Rectangle bounds = getBounds();
// Log.info("paintComponent [bounds=" + bounds + "]."); // Log.info("paintComponent [bounds=" + bounds + "].");
// _offg.drawRect(bounds.x, bounds.y, bounds.width-1, bounds.height-1);
// draw the offscreen to the screen // draw the offscreen to the screen
g.drawImage(_offimg, 0, 0, null); g.drawImage(_offimg, 0, 0, null);
} }
/** /**
@@ -58,14 +60,23 @@ public class AnimatedPanel extends JPanel implements AnimatedView
} }
/** /**
* Creates the offscreen image and graphics context to which we * Creates the offscreen image and graphics context to which we draw
* draw the scene for double-buffering purposes. * the scene for double-buffering purposes. Returns whether the
* offscreen image was successfully created.
*/ */
protected void createOffscreen () protected boolean createOffscreen ()
{ {
Dimension d = getSize(); Dimension d = getSize();
_offimg = createImage(d.width, d.height); try {
_offg = _offimg.getGraphics(); _offimg = createImage(d.width, d.height);
_offg = _offimg.getGraphics();
return true;
} catch (Exception e) {
Log.warning("Failed to create offscreen [e=" + e + "].");
Log.logStackTrace(e);
return false;
}
} }
// documentation inherited // documentation inherited
@@ -88,6 +99,12 @@ public class AnimatedPanel extends JPanel implements AnimatedView
*/ */
public void paintImmediately () public void paintImmediately ()
{ {
if (!isValid()) {
// don't paint anything until we've been fully laid out
// Log.warning("Attempted to paint invalid panel.");
return;
}
Graphics g = null; Graphics g = null;
try { try {
@@ -98,7 +115,7 @@ public class AnimatedPanel extends JPanel implements AnimatedView
if (pcg != null) { if (pcg != null) {
g = pcg.create(); g = pcg.create();
pcg.dispose(); pcg.dispose();
paint(g); paintComponent(g);
} }
} catch (NullPointerException e) { } catch (NullPointerException e) {