From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/Listing3415.java | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing3415.java') 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 -- cgit v1.2.3