summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing3407.java
blob: 36095ff7fc8e1b6751b582b091fda82e9a5c4256 (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
/* Listing3407.java */

import java.awt.*;
import java.awt.event.*;

public class Listing3407
extends Frame
implements Runnable
{
  int cnt = 0;

  public static void main(String[] args)
  {
    Listing3407 wnd = new Listing3407();
    wnd.setSize(250,150);
    wnd.setVisible(true);
    wnd.startAnimation();
  }

  public Listing3407()
  {
    super("Animations-Threads");
    setBackground(Color.lightGray);
    addWindowListener(new WindowClosingAdapter(true));
  }

  public void startAnimation()
  {
    Thread th = new Thread(this);
    th.start();
  }

  public void run()
  {
    while (true) {
      repaint();
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        //nichts
      }
    }
  }

  public void paint(Graphics g)
  {
    ++cnt;
    g.drawString("Counter = "+cnt,10,50);
  }
}