1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
| import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
import java.util.*;
public class PatternSimulator extends Applet {
SimCanvas canvas;
public Label time_label;
public Label number_label;
Button pause_button;
public void init() {
canvas = new SimCanvas();
canvas.init(this);
setLayout(new BorderLayout());
Panel p = new Panel();
p.setFont(new Font("TimesRoman", Font.BOLD, 12));
p.add(new Label("Simulation Time:", Label.RIGHT));
p.add(time_label = new Label("1000", Label.LEFT));
p.add(new Label("Airplanes in system:", Label.RIGHT));
p.add(number_label = new Label("0", Label.LEFT));
p.add(new Label(" "));
p.add(pause_button = new Button("Start Sim"));
pause_button.disable();
add("North", p);
add("Center", canvas);
}
public void start() {
if (canvas.sim_thread == null) {
canvas.sim_thread = new Thread(canvas);
canvas.sim_thread.start();
}
}
public void stop() {
canvas.sim_thread = null;
}
public boolean action(Event evt, Object arg) {
if ("Start Sim".equals(arg)) {
pause_button.setLabel("Pause");
canvas.startSim();
return true;
}
else if("Pause".equals(arg)) {
canvas.paused = true;
pause_button.setLabel("Resume");
return true;
}
else if("Resume".equals(arg)) {
canvas.paused = false;
pause_button.setLabel("Pause");
}
return false;
}
}
class SimCanvas extends Canvas implements Runnable {
Applet app;
public Thread sim_thread = null;
Image background;
Image hangar;
Image double_buffer;
Graphics double_buffer_graphics;
int mouse_down_at_x;
int mouse_down_at_y;
public Vector airplanes;
int simulation_units;
final static double new_plane_frequency = 0.10;
public boolean paused = true;
public void init(Applet app) {
resize(450, 450);
setBackground(new Color(10, 70, 30));
this.app = app;
background = app.getImage(app.getDocumentBase(), "backgrnd.gif");
hangar = app.getImage(app.getDocumentBase(), "hangar.gif");
CommercialAirplane.MakeImages(app.getImage(app.getDocumentBase(), "777.gif"), app);
PropellerAirplane.MakeImages(app.getImage(app.getDocumentBase(), "propeller.gif"), app);
double_buffer = app.createImage(450, 450);
double_buffer_graphics = double_buffer.getGraphics();
double_buffer_graphics.setColor(new Color(10, 70, 30));
double_buffer_graphics.fillRect(0,0,450,450);
double_buffer_graphics.setColor(Color.white);
double_buffer_graphics.drawString("Loading Pattern Simulator...", 10, 425);
airplanes = new Vector(20);
}
public void startSim() {
airplanes.removeAllElements();
airplanes.addElement(new CommercialAirplane());
repaint();
simulation_units = 1000;
paused = false;
}
public void paint(Graphics g) {
double_buffer_graphics.drawImage(background, 0, 0, app);
for (Enumeration e = airplanes.elements() ; e.hasMoreElements() ;) {
Airplane plane = (Airplane)e.nextElement();
plane.Draw(double_buffer_graphics, app);
}
double_buffer_graphics.drawImage(hangar, 170, 292, app);
g.drawImage(double_buffer, 0, 0, app);
}
public void update(Graphics g) {
paint(g);
}
public void run() {
//# Get the first image to the screen ASAP
while((app.checkImage(background, app) & ImageObserver.ALLBITS) == 0) {
repaint();
try {
Thread.sleep(250);
} catch (InterruptedException e) {}
}
((PatternSimulator)getParent()).pause_button.enable();
while (true) {
if(paused) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {}
}
else {
if(--simulation_units <= 0) {
((PatternSimulator)getParent()).pause_button.setLabel("Start Sim");
paused = true;
}
((PatternSimulator)getParent()).time_label.setText(Integer.toString(simulation_units));
((PatternSimulator)getParent()).number_label.setText(Integer.toString(airplanes.size()));
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
DoUpdate();
try {
paint(getGraphics());
}
catch (Exception e){
sim_thread = null;
}
}
}
}
protected void DoUpdate() {
boolean CanAddAirplane = true;
boolean CanLandAirplane = true;
for (Enumeration e = airplanes.elements() ; e.hasMoreElements() ;) {
Airplane plane = (Airplane)e.nextElement();
if(plane.InTheWayOfNewAirplane()) {
CanAddAirplane = false;
}
if(plane.CurrentlyLanding()) {
CanLandAirplane = false;
}
}
for (Enumeration e = airplanes.elements() ; e.hasMoreElements() ;) {
Airplane plane = (Airplane)e.nextElement();
if(CanLandAirplane) {
if(plane.DoLandIfPossible()) {
CanLandAirplane = false;
}
}
if(mouse_down_at_x >= 0) {
plane.Release(mouse_down_at_x, mouse_down_at_y);
}
plane.Fly();
if(plane.RemoveMe()) {
airplanes.removeElement(plane);
}
}
if(CanAddAirplane) {
double random_number = Math.random();
if(random_number < new_plane_frequency / 2) {
airplanes.addElement(new PropellerAirplane());
}
else if(random_number < new_plane_frequency) {
airplanes.addElement(new CommercialAirplane());
}
}
}
public boolean mouseDown(java.awt.Event evt, int x, int y) {
mouse_down_at_x = x;
mouse_down_at_y = y;
return true;
}
public boolean mouseUp(java.awt.Event evt, int x, int y) {
mouse_down_at_x = -1;
mouse_down_at_y = -1;
return true;
}
public boolean mouseDrag(java.awt.Event evt, int x, int y) {
mouse_down_at_x = x;
mouse_down_at_y = y;
return true;
}
}
abstract class Airplane extends Object {
public static final int NORTH = 0;
public static final int NORTHEAST = 1;
public static final int EAST = 2;
public static final int SOUTHEAST = 3;
public static final int SOUTH = 4;
public static final int SOUTHWEST = 5;
public static final int WEST = 6;
public static final int NORTHWEST = 7;
public static final int SPEED = 18;
public static final int DIAGNAL_SPEED = 10;
public static final int GROUND_SPEED = 5;
public static final int DIAGNAL_GROUND_SPEED = 3;
public static final int NORMAL = 0;
public static final int LANDING = 1;
public static final int LEAVING = 2;
int x, y, w, h;
int direction;
int status;
int random_course;
Airplane() {
this.x = 410;
this.y = 0;
direction = SOUTH;
status = NORMAL;
random_course = (int) (Math.random() * 20.0f);
}
public abstract void Draw(Graphics g, ImageObserver ob);
public boolean InTheWayOfNewAirplane() {
if(status == LANDING)
return false;
switch(direction) {
case EAST :
return (x > 265);
case SOUTHEAST :
return status != LANDING;
case SOUTH :
return (status != LANDING && y < 135);
default :
return false;
}
}
public boolean CurrentlyLanding() {
return (status == LANDING && direction <= SOUTH);
}
public boolean RemoveMe() {
if(status == LANDING && direction == NORTH)
return true;
if(status == LEAVING &&(x < -40 || y < -40 || x > 450 || y > 450))
return true;
return false;
}
public boolean DoLandIfPossible() {
if(status == NORMAL && direction == EAST && x >= 250 && x <= 275) {
status = LANDING;
direction = SOUTHEAST;
return true;
}
return false;
}
public void Fly() {
if(status == LANDING) {
switch(direction) {
case SOUTHEAST :
y += DIAGNAL_SPEED;
x += DIAGNAL_SPEED;
if(x >= 280) {
x = 282;
direction = SOUTH;
}
break;
case SOUTH :
int gradual_slow = y / 20;
y += SPEED - gradual_slow;
if(y >= 269) {
direction = SOUTHWEST;
}
break;
case SOUTHWEST :
x -= DIAGNAL_GROUND_SPEED;
y += DIAGNAL_GROUND_SPEED;
if(y >= 292) {
direction = WEST;
}
break;
case WEST :
x -= GROUND_SPEED;
if(x <= 220) {
direction = NORTH;
}
break;
}
}
else {
switch(direction) {
case NORTH :
y -= SPEED;
if(y + random_course < 65 && status != LEAVING) {
direction = NORTHEAST;
random_course = (int) (Math.random() * 20.0f);
}
break;
case NORTHEAST :
y -= DIAGNAL_SPEED;
x += DIAGNAL_SPEED;
if(y + random_course < 35 && status != LEAVING) {
direction = EAST;
}
break;
case EAST :
x += SPEED;
if(x + random_course > 370 && status != LEAVING) {
direction = SOUTHEAST;
random_course = (int) (Math.random() * 20.0f);
}
break;
case SOUTHEAST :
x += DIAGNAL_SPEED;
y += DIAGNAL_SPEED;
if(x + random_course > 400 && status != LEAVING) {
direction = SOUTH;
}
break;
case SOUTH :
y += SPEED;
if(y + random_course > 358 && status != LEAVING) {
direction = SOUTHWEST;
random_course = (int) (Math.random() * 20.0f);
}
break;
case SOUTHWEST :
x -= DIAGNAL_SPEED;
y += DIAGNAL_SPEED;
if(y + random_course > 388 && status != LEAVING) {
direction = WEST;
}
break;
case WEST :
x -= SPEED;
if(x + random_course < 62 && status != LEAVING) {
direction = NORTHWEST;
random_course = (int) (Math.random() * 20.0f);
}
break;
case NORTHWEST :
x -= DIAGNAL_SPEED;
y -= DIAGNAL_SPEED;
if(x + random_course < 32 && status != LEAVING) {
direction = NORTH;
}
break;
}
}
}
public void Release(int x_press, int y_press) {
if( status == NORMAL &&
x_press >= x && x_press <= x + w &&
y_press >= y && y_press <= y + h) {
status = LEAVING;
}
}
}
class CommercialAirplane extends Airplane {
public static Image airplane_image[] = new Image[8];
public static void MakeImages(Image base_image, Component component) {
for(int y = 0 ; y < 8 ; ++y) {
ImageFilter crop = new CropImageFilter(0, 38 * y, 36, 38);
airplane_image[y] = component.createImage(
new FilteredImageSource(base_image.getSource(), crop));
component.prepareImage(airplane_image[y], component);
}
}
public CommercialAirplane() {
super();
w = 36;
h = 38;
}
public void Draw(Graphics g, ImageObserver ob) {
g.drawImage(airplane_image[direction], x, y, ob);
}
}
class PropellerAirplane extends Airplane {
public static Image airplane_image[] = new Image[8];
public static void MakeImages(Image base_image, Component component) {
for(int y = 0 ; y < 8 ; ++y) {
ImageFilter crop = new CropImageFilter(0, 31 * y, 31, 31);
airplane_image[y] = component.createImage(
new FilteredImageSource(base_image.getSource(), crop));
component.prepareImage(airplane_image[y], component);
}
}
public PropellerAirplane() {
super();
w = 31;
h = 31;
}
public void Draw(Graphics g, ImageObserver ob) {
g.drawImage(airplane_image[direction], x, y, ob);
}
} |
Partager