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.

Kreis.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package grafik2d;
  7. /**
  8. *
  9. * @author nobody
  10. */
  11. import java.awt.BasicStroke;
  12. import java.awt.Color;
  13. import java.awt.Graphics;
  14. import java.awt.Graphics2D;
  15. import java.awt.RenderingHints;
  16. import java.awt.geom.Ellipse2D;
  17. import java.util.logging.Logger;
  18. import javax.swing.JComponent;
  19. /**
  20. *
  21. * @author le
  22. */
  23. public class Kreis extends JComponent implements Runnable
  24. {
  25. private Ellipse2D.Float kreis;
  26. private BasicStroke pinsel;
  27. private float radius;
  28. private float minRadius;
  29. private float maxRadius;
  30. private Thread thd;
  31. private long schlafzeit;
  32. private static final float DICKE = 8f;
  33. private static Logger lg = Logger.getLogger("grafik");
  34. public Kreis(long schlafzeit)
  35. {
  36. this.schlafzeit = schlafzeit;
  37. minRadius = DICKE;
  38. maxRadius = 100;
  39. kreis = new Ellipse2D.Float();
  40. pinsel = new BasicStroke(DICKE);
  41. radius = 10;
  42. thd = null;
  43. }
  44. public void start()
  45. {
  46. if (thd == null)
  47. {
  48. thd = new Thread(this);
  49. thd.start();
  50. }
  51. }
  52. @Override
  53. public void paintComponent(Graphics g)
  54. {
  55. super.paintComponent(g);
  56. Graphics2D g2 = (Graphics2D)g;
  57. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  58. RenderingHints.VALUE_ANTIALIAS_ON);
  59. int breite = this.getWidth() - 1;
  60. int hoehe = this.getHeight() - 1;
  61. maxRadius = -DICKE/2f + Math.min(breite/2f, hoehe/2f);
  62. float x = breite/2f - radius;
  63. float y = hoehe/2f - radius;
  64. kreis.setFrame(x, y, radius*2, radius*2);
  65. g2.setStroke(pinsel);
  66. // g2.setPaint(Color.RED);
  67. // g2.fill(kreis);
  68. g2.setPaint(Color.BLACK);
  69. g2.draw(kreis);
  70. }
  71. @Override
  72. public void run()
  73. {
  74. float delta = 1f;
  75. while (true)
  76. {
  77. if (radius > maxRadius) delta = -1f;
  78. if (radius < minRadius) delta = +1f;
  79. radius += delta;
  80. this.repaint();
  81. try
  82. {
  83. Thread.sleep(schlafzeit);
  84. }
  85. catch (InterruptedException iex)
  86. {
  87. lg.warning(iex.toString());
  88. }
  89. }
  90. }
  91. }