StatisticGraph.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.geom.GeneralPath;
  8. import java.util.LinkedList;
  9. import javax.swing.JPanel;
  10. import classes.HolonObject;
  11. import ui.controller.Control;
  12. import ui.model.Model;
  13. public class StatisticGraph extends JPanel {
  14. public StatisticGraph() {
  15. }
  16. /**
  17. *
  18. */
  19. private static final long serialVersionUID = 1L;
  20. // model and controller
  21. private Model model;
  22. private Control controller;
  23. // Graphics2D
  24. private Graphics2D g2;
  25. GeneralPath path = new GeneralPath();
  26. // Data
  27. private LinkedList<HolonObject> objects = new LinkedList<>();
  28. /**
  29. * Constructor.
  30. *
  31. * @param model
  32. * the Model
  33. * @param control
  34. * the Controller
  35. */
  36. public StatisticGraph(final Model model, Control control) {
  37. this.controller = control;
  38. this.model = model;
  39. this.setBackground(Color.WHITE);
  40. }
  41. /**
  42. * Paints all Components on the Canvas.
  43. *
  44. * @param g
  45. * Graphics
  46. *
  47. */
  48. public void paintComponent(Graphics g) {
  49. super.paintComponent(g);
  50. path.reset();
  51. g2 = (Graphics2D) g;
  52. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  53. g2.setRenderingHints(rh);
  54. g2.setStroke(new BasicStroke(0));
  55. g2.setColor(Color.BLACK);
  56. for (int i = 0; i <= this.getWidth(); i += 10) {
  57. g2.drawLine(i, 0, i, this.getHeight());
  58. }
  59. for (int i = 0; i <= this.getHeight(); i += 5) {
  60. g2.drawLine(0, i, this.getWidth(), i);
  61. }
  62. g2.setColor(Color.BLUE);
  63. g2.setStroke(new BasicStroke(2));
  64. path.moveTo(0,this.getHeight());
  65. for (int i = 0; i <= 49; i++) {
  66. double coordY = Math.random()*this.getHeight();
  67. controller.addTextToConsole(""+i);
  68. path.lineTo(i*this.getWidth()/49, coordY);
  69. path.moveTo(i*this.getWidth()/49, coordY);
  70. }
  71. g2.draw(path);
  72. }
  73. }