001 package com.croftsoft.apps.fraction; 002 003 import java.applet.*; 004 import java.awt.*; 005 import java.awt.event.*; 006 import java.awt.geom.*; 007 import java.util.Random; 008 import javax.swing.*; 009 import javax.swing.event.*; 010 011 import com.croftsoft.core.lang.NullArgumentException; 012 013 /********************************************************************* 014 * The Fraction hero sprite. 015 * 016 * @version 017 * 2003-02-12 018 * @since 019 * 2002-04-28 020 * @author 021 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 022 *********************************************************************/ 023 024 public class FractionHeroSprite 025 implements MouseInputListener 026 ////////////////////////////////////////////////////////////////////// 027 ////////////////////////////////////////////////////////////////////// 028 { 029 030 private int x; 031 032 private int y; 033 034 private int diameter; 035 036 private int radius; 037 038 private int verticalMovementX; 039 040 private int horizontalMovementY; 041 042 private Point mousePoint; 043 044 private Color [ ] colors; 045 046 private int colorOffset; 047 048 ////////////////////////////////////////////////////////////////////// 049 // constructor methods 050 ////////////////////////////////////////////////////////////////////// 051 052 /********************************************************************* 053 * Main constructor. 054 *********************************************************************/ 055 public FractionHeroSprite ( ) 056 ////////////////////////////////////////////////////////////////////// 057 { 058 } 059 060 ////////////////////////////////////////////////////////////////////// 061 // accessor methods 062 ////////////////////////////////////////////////////////////////////// 063 064 public int getX ( ) { return x; } 065 066 public int getY ( ) { return y; } 067 068 public int getHorizontalMovementY ( ) { return horizontalMovementY; } 069 070 public int getVerticalMovementX ( ) { return verticalMovementX; } 071 072 ////////////////////////////////////////////////////////////////////// 073 // mutator methods 074 ////////////////////////////////////////////////////////////////////// 075 076 public void setDiameter ( int diameter ) 077 ////////////////////////////////////////////////////////////////////// 078 { 079 this.diameter = diameter; 080 081 this.radius = diameter / 2; 082 083 colors = new Color [ diameter ]; 084 085 for ( int i = 0; i < colors.length; i++ ) 086 { 087 colors [ i ] = new Color ( 088 ( 256 / diameter ) * ( i + 1 ) - 1, 089 ( ( 256 / diameter ) * ( i + 1 ) - 1 ) / 4, 090 ( ( 256 / diameter ) * ( i + 1 ) - 1 ) / 2 ); 091 } 092 } 093 094 public void setX ( int x ) { this.x = x; } 095 096 public void setY ( int y ) { this.y = y; } 097 098 public void setHorizontalMovementY ( int horizontalMovementY ) 099 ////////////////////////////////////////////////////////////////////// 100 { 101 this.horizontalMovementY = horizontalMovementY; 102 } 103 104 public void setVerticalMovementX ( int verticalMovementX ) 105 ////////////////////////////////////////////////////////////////////// 106 { 107 this.verticalMovementX = verticalMovementX; 108 } 109 110 ////////////////////////////////////////////////////////////////////// 111 // interface ComponentAnimator methods 112 ////////////////////////////////////////////////////////////////////// 113 114 public void update ( JComponent component ) 115 ////////////////////////////////////////////////////////////////////// 116 { 117 if ( mousePoint == null ) 118 { 119 return; 120 } 121 122 int newX = x; 123 124 int newY = y; 125 126 int deltaX = mousePoint.x - x; 127 128 int deltaY = mousePoint.y - y + radius; 129 130 if ( deltaX > 0 ) 131 { 132 newX++; 133 } 134 else if ( deltaX < 0 ) 135 { 136 newX--; 137 } 138 139 if ( deltaY > 0 ) 140 { 141 newY++; 142 } 143 else if ( deltaY < 0 ) 144 { 145 newY--; 146 } 147 148 if ( Math.abs ( deltaX ) > Math.abs ( deltaY ) ) 149 { 150 if ( y == horizontalMovementY ) 151 { 152 x = newX; 153 } 154 155 if ( x == verticalMovementX ) 156 { 157 y = newY; 158 } 159 } 160 else 161 { 162 if ( x == verticalMovementX ) 163 { 164 y = newY; 165 } 166 167 if ( y == horizontalMovementY ) 168 { 169 x = newX; 170 } 171 } 172 } 173 174 ////////////////////////////////////////////////////////////////////// 175 ////////////////////////////////////////////////////////////////////// 176 177 public void paint ( 178 JComponent component, 179 Graphics2D graphics ) 180 ////////////////////////////////////////////////////////////////////// 181 { 182 colorOffset = ( colorOffset + 1 ) % colors.length; 183 184 for ( int i = diameter; i > 0; i-- ) 185 { 186 graphics.setColor ( 187 colors [ ( i - 1 + colorOffset ) % colors.length ] ); 188 189 graphics.fillOval ( x - i / 2, y - diameter, i, diameter ); 190 } 191 } 192 193 public void getPaintBounds ( Rectangle paintBounds ) 194 ////////////////////////////////////////////////////////////////////// 195 { 196 paintBounds.x = x - diameter / 2 - 1; 197 198 paintBounds.y = y - diameter - 1; 199 200 paintBounds.width = diameter + 2; 201 202 paintBounds.height = diameter + 2; 203 } 204 205 ////////////////////////////////////////////////////////////////////// 206 ////////////////////////////////////////////////////////////////////// 207 208 public void mouseClicked ( MouseEvent mouseEvent ) 209 ////////////////////////////////////////////////////////////////////// 210 { 211 } 212 213 public void mouseDragged ( MouseEvent mouseEvent ) 214 ////////////////////////////////////////////////////////////////////// 215 { 216 mousePoint = mouseEvent.getPoint ( ); 217 } 218 219 public void mouseEntered ( MouseEvent mouseEvent ) 220 ////////////////////////////////////////////////////////////////////// 221 { 222 } 223 224 public void mouseExited ( MouseEvent mouseEvent ) 225 ////////////////////////////////////////////////////////////////////// 226 { 227 } 228 229 public void mouseMoved ( MouseEvent mouseEvent ) 230 ////////////////////////////////////////////////////////////////////// 231 { 232 mousePoint = mouseEvent.getPoint ( ); 233 } 234 235 public void mousePressed ( MouseEvent mouseEvent ) 236 ////////////////////////////////////////////////////////////////////// 237 { 238 } 239 240 public void mouseReleased ( MouseEvent mouseEvent ) 241 ////////////////////////////////////////////////////////////////////// 242 { 243 } 244 245 ////////////////////////////////////////////////////////////////////// 246 ////////////////////////////////////////////////////////////////////// 247 }