You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Zeiger.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3. * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
  4. */
  5. package aufgabe8_grafik2d;
  6. import java.awt.BasicStroke;
  7. import java.awt.Color;
  8. import java.awt.Graphics;
  9. import java.awt.Graphics2D;
  10. import java.awt.RenderingHints;
  11. import java.awt.geom.Line2D;
  12. import static java.lang.Math.*;
  13. import java.util.concurrent.ExecutorService;
  14. import java.util.concurrent.Executors;
  15. import java.util.concurrent.Future;
  16. import javax.swing.JComponent;
  17. /**
  18. *
  19. * @author ahren
  20. */
  21. public class Zeiger extends JComponent implements Runnable
  22. {
  23. private static final float DICKE = 4f;
  24. private Line2D.Float linie;
  25. private BasicStroke stift;
  26. private volatile double radius;
  27. private volatile float xMitte;
  28. private volatile float yMitte;
  29. private volatile double xAussen;
  30. private volatile double yAussen;
  31. private volatile double alpha;
  32. private long schlafzeit;
  33. private int zeigerlaenge;
  34. private ExecutorService eService;
  35. private Future task;
  36. /**
  37. * Initialisiert einen Zeiger.
  38. * @param schlafzeit: Ist die Zeit zwischen den Ticks
  39. * @param zeigerlaenge: Ist die länge des Zeigers
  40. */
  41. public Zeiger(long schlafzeit, int zeigerlaenge)
  42. {
  43. this.schlafzeit = schlafzeit;
  44. this.zeigerlaenge = zeigerlaenge;
  45. linie = new Line2D.Float();
  46. stift = new BasicStroke(DICKE);
  47. alpha = 0;
  48. eService = Executors.newSingleThreadExecutor();
  49. task = null;
  50. }
  51. @Override
  52. public void run()
  53. {
  54. float delta = 1f;
  55. while(true)
  56. {
  57. synchronized(this)
  58. {
  59. alpha += 0.1;
  60. }
  61. this.repaint();
  62. try
  63. {
  64. Thread.sleep(schlafzeit);
  65. }
  66. catch (Exception ex)
  67. {
  68. //lg.severe(ex.toString());
  69. }
  70. }
  71. }
  72. public void start()
  73. {
  74. if (task == null)
  75. {
  76. task = eService.submit(this);
  77. }
  78. }
  79. @Override
  80. public void paintComponent(Graphics g)
  81. {
  82. super.paintComponent(g);
  83. Graphics2D g2 = (Graphics2D)g;
  84. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  85. RenderingHints.VALUE_ANTIALIAS_ON);
  86. float breite = this.getWidth() - 1;
  87. float hoehe = this.getHeight() - 1;
  88. radius = (min(hoehe, breite)/2 * 0.9) * 1/ zeigerlaenge;
  89. g2.translate(breite/2, hoehe/2);
  90. g2.rotate(alpha);
  91. xAussen = radius;
  92. yAussen = 0;
  93. linie.setLine(0, 0, xAussen, yAussen);
  94. g2.setStroke(stift);
  95. g2.setPaint(Color.RED);
  96. g2.draw(linie);
  97. }
  98. }