Guest User

Untitled

a guest
Feb 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. package de.mathnat.neuralnetwork.graph;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.geom.Line2D;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. public class GraphPanel extends JPanel {
  11.  
  12. private List<Map.Entry<Double, Double>> testData = new ArrayList<>();
  13. private List<Map.Entry<Double, Double>> trainData = new ArrayList<>();
  14.  
  15. @Override
  16. protected void paintComponent(Graphics graphics) {
  17. super.paintComponent(graphics);
  18. draw(testData, graphics, Color.RED, Color.LIGHT_GRAY);
  19. draw(trainData, graphics, Color.BLUE, Color.LIGHT_GRAY);
  20. }
  21.  
  22. private void draw(List<Map.Entry<Double, Double>> data, Graphics graphics, Color nodeColor, Color edgeColor) {
  23. int start = 0;
  24. if(data.size() > getWidth()) {
  25. start = data.size() - getWidth();
  26. }
  27. for (int i = start; i < data.size(); i++) {
  28. Map.Entry<Double, Double> currentEntry = data.get(i);
  29. Map.Entry<Double, Double> nextEntry = i + 1 >= data.size() ? null : data.get(i + 1);
  30. int x0 = currentEntry.getKey().intValue() - start;
  31. int y0 = getHeight() - currentEntry.getValue().intValue();
  32. if (nextEntry != null) {
  33. graphics.setColor(edgeColor);
  34. int x1 = nextEntry.getKey().intValue() - start;
  35. int y1 = getHeight() - nextEntry.getValue().intValue();
  36. drawLine(graphics, x0, y0, x1, y1);
  37. }
  38. graphics.setColor(nodeColor);
  39. drawCircle(graphics, x0, y0, 6);
  40. }
  41. }
  42.  
  43. private void drawCircle(Graphics graphics, int x, int y, int radius) {
  44. graphics.fillOval(x - radius / 2, y - radius / 2, radius, radius);
  45. }
  46.  
  47. private void drawLine(Graphics graphics, int x0, int y0, int x1, int y1) {
  48. Graphics2D graphics2D = (Graphics2D) graphics;
  49. graphics2D.setStroke(new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
  50. graphics2D.draw(new Line2D.Double(x0, y0, x1, y1));
  51. }
  52.  
  53. public void clearData() {
  54. testData.clear();
  55. }
  56.  
  57. public void addTestData(Map.Entry<Double, Double> datum) {
  58. testData.add(datum);
  59. }
  60.  
  61. public void addTrainData(Map.Entry<Double, Double> datum) {
  62. trainData.add(datum);
  63. }
  64. }
Add Comment
Please, Sign In to add comment