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 import com.croftsoft.core.animation.*; 013 import com.croftsoft.core.animation.sprite.*; 014 015 /********************************************************************* 016 * A Fraction door. 017 * 018 * @version 019 * 2002-07-21 020 * @since 021 * 2002-04-28 022 * @author 023 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 024 *********************************************************************/ 025 026 public class FractionDoor 027 implements ComponentAnimator 028 ////////////////////////////////////////////////////////////////////// 029 ////////////////////////////////////////////////////////////////////// 030 { 031 032 private static final Color DOOR_FRAME_COLOR 033 = Color.ORANGE; 034 035 private static final Color NUMBERS_COLOR 036 = Color.BLACK; 037 038 private static final Color DOOR_COLOR_CLOSED 039 = Color.LIGHT_GRAY; 040 041 private static final Color DOOR_COLOR_HIGHLIGHTED 042 = Color.YELLOW; 043 044 private static final Color DOOR_COLOR_OPEN 045 = Color.WHITE; 046 047 // 048 049 private final Rectangle rectangle; 050 051 private final int numerator; 052 053 private final int denominator; 054 055 private final String numeratorStr; 056 057 private final String denominatorStr; 058 059 private final int numeratorX; 060 061 private final int numeratorY; 062 063 private final int denominatorX; 064 065 private final int denominatorY; 066 067 private final int lineX1; 068 069 private final int lineY1; 070 071 private final int lineX2; 072 073 private final int lineY2; 074 075 private final int horizontalMovementY; 076 077 private final int verticalMovementX; 078 079 // 080 081 private Color doorColor; 082 083 ////////////////////////////////////////////////////////////////////// 084 // constructor methods 085 ////////////////////////////////////////////////////////////////////// 086 087 public FractionDoor ( 088 Rectangle rectangle, 089 int numerator, 090 int denominator, 091 int numeratorX, 092 int numeratorY, 093 int denominatorX, 094 int denominatorY, 095 int lineX1, 096 int lineY1, 097 int lineX2, 098 int lineY2, 099 int horizontalMovementY, 100 int verticalMovementX ) 101 ////////////////////////////////////////////////////////////////////// 102 { 103 NullArgumentException.check ( this.rectangle = rectangle ); 104 105 this.numerator = numerator; 106 107 this.denominator = denominator; 108 109 this.numeratorStr = Integer.toString ( numerator ); 110 111 this.denominatorStr = Integer.toString ( denominator ); 112 113 this.numeratorX = numeratorX; 114 115 this.numeratorY = numeratorY; 116 117 this.denominatorX = denominatorX; 118 119 this.denominatorY = denominatorY; 120 121 this.lineX1 = lineX1; 122 123 this.lineY1 = lineY1; 124 125 this.lineX2 = lineX2; 126 127 this.lineY2 = lineY2; 128 129 this.horizontalMovementY = horizontalMovementY; 130 131 this.verticalMovementX = verticalMovementX; 132 133 reset ( ); 134 } 135 136 ////////////////////////////////////////////////////////////////////// 137 // accessor methods 138 ////////////////////////////////////////////////////////////////////// 139 140 public int getNumerator ( ) { return numerator; } 141 142 public int getDenominator ( ) { return denominator; } 143 144 ////////////////////////////////////////////////////////////////////// 145 // mutator methods 146 ////////////////////////////////////////////////////////////////////// 147 148 public void setHighlighted ( ) 149 ////////////////////////////////////////////////////////////////////// 150 { 151 doorColor = DOOR_COLOR_HIGHLIGHTED; 152 } 153 154 public void setOpen ( ) 155 ////////////////////////////////////////////////////////////////////// 156 { 157 doorColor = DOOR_COLOR_OPEN; 158 } 159 160 public void reset ( ) 161 ////////////////////////////////////////////////////////////////////// 162 { 163 doorColor = DOOR_COLOR_CLOSED; 164 } 165 166 ////////////////////////////////////////////////////////////////////// 167 ////////////////////////////////////////////////////////////////////// 168 169 public boolean contains ( 170 int x, 171 int y ) 172 ////////////////////////////////////////////////////////////////////// 173 { 174 return ( x == verticalMovementX ) 175 && ( y == horizontalMovementY ); 176 } 177 178 public int getHorizontalMovementY ( ) 179 ////////////////////////////////////////////////////////////////////// 180 { 181 return horizontalMovementY; 182 } 183 184 public int getVerticalMovementX ( ) 185 ////////////////////////////////////////////////////////////////////// 186 { 187 return verticalMovementX; 188 } 189 190 ////////////////////////////////////////////////////////////////////// 191 // interface ComponentAnimator methods 192 ////////////////////////////////////////////////////////////////////// 193 194 public void update ( JComponent component ) 195 ////////////////////////////////////////////////////////////////////// 196 { 197 } 198 199 public void paint ( 200 JComponent component, 201 Graphics2D graphics2D ) 202 ////////////////////////////////////////////////////////////////////// 203 { 204 graphics2D.setColor ( doorColor ); 205 206 graphics2D.fill ( rectangle ); 207 208 graphics2D.setColor ( DOOR_FRAME_COLOR ); 209 210 graphics2D.draw ( rectangle ); 211 212 graphics2D.setColor ( NUMBERS_COLOR ); 213 214 graphics2D.drawString ( 215 numeratorStr, 216 numeratorX, 217 numeratorY ); 218 219 graphics2D.drawLine ( 220 lineX1, 221 lineY1, 222 lineX2, 223 lineY2 ); 224 225 graphics2D.drawString ( 226 denominatorStr, 227 denominatorX, 228 denominatorY ); 229 } 230 231 ////////////////////////////////////////////////////////////////////// 232 ////////////////////////////////////////////////////////////////////// 233 }