summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java')
-rw-r--r--Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java99
1 files changed, 99 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java
new file mode 100644
index 0000000..6b6bfac
--- /dev/null
+++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file