mit sinus und cosinus implementiert

This commit is contained in:
ahren 2023-11-27 11:53:50 +01:00
parent 06781f51e0
commit 53c8ddabb9
3 changed files with 24 additions and 20 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build/

View File

@ -23,10 +23,10 @@ public class Start
Container c = frm.getContentPane(); Container c = frm.getContentPane();
c.setLayout(new OverlayLayout(c)); c.setLayout(new OverlayLayout(c));
for (int i = 0; i < 100; i++) for (int zeigerLaenge = 1; zeigerLaenge <=3; zeigerLaenge++)
{ {
long schlafzeit = (long)(1 + 100*Math.random()); long schlafzeit = (long)(100*zeigerLaenge);
Zeiger papier = new Zeiger(schlafzeit); Zeiger papier = new Zeiger(schlafzeit, zeigerLaenge);
papier.setOpaque(false); papier.setOpaque(false);
c.add(papier); c.add(papier);
papier.start(); papier.start();

View File

@ -11,6 +11,7 @@ import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.geom.Line2D; import java.awt.geom.Line2D;
import static java.lang.Math.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -26,19 +27,23 @@ public class Zeiger extends JComponent implements Runnable
private Line2D.Float linie; private Line2D.Float linie;
private BasicStroke stift; private BasicStroke stift;
private volatile float radius; private volatile float radius;
private volatile float minRadius; private volatile float xMitte;
private volatile float maxRadius; private volatile float yMitte;
private volatile double xAussen;
private volatile double yAussen;
private volatile double alpha;
private long schlafzeit; private long schlafzeit;
private int zeigerlaenge;
private ExecutorService eService; private ExecutorService eService;
private Future task; private Future task;
public Zeiger(long schlafzeit) public Zeiger(long schlafzeit, int zeigerlaenge)
{ {
this.schlafzeit = schlafzeit; this.schlafzeit = schlafzeit;
this.zeigerlaenge = zeigerlaenge;
linie = new Line2D.Float(); linie = new Line2D.Float();
stift = new BasicStroke(DICKE); stift = new BasicStroke(DICKE);
radius = 100; alpha = 0;
minRadius = DICKE;
eService = Executors.newSingleThreadExecutor(); eService = Executors.newSingleThreadExecutor();
task = null; task = null;
} }
@ -51,9 +56,9 @@ public class Zeiger extends JComponent implements Runnable
{ {
synchronized(this) synchronized(this)
{ {
if (radius > maxRadius) delta = -1f; alpha += 0.1;
if (radius < minRadius) delta = +1f; xAussen = xMitte +cos(2*PI*alpha) * radius;
radius += delta; yAussen = yMitte + sin(2*PI*alpha) * radius;
} }
this.repaint(); this.repaint();
try try
@ -86,20 +91,18 @@ public class Zeiger extends JComponent implements Runnable
float breite = this.getWidth() - 1; float breite = this.getWidth() - 1;
float hoehe = this.getHeight() - 1; float hoehe = this.getHeight() - 1;
synchronized(this)
{
maxRadius = -DICKE/2 + Math.min(breite, hoehe) / 2;
}
float x = breite/2 - radius; float radius = (min(hoehe, breite)/2 - 50) * 1/ zeigerlaenge;
float y = hoehe/2 - radius;
linie.setFrame(x, y, 2*radius, 2*radius); xMitte = breite/2;
yMitte = hoehe/2;
xAussen = xMitte +cos(2*PI*alpha) * radius;
yAussen = yMitte + sin(2*PI*alpha) * radius;
linie.setLine(xMitte, yMitte, xAussen, yAussen);
g2.setStroke(stift); g2.setStroke(stift);
g2.setPaint(Color.RED); g2.setPaint(Color.RED);
// g2.fill(ellipse);
// g2.setPaint(Color.BLACK);
g2.draw(linie); g2.draw(linie);
} }