Posted By

wickedhood on 07/01/14


Tagged


Versions (?)

graph nodes


 / Published in: Java
 

the class that draws the nodes so that they are easily viewed for testing reasons

  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5.  
  6. public class GraphNodes<T> extends JPanel {
  7.  
  8. public GraphNodes() {
  9.  
  10. }
  11.  
  12. public void paintComponent(Graphics g) {
  13. super.paintComponent(g);
  14. this.setBackground(Color.BLACK);
  15.  
  16. //DRAW "grid" every 20 pixels
  17. g.setColor(Color.GRAY);
  18. for (int x = 50; x <= 450; x += 20) {
  19. g.drawLine(50, x, 450, x);
  20. g.drawLine(x, 50, x, 450);
  21. }
  22. g.setColor(Color.WHITE);
  23. int middleX = 250;
  24. int middleY = 250;
  25. //X positive axis
  26. g.drawLine(middleX, middleY, middleX, middleY + 200);
  27. //X negative axis
  28. g.drawLine(middleX, middleY, middleX, middleY - 200);
  29. //Y positive axis
  30. g.drawLine(middleX, middleY, middleX + 200, middleY);
  31. //Y negative axis
  32. g.drawLine(middleX, middleY, middleX - 200, middleY);
  33. /*
  34.   * HELP ME SEE THE WHERE g.setColor(Color.BLUE);
  35.   * g.drawString("(450,450)", 450, 450); g.drawString("(50,50)", 50, 50);
  36.   * g.drawString("(450,50)", 450, 50); g.drawString("(50,450)", 50, 460);
  37.   *
  38.   * Draw some text g.setColor(Color.GREEN); //sec, third parameters is
  39.   * coord g.drawString("(250,250)", 250, 250);
  40.   */
  41. OrderedTree<String> ot = new OrderedTree<>();
  42. ot.add("A", 0, 0);
  43. ot.add("C", 2, 2);
  44. ot.add("B", 2, -2);
  45. ot.add("C", "E", 4, 0);
  46. ot.add("C", "D", 4, 4);
  47. ot.add("E", "F", 4, 2);
  48. ot.add("E", "G", 6, 2);
  49. ot.add("E", "H", 5, -2);
  50. ot.add("B", "I", -5, -8);
  51. ot.add("I", "J", -6, -3);
  52. ot.add("I", "K", -8, -10);
  53. ot.add("I", "L", -3, -3);
  54. ot.add("J", "M", -6, -2);
  55. ot.add("J", "N", -6, -4);
  56. ot.add("J", "O", -9, -5);
  57. ot.add("J", "P", -8, -3);
  58. ot.add("C", "Q", -5, 5);
  59. ot.add("Q", "R", -7, 7);
  60. ot.add("Q", "S", -3, 9);
  61. ot.add("Q", "T", -3, 1);
  62. ot.add("H", "U", 5, -5);
  63. ot.add("U", "V", 5, -4);
  64. ot.add("U", "W", 8, -6);
  65. ot.add("U", "X", 7, -7);
  66. ot.add("X", "Y", 9, -2);
  67. ot.add("Y", "Z", 8, -3);
  68. ot.addRelationship("A", "M");
  69. ot.addRelationship("H", "Z");
  70. ot.addRelationship("B", "V");
  71. ot.addRelationship("O", "K");
  72. ot.addRelationship("T", "A");
  73. for(Node<String> n : ot.allNodes){
  74. drawNode(g,n);
  75. if(n.children.size() >0){
  76. for(Node<String> c : n.children){
  77. drawNodeLine(g,n,c);
  78. }
  79. }
  80. }
  81. String end = "X";
  82. ot.getPath("S",end);
  83. String s ="";
  84. for(Node<String> m : ot.path){
  85. s+=m.element.toString();
  86. }
  87. System.out.println(s.substring(0,s.indexOf(end)+1));
  88.  
  89. }
  90.  
  91. private void drawNodeLine(Graphics g, Node<String> n1, Node<String> n2) {
  92. g.setColor(Color.LIGHT_GRAY);
  93. g.drawLine(getGridX(n1.x), getGridY(n1.y), getGridX(n2.x), getGridY(n2.y));
  94. }
  95.  
  96. private int getGridX(int x) {
  97. int xO = 20;
  98. return 250 + (x * xO);
  99. }
  100.  
  101. private int getGridY(int y) {
  102. int c = -1 * y;
  103. int yO = 20;
  104. return 250 + (c * yO);
  105. }
  106.  
  107. private String getRandomLetter() {
  108. return (char) ((Math.random() * 26) + 97) + "";
  109. }
  110.  
  111. private int getRandomNumber() {
  112. return (int) (Math.random() * 20) - 10;
  113. }
  114.  
  115. private void drawNode(Graphics g, Node n) {
  116. drawNode(g, n.element.toString(), n.x, n.y);
  117. }
  118.  
  119. private void drawNode(Graphics g, String n, int x, int y) {
  120. int c = -1 * y;
  121. int xO = 20;
  122. int yO = 20;
  123. int totX = 245 + (x * xO);
  124. int totY = 245 + (c * yO);
  125. g.setColor(Color.BLUE);
  126. g.drawOval(totX, totY, 10, 10);
  127.  
  128. g.setColor(Color.WHITE);
  129.  
  130. g.drawString(n, totX + 2, totY + 10);
  131. }
  132.  
  133. public static void main(String args[]) {
  134. JFrame f = new JFrame("Graph Nodes");
  135. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  136. GraphNodes t = new GraphNodes();
  137.  
  138. f.add(t);
  139. f.setSize(700, 700);
  140. f.setVisible(true);
  141.  
  142.  
  143. }
  144. }

Report this snippet  

You need to login to post a comment.