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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14. import java.util.concurrent.Future;
  15. import javax.swing.JComponent;
  16. /**
  17. *
  18. * @author ahren
  19. */
  20. public class Zeiger extends JComponent implements Runnable
  21. {
  22. private static final float DICKE = 4f;
  23. private Line2D.Float linie;
  24. private BasicStroke stift;
  25. private volatile float radius;
  26. private volatile float minRadius;
  27. private volatile float maxRadius;
  28. private long schlafzeit;
  29. private ExecutorService eService;
  30. private Future task;
  31. public Zeiger(long schlafzeit)
  32. {
  33. this.schlafzeit = schlafzeit;
  34. linie = new Line2D.Float();
  35. stift = new BasicStroke(DICKE);
  36. radius = 100;
  37. minRadius = DICKE;
  38. eService = Executors.newSingleThreadExecutor();
  39. task = null;
  40. }
  41. @Override
  42. public void run()
  43. {
  44. float delta = 1f;
  45. while(true)
  46. {
  47. synchronized(this)
  48. {
  49. if (radius > maxRadius) delta = -1f;
  50. if (radius < minRadius) delta = +1f;
  51. radius += delta;
  52. }
  53. this.repaint();
  54. try
  55. {
  56. Thread.sleep(schlafzeit);
  57. }
  58. catch (Exception ex)
  59. {
  60. //lg.severe(ex.toString());
  61. }
  62. }
  63. }
  64. public void start()
  65. {
  66. if (task == null)
  67. {
  68. task = eService.submit(this);
  69. }
  70. }
  71. @Override
  72. public void paintComponent(Graphics g)
  73. {
  74. super.paintComponent(g);
  75. Graphics2D g2 = (Graphics2D)g;
  76. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  77. RenderingHints.VALUE_ANTIALIAS_ON);
  78. float breite = this.getWidth() - 1;
  79. float hoehe = this.getHeight() - 1;
  80. synchronized(this)
  81. {
  82. maxRadius = -DICKE/2 + Math.min(breite, hoehe) / 2;
  83. }
  84. float x = breite/2 - radius;
  85. float y = hoehe/2 - radius;
  86. linie.setFrame(x, y, 2*radius, 2*radius);
  87. g2.setStroke(stift);
  88. g2.setPaint(Color.RED);
  89. // g2.fill(ellipse);
  90. // g2.setPaint(Color.BLACK);
  91. g2.draw(linie);
  92. }
  93. }