Changed AnimatedPanel to a JComponent from a Canvas because we no longer
need to extend the latter. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1207 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//
|
//
|
||||||
// $Id: AnimatedPanel.java,v 1.18 2002/03/08 01:37:51 mdb Exp $
|
// $Id: AnimatedPanel.java,v 1.19 2002/04/06 04:49:43 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.media.animation;
|
package com.threerings.media.animation;
|
||||||
|
|
||||||
import java.awt.AWTException;
|
import java.awt.AWTException;
|
||||||
import java.awt.BufferCapabilities;
|
import java.awt.BufferCapabilities;
|
||||||
import java.awt.Canvas;
|
import java.awt.Component;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
@@ -16,8 +16,13 @@ import java.awt.Rectangle;
|
|||||||
import java.awt.image.BufferStrategy;
|
import java.awt.image.BufferStrategy;
|
||||||
import java.awt.image.VolatileImage;
|
import java.awt.image.VolatileImage;
|
||||||
|
|
||||||
|
import javax.swing.event.AncestorEvent;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.samskivert.swing.event.AncestorAdapter;
|
||||||
import com.samskivert.util.Histogram;
|
import com.samskivert.util.Histogram;
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
|
|
||||||
@@ -26,11 +31,11 @@ import com.threerings.media.sprite.SpriteManager;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The animated panel provides a useful extensible implementation of a
|
* The animated panel provides a useful extensible implementation of a
|
||||||
* {@link Canvas} that implements the {@link AnimatedView} interface. It
|
* {@link JComponent} that implements the {@link AnimatedView} interface.
|
||||||
* takes care of automatically creating an animation manager and a sprite
|
* It takes care of automatically creating an animation manager and a
|
||||||
* manager to manage the animations that take place within this panel. If
|
* sprite manager to manage the animations that take place within this
|
||||||
* a sprite manager is not needed, {@link #needsSpriteManager} can be
|
* panel. If a sprite manager is not needed, {@link #needsSpriteManager}
|
||||||
* overridden to prevent it from being created.
|
* can be overridden to prevent it from being created.
|
||||||
*
|
*
|
||||||
* <p> Users of the animated panel must be sure to call {@link #stop} when
|
* <p> Users of the animated panel must be sure to call {@link #stop} when
|
||||||
* their panel is no longer being displayed and {@link #start} to start it
|
* their panel is no longer being displayed and {@link #start} to start it
|
||||||
@@ -40,7 +45,8 @@ import com.threerings.media.sprite.SpriteManager;
|
|||||||
* <p> Sub-classes should override {@link #render} to draw their
|
* <p> Sub-classes should override {@link #render} to draw their
|
||||||
* panel-specific contents.
|
* panel-specific contents.
|
||||||
*/
|
*/
|
||||||
public class AnimatedPanel extends Canvas implements AnimatedView
|
public class AnimatedPanel extends JComponent
|
||||||
|
implements AnimatedView
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constructs an animated panel.
|
* Constructs an animated panel.
|
||||||
@@ -55,6 +61,23 @@ public class AnimatedPanel extends Canvas implements AnimatedView
|
|||||||
_spritemgr = new SpriteManager();
|
_spritemgr = new SpriteManager();
|
||||||
_animmgr.setSpriteManager(_spritemgr);
|
_animmgr.setSpriteManager(_spritemgr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// turn off double buffering because we handle our own rendering;
|
||||||
|
// it's not enough to turn it off for this component, we need to
|
||||||
|
// climb all the way up the chain, otherwise one of our parents
|
||||||
|
// might be painting and decide to do its own double buffering
|
||||||
|
addAncestorListener(new AncestorAdapter() {
|
||||||
|
public void ancestorAdded (AncestorEvent event) {
|
||||||
|
Component c = AnimatedPanel.this;
|
||||||
|
while (c != null) {
|
||||||
|
if (c instanceof JComponent) {
|
||||||
|
((JComponent)c).setDoubleBuffered(false);
|
||||||
|
}
|
||||||
|
c = c.getParent();
|
||||||
|
}
|
||||||
|
setOpaque(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,27 +146,39 @@ public class AnimatedPanel extends Canvas implements AnimatedView
|
|||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void paint (Graphics g)
|
public void paint (Graphics g)
|
||||||
{
|
{
|
||||||
// we don't paint directly any more, we pass the dirty rect to the
|
// convert the clipping rectangle into the proper coordinates and
|
||||||
// animation manager to queue up along with our next repaint
|
// call into our rendering system
|
||||||
Rectangle dirty = g.getClipBounds();
|
Rectangle r = g.getClipBounds();
|
||||||
// we need to adjust the dirty rect by our viewport offset before
|
r.translate(_tx, _ty);
|
||||||
// passing it to the animation manager
|
_paintList.add(r);
|
||||||
dirty.translate(_tx, _ty);
|
// we have to tell paintImmediately() to use the graphics object
|
||||||
_animmgr.addDirtyRect(dirty);
|
// that we were passed, otherwise Swing will fuck everything to
|
||||||
|
// the high heavens
|
||||||
|
paintImmediately(g, _paintList);
|
||||||
|
_paintList.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void update (Graphics g)
|
public void paintImmediately (int x, int y, int w, int h)
|
||||||
{
|
{
|
||||||
// the normal Canvas.update() fills itself with its background
|
paintImmediately(new Rectangle(x, y, w, h));
|
||||||
// color before calling paint() which we definitely don't want
|
|
||||||
paint(g);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void validate ()
|
public void paintImmediately (Rectangle r)
|
||||||
{
|
{
|
||||||
super.validate();
|
// convert the clipping rectangle into the proper coordinates and
|
||||||
|
// call into our rendering system
|
||||||
|
r.translate(_tx, _ty);
|
||||||
|
_paintList.add(r);
|
||||||
|
paintImmediately(_paintList);
|
||||||
|
_paintList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
|
public void doLayout ()
|
||||||
|
{
|
||||||
|
super.doLayout();
|
||||||
|
|
||||||
// if we change size, clear out our old back buffer
|
// if we change size, clear out our old back buffer
|
||||||
_backimg = null;
|
_backimg = null;
|
||||||
@@ -166,6 +201,18 @@ public class AnimatedPanel extends Canvas implements AnimatedView
|
|||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
public void paintImmediately (List invalidRects)
|
public void paintImmediately (List invalidRects)
|
||||||
|
{
|
||||||
|
paintImmediately(null, invalidRects);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders this animated panel. If the supplied graphics reference is
|
||||||
|
* non-null, we will render to that instance, otherwise a graphics
|
||||||
|
* object will be obtained via a call to {@link #getGraphics}. This
|
||||||
|
* method, of course, must be called from the AWT thread or mayhem
|
||||||
|
* will ensue.
|
||||||
|
*/
|
||||||
|
protected void paintImmediately (Graphics gfx, List invalidRects)
|
||||||
{
|
{
|
||||||
// no use in painting if we're not showing or if we've not yet
|
// no use in painting if we're not showing or if we've not yet
|
||||||
// been validated
|
// been validated
|
||||||
@@ -300,7 +347,11 @@ public class AnimatedPanel extends Canvas implements AnimatedView
|
|||||||
|
|
||||||
// draw the back buffer to the screen
|
// draw the back buffer to the screen
|
||||||
try {
|
try {
|
||||||
g = getGraphics();
|
if (gfx == null) {
|
||||||
|
g = getGraphics();
|
||||||
|
} else {
|
||||||
|
g = gfx;
|
||||||
|
}
|
||||||
|
|
||||||
// if we're scrolling, we've got to copy the whole image
|
// if we're scrolling, we've got to copy the whole image
|
||||||
// to the screen, otherwise we can just copy the dirty
|
// to the screen, otherwise we can just copy the dirty
|
||||||
@@ -414,6 +465,7 @@ public class AnimatedPanel extends Canvas implements AnimatedView
|
|||||||
/** A histogram for tracking how frequently we're rendered. */
|
/** A histogram for tracking how frequently we're rendered. */
|
||||||
protected Histogram _histo = new Histogram(0, 1, 100);
|
protected Histogram _histo = new Histogram(0, 1, 100);
|
||||||
|
|
||||||
/** The number of buffers to use when rendering. */
|
/** Used to pass dirty regions supplied by the AWT to the rendering
|
||||||
protected static final int BUFFER_COUNT = 2;
|
* system. */
|
||||||
|
protected ArrayList _paintList = new ArrayList();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user