Browse Source

mit sinus und cosinus implementiert

master
ahren 1 year ago
parent
commit
53c8ddabb9
3 changed files with 24 additions and 20 deletions
  1. 1
    0
      .gitignore
  2. 3
    3
      src/aufgabe8_grafik2d/Start.java
  3. 20
    17
      src/aufgabe8_grafik2d/Zeiger.java

+ 1
- 0
.gitignore View File

/build/

+ 3
- 3
src/aufgabe8_grafik2d/Start.java View File

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());
Zeiger papier = new Zeiger(schlafzeit);
long schlafzeit = (long)(100*zeigerLaenge);
Zeiger papier = new Zeiger(schlafzeit, zeigerLaenge);
papier.setOpaque(false); papier.setOpaque(false);
c.add(papier); c.add(papier);
papier.start(); papier.start();

+ 20
- 17
src/aufgabe8_grafik2d/Zeiger.java View File

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;
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 maxRadius;
private volatile float xMitte;
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;
minRadius = DICKE;
alpha = 0;
eService = Executors.newSingleThreadExecutor(); eService = Executors.newSingleThreadExecutor();
task = null; task = null;
} }
{ {
synchronized(this) synchronized(this)
{ {
if (radius > maxRadius) delta = -1f;
if (radius < minRadius) delta = +1f;
radius += delta;
alpha += 0.1;
xAussen = xMitte +cos(2*PI*alpha) * radius;
yAussen = yMitte + sin(2*PI*alpha) * radius;
} }
this.repaint(); this.repaint();
try try
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 radius = (min(hoehe, breite)/2 - 50) * 1/ zeigerlaenge;
float x = breite/2 - radius;
float y = hoehe/2 - radius;
xMitte = breite/2;
yMitte = hoehe/2;
xAussen = xMitte +cos(2*PI*alpha) * radius;
yAussen = yMitte + sin(2*PI*alpha) * radius;
linie.setFrame(x, y, 2*radius, 2*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);
} }

Loading…
Cancel
Save