001 package com.croftsoft.apps.ajgp.http; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.net.*; 006 import javax.swing.*; 007 008 import com.croftsoft.core.CroftSoftConstants; 009 import com.croftsoft.core.animation.AnimatedApplet; 010 import com.croftsoft.core.animation.AnimationInit; 011 import com.croftsoft.core.animation.animator.TextAnimator; 012 import com.croftsoft.core.animation.painter.ColorPainter; 013 import com.croftsoft.core.io.StringCoder; 014 import com.croftsoft.core.net.http.msg.HttpMessagePusher; 015 import com.croftsoft.core.util.queue.ListQueue; 016 import com.croftsoft.core.util.queue.Queue; 017 018 /********************************************************************* 019 * Applet that downloads high score from server. 020 * 021 * @version 022 * $Date: 2008/04/19 21:30:59 $ 023 * @since 024 * 2003-06-01 025 * @author 026 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 027 *********************************************************************/ 028 029 public final class ScoreApplet 030 extends AnimatedApplet 031 ////////////////////////////////////////////////////////////////////// 032 ////////////////////////////////////////////////////////////////////// 033 { 034 035 // applet constants 036 037 private static final String VERSION 038 = "2003-06-04"; 039 040 private static final String TITLE 041 = "CroftSoft ScoreApplet"; 042 043 private static final String APPLET_INFO 044 = "\n" + TITLE 045 + "\n" + "Version " + VERSION 046 + "\n" + CroftSoftConstants.COPYRIGHT 047 + "\n" + CroftSoftConstants.HOME_PAGE 048 + "\n" + CroftSoftConstants.DEFAULT_LICENSE 049 + "\n"; 050 051 private static final Font FONT 052 = new Font ( "Arioso", Font.BOLD, 30 ); 053 054 private static final Color BACKGROUND_COLOR = Color.BLACK; 055 056 private static final Color FOREGROUND_COLOR = Color.GREEN; 057 058 private static final int DELTA_X = 1; 059 060 private static final int DELTA_Y = 1; 061 062 // network constants 063 064 private static final String CHAR_SET_NAME = StringCoder.UTF_8; 065 066 private static final String CONTENT_TYPE = "text/plain"; 067 068 private static final String DEFAULT_HOST = "localhost"; 069 070 private static final String SERVLET_PATH = "servlet"; 071 072 private static final String DEFAULT_PATH = "/score/" + SERVLET_PATH; 073 074 private static final int DEFAULT_PORT = 8080; 075 076 private static final String REQUEST_GET = "get"; 077 078 private static final String REQUEST_SET = "set "; 079 080 private static final String USER_AGENT = "Score/1.0"; 081 082 // 083 084 private final TextAnimator textAnimator; 085 086 private final Queue outgoingQueue; 087 088 private final Queue incomingQueue; 089 090 // 091 092 private HttpMessagePusher httpMessagePusher; 093 094 private long score; 095 096 private long highScore; 097 098 private boolean gameOver; 099 100 ////////////////////////////////////////////////////////////////////// 101 ////////////////////////////////////////////////////////////////////// 102 103 public static void main ( String [ ] args ) 104 ////////////////////////////////////////////////////////////////////// 105 { 106 launch ( new ScoreApplet ( ) ); 107 } 108 109 public static AnimationInit createAnimationInit ( ) 110 ////////////////////////////////////////////////////////////////////// 111 { 112 AnimationInit animationInit = new AnimationInit ( ); 113 114 animationInit.setAppletInfo ( APPLET_INFO ); 115 116 animationInit.setBackgroundColor ( BACKGROUND_COLOR ); 117 118 animationInit.setFont ( FONT ); 119 120 animationInit.setForegroundColor ( FOREGROUND_COLOR ); 121 122 animationInit.setFrameTitle ( TITLE ); 123 124 animationInit.setShutdownConfirmationPrompt ( null ); 125 126 return animationInit; 127 } 128 129 ////////////////////////////////////////////////////////////////////// 130 ////////////////////////////////////////////////////////////////////// 131 132 public ScoreApplet ( ) 133 ////////////////////////////////////////////////////////////////////// 134 { 135 super ( createAnimationInit ( ) ); 136 137 textAnimator = new TextAnimator ( ); 138 139 textAnimator.setDeltaX ( DELTA_X ); 140 141 textAnimator.setDeltaY ( DELTA_Y ); 142 143 outgoingQueue = new ListQueue ( ); 144 145 incomingQueue = new ListQueue ( ); 146 } 147 148 ////////////////////////////////////////////////////////////////////// 149 ////////////////////////////////////////////////////////////////////// 150 151 public void init ( ) 152 ////////////////////////////////////////////////////////////////////// 153 { 154 super.init ( ); 155 156 addComponentPainter ( new ColorPainter ( ) ); 157 158 addComponentAnimator ( textAnimator ); 159 160 URL codeBaseURL = null; 161 162 try 163 { 164 codeBaseURL = getCodeBase ( ); 165 } 166 catch ( Exception ex ) 167 { 168 } 169 170 URL servletURL = null; 171 172 try 173 { 174 if ( codeBaseURL != null ) 175 { 176 servletURL = new URL ( codeBaseURL, SERVLET_PATH ); 177 } 178 else 179 { 180 servletURL = new URL ( 181 "http", DEFAULT_HOST, DEFAULT_PORT, DEFAULT_PATH ); 182 } 183 } 184 catch ( MalformedURLException ex ) 185 { 186 ex.printStackTrace ( ); 187 } 188 189 StringCoder stringCoder = new StringCoder ( CHAR_SET_NAME ); 190 191 httpMessagePusher = new HttpMessagePusher ( 192 outgoingQueue, 193 incomingQueue, 194 servletURL, 195 USER_AGENT, 196 CONTENT_TYPE, 197 stringCoder, 198 stringCoder ); 199 200 httpMessagePusher.init ( ); 201 202 outgoingQueue.append ( REQUEST_GET ); 203 204 animatedComponent.addMouseListener ( 205 new MouseAdapter ( ) 206 { 207 public void mousePressed ( MouseEvent mouseEvent ) 208 { 209 gameOver = true; 210 } 211 } ); 212 } 213 214 public void start ( ) 215 ////////////////////////////////////////////////////////////////////// 216 { 217 super.start ( ); 218 219 httpMessagePusher.start ( ); 220 } 221 222 public void stop ( ) 223 ////////////////////////////////////////////////////////////////////// 224 { 225 httpMessagePusher.stop ( ); 226 227 super.stop ( ); 228 } 229 230 public void destroy ( ) 231 ////////////////////////////////////////////////////////////////////// 232 { 233 httpMessagePusher.destroy ( ); 234 235 super.destroy ( ); 236 } 237 238 ////////////////////////////////////////////////////////////////////// 239 ////////////////////////////////////////////////////////////////////// 240 241 public void update ( JComponent component ) 242 ////////////////////////////////////////////////////////////////////// 243 { 244 super.update ( component ); 245 246 String highScoreString = ( String ) incomingQueue.poll ( ); 247 248 if ( highScoreString != null ) 249 { 250 try 251 { 252 highScore = Long.parseLong ( highScoreString ); 253 } 254 catch ( NumberFormatException ex ) 255 { 256 System.err.println ( 257 "Unexpected server response: " + highScoreString ); 258 } 259 } 260 261 if ( gameOver ) 262 { 263 gameOver = false; 264 265 if ( score > highScore ) 266 { 267 highScore = score; 268 269 outgoingQueue.append ( REQUEST_SET + highScore ); 270 } 271 272 score = 0; 273 } 274 else 275 { 276 score++; 277 } 278 279 textAnimator.setText ( 280 "Score: " + score + " High Score: " + highScore ); 281 } 282 283 ////////////////////////////////////////////////////////////////////// 284 ////////////////////////////////////////////////////////////////////// 285 }