summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/SkyscraperApplet.java
blob: ad398229ebdfa0b26e0d97c3284905fc4693dfc8 (plain)
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
/* SkyscraperApplet.java */

import java.awt.*;
import java.util.*;
import java.applet.*;

class Skyscraper
{
  public int x;
  public int y;
  public int width;
  public int height;
  int        wndcntx;
  int        wndcnty;
  boolean    blinkon = false;

  Skyscraper(int x, int y)
  {
    this.x = x;
    this.y = y;
    this.width = (int)(30*(0.5+Math.random()));
    this.height = (int)(100*(0.5+Math.random()));
    wndcntx = (width-4)/5;
    wndcnty = (height-4)/5;
  }

  void LightEvent(Graphics g)
  {
    double rnd  = Math.random();
    int    xwnd = (int)(Math.random()*wndcntx);
    int    ywnd = (int)(Math.random()*wndcnty);
    if (blinkon) {
      g.setColor(Color.black);
      g.fillRect(x+width/2,y-height-20,2,2);
      blinkon = false;
    }
    if (rnd >= 0.9) {
      blinkon = true;
      g.setColor(Color.red);
      g.fillRect(x+width/2,y-height-20,2,2);
    } else if (rnd >= 0.7) {
      g.setColor(Color.black);
      g.fillRect(x+2+xwnd*5,y-height+2+ywnd*5,2,2);
    } else {
      g.setColor(Color.yellow);
      g.fillRect(x+2+xwnd*5,y-height+2+ywnd*5,2,2);
    }
  }
}

public class SkyscraperApplet
extends Applet
implements Runnable
{
  //Membervariablen
  Thread th;
  Vector v = new Vector();
  AudioClip thunder;
  boolean running;

   //Parameter
  int    DELAY;
  float  FLASH;
  String THUNDER;

  public void init()
  {
    Skyscraper house;
    int x = 5;

    //H�user erzeugen
    while (this.getSize().width-x-1 >= 30) {
      house = new Skyscraper(x,this.getSize().height-10);
      v.addElement(house);
      x += house.width + 5;
    }
    setBackground(Color.black);

    //Parameter einlesen
    try {
      DELAY = Integer.parseInt(getParameter("delay"));
    } catch (NumberFormatException e) {
      DELAY = 75;
    }
    try {
      FLASH = (new Float(getParameter("flash"))).floatValue();
    } catch (NumberFormatException e) {
      FLASH = 0.01F;
    }
    THUNDER = getParameter("thunder");
    if (THUNDER != null) {
      thunder = getAudioClip(getCodeBase(),THUNDER);
    }
    System.out.println("DELAY = "+DELAY);
    System.out.println("FLASH = "+FLASH);
    System.out.println("THUNDER = "+THUNDER);
  }

  public void start()
  {
    if (th == null) {
      running = true;
      th = new Thread(this);
      th.start();
    }
  }

  public void stop()
  {
    if (th != null) {
      running = false;
      th = null;
    }
  }

  public void run()
  {
    while (running) {
      repaint();
      try {
        Thread.sleep(DELAY);
      } catch (InterruptedException e) {
        //nothing
      }
    }
  }

  public void update(Graphics g)
  {
    paint(g);
  }

  public void paint(Graphics g)
  {
    int i;
    Skyscraper house;

    i = (int)Math.floor(Math.random()*v.size());
    house = (Skyscraper)v.elementAt(i);
    house.LightEvent(g);
    if (Math.random() < FLASH) {
      Lightning(g,house.x+10,house.y-house.height);
    }
  }

  public void Lightning(Graphics g, int x, int y)
  {
    Vector poly = new Vector();
    int dx, dy, i, polysize;

    thunder.play();
    //Blitzpolygon berechnen
    poly.addElement(new Point(x,y));
    polysize = 1;
    while (y > 10) {
      dx = 10 - (int)(Math.floor(Math.random()*20));
      dy = - (int)(Math.floor(Math.random()*20));
      x += dx;
      y += dy;
      poly.addElement(new Point(x,y));
      ++polysize;
    }
    //Blitzvector in Koordinaten-Arrays umwandeln
    int[] xpoints = new int[poly.size()];
    int[] ypoints = new int[poly.size()];
    for (i = 0; i < polysize; ++i) {
      Point p = (Point)poly.elementAt(i);
      xpoints[i] = p.x;
      ypoints[i] = p.y;
    }
    //Blitz zeichnen
    for (i = 0; i <= 1; ++i) {
      g.setColor(Color.white);
      g.drawPolyline(xpoints, ypoints, polysize);
      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {}
      g.setColor(Color.black);
      g.drawPolyline(xpoints, ypoints, polysize);
      try {
        Thread.sleep(20);
      } catch (InterruptedException e) {}
    }
  }
}