123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- /*
- * 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 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 minRadius;
- private volatile float maxRadius;
- private long schlafzeit;
- private ExecutorService eService;
- private Future task;
-
- public Zeiger(long schlafzeit)
- {
- this.schlafzeit = schlafzeit;
- linie = new Line2D.Float();
- stift = new BasicStroke(DICKE);
- radius = 100;
- minRadius = DICKE;
- eService = Executors.newSingleThreadExecutor();
- task = null;
- }
-
- @Override
- public void run()
- {
- float delta = 1f;
- while(true)
- {
- synchronized(this)
- {
- if (radius > maxRadius) delta = -1f;
- if (radius < minRadius) delta = +1f;
- radius += delta;
- }
- 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;
-
- synchronized(this)
- {
- maxRadius = -DICKE/2 + Math.min(breite, hoehe) / 2;
- }
-
- float x = breite/2 - radius;
- float y = hoehe/2 - radius;
-
- linie.setFrame(x, y, 2*radius, 2*radius);
-
- g2.setStroke(stift);
- g2.setPaint(Color.RED);
- // g2.fill(ellipse);
- // g2.setPaint(Color.BLACK);
- g2.draw(linie);
- }
-
- }
-
|