2023-11-27 11:53:50 +01:00

112 lines
2.5 KiB
Java

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package aufgabe8_grafik2d;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;
import static java.lang.Math.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.swing.JComponent;
/**
*
* @author ahren
*/
public class Zeiger extends JComponent implements Runnable
{
private static final float DICKE = 4f;
private Line2D.Float linie;
private BasicStroke stift;
private volatile float radius;
private volatile float xMitte;
private volatile float yMitte;
private volatile double xAussen;
private volatile double yAussen;
private volatile double alpha;
private long schlafzeit;
private int zeigerlaenge;
private ExecutorService eService;
private Future task;
public Zeiger(long schlafzeit, int zeigerlaenge)
{
this.schlafzeit = schlafzeit;
this.zeigerlaenge = zeigerlaenge;
linie = new Line2D.Float();
stift = new BasicStroke(DICKE);
alpha = 0;
eService = Executors.newSingleThreadExecutor();
task = null;
}
@Override
public void run()
{
float delta = 1f;
while(true)
{
synchronized(this)
{
alpha += 0.1;
xAussen = xMitte +cos(2*PI*alpha) * radius;
yAussen = yMitte + sin(2*PI*alpha) * radius;
}
this.repaint();
try
{
Thread.sleep(schlafzeit);
}
catch (Exception ex)
{
//lg.severe(ex.toString());
}
}
}
public void start()
{
if (task == null)
{
task = eService.submit(this);
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
float breite = this.getWidth() - 1;
float hoehe = this.getHeight() - 1;
float radius = (min(hoehe, breite)/2 - 50) * 1/ zeigerlaenge;
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.setPaint(Color.RED);
g2.draw(linie);
}
}