package com.croftsoft.apps.exemplar; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import com.croftsoft.core.animation.ComponentAnimator; import com.croftsoft.core.animation.animator.NullComponentAnimator; import com.croftsoft.core.gui.WindowLib; import com.croftsoft.core.lang.NullArgumentException; import com.croftsoft.core.lang.lifecycle.Lifecycle; import com.croftsoft.core.lang.lifecycle.Updatable; /********************************************************************* * Exemplar view. * * @version * $Id: View.java,v 1.14 2006/06/22 13:46:22 croft Exp $ * @since * 2006-01-03 * @author * David Wallace Croft *********************************************************************/ public final class View implements Lifecycle, Updatable ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// { private final Config config; private final Accessor accessor; private final Queue eventQueue; private final JFrame jFrame; private final JComponent jComponent; // private ComponentAnimator componentAnimator; ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// public View ( final Config config, final Accessor accessor, final Queue eventQueue, final JFrame jFrame ) ////////////////////////////////////////////////////////////////////// { NullArgumentException.checkArgs ( this.config = config, this.accessor = accessor, this.eventQueue = eventQueue, this.jFrame = jFrame ); jFrame.setTitle ( config.getFrameTitle ( ) ); WindowLib.centerOnScreen ( jFrame, config.getFrameSize ( ) ); componentAnimator = NullComponentAnimator.INSTANCE; jComponent = new JComponent ( ) { private static final long serialVersionUID = 0L; @Override public void paintComponent ( final Graphics graphics ) { componentAnimator.paint ( this, ( Graphics2D ) graphics ); } }; final Container contentPane = jFrame.getContentPane ( ); contentPane.setLayout ( new BorderLayout ( ) ); contentPane.add ( jComponent, BorderLayout.CENTER ); } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// public void addMouseListener ( final MouseListener mouseListener ) ////////////////////////////////////////////////////////////////////// { jComponent.addMouseListener ( mouseListener ); } ////////////////////////////////////////////////////////////////////// // lifecycle methods ////////////////////////////////////////////////////////////////////// public void init ( ) ////////////////////////////////////////////////////////////////////// { System.out.println ( "View.init()" ); componentAnimator = new Animator ( config, accessor, jComponent ); } public void start ( ) ////////////////////////////////////////////////////////////////////// { System.out.println ( "View.start()" ); } public void stop ( ) ////////////////////////////////////////////////////////////////////// { System.out.println ( "View.stop()" ); } public void destroy ( ) ////////////////////////////////////////////////////////////////////// { System.out.println ( "View.destroy()" ); jFrame.setVisible ( false ); jFrame.dispose ( ); } public void update ( ) ////////////////////////////////////////////////////////////////////// { Message message = null; while ( ( message = eventQueue.poll ( ) ) != null ) { final Message.Type type = message.getType ( ); switch ( type ) { case CLICK_COUNT_CHANGED: // Could play a sound here. System.out.println ( "Click!" ); break; default: System.out.println ( getClass ( ).getName ( ) + ": " + "unknown message type: " + type ); } } componentAnimator.update ( jComponent ); } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// }