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

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

public class Listing3415
extends Frame
implements Runnable
{
  private Thread th;
  private int actx;
  private int dx;
  private int actarc1;
  private int actarc2;
  private Image dbImage;
  private Graphics dbGraphics;

  public static void main(String[] args)
  {
    Listing3415 frame = new Listing3415();
    frame.setSize(210,170);
    frame.setVisible(true);
    frame.startAnimation();
  }

  public Listing3415()
  {
    super("Ameisenanimation");
    addWindowListener(new WindowClosingAdapter(true));
  }

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

  public void run()
  {
    actx = 0;
    dx = 1;
    actarc1 = 0;
    actarc2 = 0;
    while (true) {
      repaint();
      actx += dx;
      if (actx < 0 || actx > 100) {
        dx = -dx;
        actx += 2*dx;
      }
      actarc1 = (actarc1 + 1) % 360;
      actarc2 = (actarc2 + 2) % 360;
      try {
        Thread.sleep(40);
      } catch (InterruptedException e) {
        //nichts
      }
    }
  }

  public void update(Graphics g)
  {
    //Double-Buffer initialisieren
    if (dbImage == null) {
      dbImage = createImage(
        this.getSize().width,
        this.getSize().height
      );
      dbGraphics = dbImage.getGraphics();
    }
    //Hintergrund l�schen
    dbGraphics.setColor(getBackground());
    dbGraphics.fillRect(
      0,
      0,
      this.getSize().width,
      this.getSize().height
    );
    //Vordergrund zeichnen
    dbGraphics.setColor(getForeground());
    paint(dbGraphics);
    //Offscreen anzeigen
    g.drawImage(dbImage,0,0,this);
  }

  public void paint(Graphics g)
  {
    int xoffs = getInsets().left;
    int yoffs = getInsets().top;
    g.setColor(Color.lightGray);
    g.fillOval(xoffs+actx,yoffs+20,100,100);
    g.setColor(Color.red);
    g.drawArc(xoffs+actx,yoffs+20,100,100,actarc1,10);
    g.drawArc(xoffs+actx-1,yoffs+19,102,102,actarc1,10);
    g.setColor(Color.blue);
    g.drawArc(xoffs+actx,yoffs+20,100,100,360-actarc2,10);
    g.drawArc(xoffs+actx-1,yoffs+19,102,102,360-actarc2,10);
  }
}