001         package com.croftsoft.apps.ajgp.input;
002    
003         import java.awt.Color;
004         import java.awt.Graphics2D;
005         import java.awt.Point;
006         import java.awt.event.KeyAdapter;
007         import java.awt.event.KeyEvent;
008         import java.awt.event.MouseAdapter;
009         import java.awt.event.MouseEvent;
010         import java.awt.event.MouseMotionAdapter;
011         import javax.swing.JComponent;
012    
013         import com.croftsoft.core.animation.ComponentAnimator;
014    
015         /*********************************************************************
016         * User input ComponentAnimator implementation example.
017         *
018         * @version
019         *   $Date: 2008/04/19 21:30:58 $
020         * @since
021         *   2003-09-29
022         * @author
023         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
024         *********************************************************************/
025    
026         public final class  InputAnimator
027           implements ComponentAnimator
028         //////////////////////////////////////////////////////////////////////
029         //////////////////////////////////////////////////////////////////////
030         {
031    
032         private static final Color   BACKGROUND_COLOR = Color.BLACK;
033    
034         private static final Color   FOREGROUND_COLOR = Color.GREEN;
035    
036         private static final String  TEXT = "CroftSoft Input Example";
037    
038         //
039    
040         private KeyEvent  keyEvent;
041    
042         private Point     mousePoint;
043    
044         private boolean   mousePressed;
045    
046         private int       x;
047    
048         private int       y;
049    
050         //////////////////////////////////////////////////////////////////////
051         //////////////////////////////////////////////////////////////////////
052    
053         public  InputAnimator ( JComponent  component )
054         //////////////////////////////////////////////////////////////////////
055         {
056           component.addMouseListener (
057             new MouseAdapter ( )
058             {
059               public void  mousePressed ( MouseEvent  mouseEvent )
060               {
061                 mousePressed = true;
062               }
063             } );
064    
065           component.addMouseMotionListener (
066             new MouseMotionAdapter ( )
067             {
068               public void  mouseMoved ( MouseEvent  mouseEvent )
069               {
070                 mousePoint = mouseEvent.getPoint ( );
071               }
072             } );
073    
074           component.addKeyListener (
075             new KeyAdapter ( )
076             {
077               public void  keyPressed  ( KeyEvent  keyEvent )
078               {
079                 InputAnimator.this.keyEvent = keyEvent;
080               }
081             } );
082    
083           component.requestFocus ( );
084         }
085    
086         //////////////////////////////////////////////////////////////////////
087         //////////////////////////////////////////////////////////////////////
088    
089         public void  update ( JComponent  component )
090         //////////////////////////////////////////////////////////////////////
091         {
092           boolean  repaintRequired = false;
093    
094           if ( mousePoint != null )
095           {
096             repaintRequired = true;
097    
098             x = mousePoint.x;
099    
100             y = mousePoint.y;
101    
102             mousePoint = null;
103           }
104    
105           if ( keyEvent != null )
106           {
107             int  keyCode = keyEvent.getKeyCode ( );
108    
109             switch ( keyCode )
110             {
111               case KeyEvent.VK_DOWN:
112               case KeyEvent.VK_KP_DOWN:
113    
114                 repaintRequired = true;
115    
116                 y++;
117    
118                 break;
119    
120               case KeyEvent.VK_UP:
121               case KeyEvent.VK_KP_UP:
122    
123                 repaintRequired = true;
124    
125                 y--;
126    
127                 break;
128    
129               case KeyEvent.VK_LEFT:
130               case KeyEvent.VK_KP_LEFT:
131    
132                 repaintRequired = true;
133    
134                 x--;
135    
136                 break;
137    
138               case KeyEvent.VK_RIGHT:
139               case KeyEvent.VK_KP_RIGHT:
140    
141                 repaintRequired = true;
142    
143                 x++;
144    
145                 break;
146             }
147    
148             keyEvent = null;
149           }
150    
151           if ( repaintRequired )
152           {
153             component.repaint ( );
154           }
155         }
156    
157         public void  paint (
158           JComponent  component,
159           Graphics2D  graphics )
160         //////////////////////////////////////////////////////////////////////
161         {
162           graphics.setColor ( BACKGROUND_COLOR );
163    
164           graphics.fillRect ( 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE );
165    
166           graphics.setColor ( FOREGROUND_COLOR );
167    
168           graphics.drawString ( TEXT, x, y );
169         }
170    
171         //////////////////////////////////////////////////////////////////////
172         //////////////////////////////////////////////////////////////////////
173         }