diff --git a/src/java/com/threerings/media/FrameManager.java b/src/java/com/threerings/media/FrameManager.java index ef73e9e2..2041db30 100644 --- a/src/java/com/threerings/media/FrameManager.java +++ b/src/java/com/threerings/media/FrameManager.java @@ -42,7 +42,6 @@ import java.awt.EventQueue; import javax.swing.JLayeredPane; import javax.swing.RepaintManager; import javax.swing.JRootPane; -import javax.swing.RootPaneContainer; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; @@ -122,15 +121,30 @@ public abstract class FrameManager { } + /** + * Provides a bridge between either {@link ManagedJFrame} or {@link + * ManagedJApplet} and the frame manager. + */ + public static interface ManagedRoot + { + /** Configures the root with a reference to its frame manager. */ + public void init (FrameManager fmgr); + + /** Returns the window at the root of the UI hierarchy. */ + public Window getWindow (); + + /** Returns the top-level Swing pane. */ + public JRootPane getRootPane(); + } + /** * Creates a frame manager that will use a {@link SystemMediaTimer} to * obtain timing information, which is available on every platform, but * returns inaccurate time stamps on many platforms. * - * @see #newInstance(Window, RootPaneContainer, MediaTimer) + * @see #newInstance(ManagedRoot, MediaTimer) */ - public static FrameManager newInstance ( - Window window, RootPaneContainer root) + public static FrameManager newInstance (ManagedRoot root) { // first try creating a PerfTimer which is the best if we're using // JDK1.4.2 @@ -142,20 +156,14 @@ public abstract class FrameManager "System.currentTimeMillis() based timer."); timer = new SystemMediaTimer(); } - return newInstance(window, root, timer); + return newInstance(root, timer); } /** - * Constructs a frame manager that will do its rendering to the - * supplied frame. It is likely that the caller will want to have put - * the frame into full-screen exclusive mode prior to providing it to - * the frame manager so that the frame manager can take advantage of - * optimizations available in that mode. - * - * @see GraphicsDevice#setFullScreenWindow + * Constructs a frame manager that will do its rendering to the supplied + * root. */ - public static FrameManager newInstance ( - Window window, RootPaneContainer root, MediaTimer timer) + public static FrameManager newInstance (ManagedRoot root, MediaTimer timer) { FrameManager fmgr; if (_useFlip.getValue()) { @@ -165,21 +173,18 @@ public abstract class FrameManager Log.info("Creating back frame manager."); fmgr = new BackFrameManager(); } - fmgr.init(window, root, timer); + fmgr.init(root, timer); return fmgr; } /** * Initializes this frame manager and prepares it for operation. */ - protected void init ( - Window window, RootPaneContainer root, MediaTimer timer) + protected void init (ManagedRoot root, MediaTimer timer) { - _window = window; + _window = root.getWindow(); _root = root; - if (window instanceof ManagedJFrame) { - ((ManagedJFrame)window).init(this); - } + _root.init(this); _timer = timer; // set up our custom repaint manager @@ -773,7 +778,7 @@ public abstract class FrameManager protected Window _window; /** Provides access to our Swing bits. */ - protected RootPaneContainer _root; + protected ManagedRoot _root; /** Used to obtain timing measurements. */ protected MediaTimer _timer; diff --git a/src/java/com/threerings/media/ManagedJApplet.java b/src/java/com/threerings/media/ManagedJApplet.java new file mode 100644 index 00000000..37ad6666 --- /dev/null +++ b/src/java/com/threerings/media/ManagedJApplet.java @@ -0,0 +1,95 @@ +// +// $Id$ +// +// Nenya library - tools for developing networked games +// Copyright (C) 2002-2006 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/nenya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.media; + +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Insets; +import java.awt.Rectangle; +import java.awt.Shape; +import java.awt.Window; +import javax.swing.JApplet; + +import com.threerings.media.Log; + +/** + * When using the {@link FrameManager} in an Applet, one must use this + * top-level class. + */ +public class ManagedJApplet extends JApplet +{ + // from interface FrameManager.ManagedRoot + public void init (FrameManager fmgr) + { + _fmgr = fmgr; + } + + // from interface FrameManager.ManagedRoot + public Window getWindow () + { + Component parent = getParent(); + while (!(parent instanceof Window) && parent != null) { + parent = parent.getParent(); + } + return (Window)parent; + } + + /** + * Returns the frame manager managing this frame. + */ + public FrameManager getFrameManager () + { + return _fmgr; + } + + @Override // from Component + public void paint (Graphics g) + { + // we catch paint requests and forward them on to the repaint + // infrastructure + update(g); + } + + @Override // from Component + public void update (Graphics g) + { + Shape clip = g.getClip(); + Rectangle dirty; + if (clip != null) { + dirty = clip.getBounds(); + } else { + dirty = getRootPane().getBounds(); + // account for our frame insets + Insets insets = getInsets(); + dirty.x += insets.left; + dirty.y += insets.top; + } + + if (_fmgr != null) { + _fmgr.restoreFromBack(dirty); + } else { + Log.info("Dropping AWT dirty rect " + dirty + " (" + clip + ")."); + } + } + + protected FrameManager _fmgr; +} diff --git a/src/java/com/threerings/media/ManagedJFrame.java b/src/java/com/threerings/media/ManagedJFrame.java index fdc1182e..b8834980 100644 --- a/src/java/com/threerings/media/ManagedJFrame.java +++ b/src/java/com/threerings/media/ManagedJFrame.java @@ -26,10 +26,12 @@ import java.awt.GraphicsConfiguration; import java.awt.Insets; import java.awt.Rectangle; import java.awt.Shape; +import java.awt.Window; import javax.swing.JFrame; import com.samskivert.util.StringUtil; + import com.threerings.media.Log; /** @@ -37,6 +39,7 @@ import com.threerings.media.Log; * class. */ public class ManagedJFrame extends JFrame + implements FrameManager.ManagedRoot { /** * Constructs a managed frame with no title. @@ -62,14 +65,18 @@ public class ManagedJFrame extends JFrame super(gc); } - /** - * Called by our frame manager when it's ready to go. - */ + // from interface FrameManager.ManagedRoot public void init (FrameManager fmgr) { _fmgr = fmgr; } + // from interface FrameManager.ManagedRoot + public Window getWindow () + { + return this; + } + /** * Returns the frame manager managing this frame. */ @@ -107,9 +114,6 @@ public class ManagedJFrame extends JFrame if (_fmgr != null) { _fmgr.restoreFromBack(dirty); - } else { - Log.info("Dropping AWT dirty rect " + StringUtil.toString(dirty) + - " [clip=" + StringUtil.toString(clip) + "]."); } } diff --git a/tests/src/java/com/threerings/media/util/PathViz.java b/tests/src/java/com/threerings/media/util/PathViz.java index 9575c991..9d4f08da 100644 --- a/tests/src/java/com/threerings/media/util/PathViz.java +++ b/tests/src/java/com/threerings/media/util/PathViz.java @@ -74,7 +74,7 @@ public class PathViz extends MediaPanel public static void main (String[] args) { ManagedJFrame frame = new ManagedJFrame("Path viz"); - FrameManager fmgr = FrameManager.newInstance(frame, frame); + FrameManager fmgr = FrameManager.newInstance(frame); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new PathViz(fmgr)); diff --git a/tests/src/java/com/threerings/miso/client/ScrollingFrame.java b/tests/src/java/com/threerings/miso/client/ScrollingFrame.java index 38b6c720..1e1203b6 100644 --- a/tests/src/java/com/threerings/miso/client/ScrollingFrame.java +++ b/tests/src/java/com/threerings/miso/client/ScrollingFrame.java @@ -26,17 +26,17 @@ import java.awt.Component; import java.awt.GraphicsConfiguration; import javax.swing.JButton; -import javax.swing.JFrame; import javax.swing.JPanel; import com.samskivert.swing.VGroupLayout; +import com.threerings.media.ManagedJFrame; import com.threerings.media.SafeScrollPane; /** * The main application window. */ -public class ScrollingFrame extends JFrame +public class ScrollingFrame extends ManagedJFrame { /** * Creates a frame in which the scrolling test app can operate. diff --git a/tests/src/java/com/threerings/miso/client/ScrollingTestApp.java b/tests/src/java/com/threerings/miso/client/ScrollingTestApp.java index 091f2b12..e91c5067 100644 --- a/tests/src/java/com/threerings/miso/client/ScrollingTestApp.java +++ b/tests/src/java/com/threerings/miso/client/ScrollingTestApp.java @@ -88,7 +88,7 @@ public class ScrollingTestApp _frame = new ScrollingFrame(gc); // set up our frame manager - _framemgr = FrameManager.newInstance(_frame, _frame); + _framemgr = FrameManager.newInstance(_frame); // we don't need to configure anything ResourceManager rmgr = new ResourceManager("rsrc"); diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerApp.java b/tests/src/java/com/threerings/miso/viewer/ViewerApp.java index c488fcf9..1f3ad41f 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerApp.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerApp.java @@ -81,7 +81,7 @@ public class ViewerApp // create the window _frame = new ViewerFrame(gc); - _framemgr = FrameManager.newInstance(_frame, _frame); + _framemgr = FrameManager.newInstance(_frame); // we don't need to configure anything ResourceManager rmgr = new ResourceManager("rsrc"); diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java index 5728f18d..cef6d73a 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java @@ -33,10 +33,12 @@ import javax.swing.JMenuBar; import com.samskivert.swing.util.MenuUtil; +import com.threerings.media.ManagedJFrame; + /** * The viewer frame is the main application window. */ -public class ViewerFrame extends JFrame +public class ViewerFrame extends ManagedJFrame { /** * Creates a frame in which the viewer application can operate.