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.

Gerade.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 grafik;
  7. import grafik.logger.OhmLogger;
  8. import java.awt.BasicStroke;
  9. import java.awt.Color;
  10. import java.awt.Graphics;
  11. import java.awt.Graphics2D;
  12. import java.awt.RenderingHints;
  13. import java.awt.geom.AffineTransform;
  14. import java.awt.geom.Ellipse2D;
  15. import java.awt.geom.Line2D;
  16. import java.util.logging.Logger;
  17. import javax.swing.JComponent;
  18. /**
  19. *
  20. * @author le
  21. */
  22. public class Gerade extends JComponent implements Runnable
  23. {
  24. private Line2D.Double line;
  25. private BasicStroke pinsel;
  26. private float angle;
  27. private float radius;
  28. private Thread thd;
  29. private long sleepTime;
  30. private static final float DICKE = 8f;
  31. private static Logger lg = OhmLogger.getLogger();
  32. private float x1, x2, y1, y2;
  33. public Gerade(long sleepTime)
  34. {
  35. this.sleepTime = sleepTime;
  36. line = new Line2D.Double();
  37. angle = 0;
  38. radius = 100;
  39. pinsel = new BasicStroke(DICKE);
  40. thd = null;
  41. start();
  42. }
  43. public void start()
  44. {
  45. if (thd == null)
  46. {
  47. lg.info("start");
  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. x1 = breite/2f;
  62. y1 = hoehe/2f;
  63. /** Rotate with Angle, sin and cos */
  64. // x2 = (float) (radius * Math.cos((2 * Math.PI * angle) / 360) + x1);
  65. // y2 = (float) (radius * Math.sin((2 * Math.PI * angle) / 360) + y1);
  66. line.setLine(x1, y1, x2, y2);
  67. /** Rotate with transforms */
  68. g2.translate(x1, y1);
  69. g2.rotate(Math.toRadians(angle));
  70. g2.setStroke(pinsel);
  71. g2.setPaint(Color.RED);
  72. g2.fill(line);
  73. g2.setPaint(Color.BLACK);
  74. g2.draw(line);
  75. }
  76. @Override
  77. public void run()
  78. {
  79. while (true)
  80. {
  81. if(angle >= 360){
  82. angle = 0;
  83. } else {
  84. angle += 1;
  85. }
  86. // if (radius > maxRadius) delta = -1f;
  87. // if (radius < minRadius) delta = +1f;
  88. // radius += delta;
  89. this.repaint();
  90. try
  91. {
  92. Thread.sleep(sleepTime);
  93. }
  94. catch (InterruptedException iex)
  95. {
  96. lg.warning(iex.toString());
  97. }
  98. }
  99. }
  100. }