RouteSplitpanePlanner.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package panels;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.util.ArrayList;
  10. import javax.swing.JPanel;
  11. import javax.xml.stream.events.Characters;
  12. import customComponents.MapLabel;
  13. public class RouteSplitpanePlanner extends JPanel{
  14. private int rows, columns;
  15. private ArrayList<ArrayList<MapLabel>> labels = new ArrayList<ArrayList<MapLabel>>();
  16. ArrayList<ArrayList<Integer>> coordinaten;
  17. private ArrayList<ArrayList<MapLabel>> lines = new ArrayList<ArrayList<MapLabel>>();
  18. ArrayList<Character> steps = new ArrayList<Character>();
  19. private boolean automatisch;
  20. private int orientation;
  21. public RouteSplitpanePlanner(){
  22. setLayout(new FlowLayout());
  23. coordinaten = new ArrayList<ArrayList<Integer>>();
  24. automatisch = false;
  25. orientation = 0;
  26. }
  27. public void drawField(int c, int r, ArrayList<ArrayList<Integer>> cor){
  28. clear();
  29. coordinaten = cor;
  30. this.rows = r + 1;
  31. this.columns = c + 1;
  32. for(int h = 0; h < rows -1; h++){
  33. labels.add(new ArrayList<MapLabel>());
  34. for(int v = 0; v < columns -1 ; v++){
  35. labels.get(h).add(new MapLabel(0 ,0, h, v));
  36. }
  37. }
  38. for(ArrayList<MapLabel> row : labels){
  39. for(final MapLabel label : row){
  40. add(label);
  41. label.addMouseListener(new MouseAdapter() {
  42. @Override
  43. public void mouseClicked(MouseEvent e) {
  44. if(automatisch){
  45. }else{
  46. if(!label.isSelected() && e.getButton() == e.BUTTON1){
  47. if(coordinaten.size() == 0){
  48. label.isSelected(true);
  49. coordinaten.add(new ArrayList<Integer>());
  50. coordinaten.get(coordinaten.size()-1).add(label.getXcoord());
  51. coordinaten.get(coordinaten.size()-1).add(label.getYcoord());
  52. }else if(checkInRange(label.getXcoord(), label.getYcoord())){
  53. label.isSelected(true);
  54. coordinaten.add(new ArrayList<Integer>());
  55. coordinaten.get(coordinaten.size()-1).add(label.getXcoord());
  56. coordinaten.get(coordinaten.size()-1).add(label.getYcoord());
  57. lines.add(new ArrayList<MapLabel>());
  58. lines.get(lines.size()-1).add(labels.get(coordinaten.get(coordinaten.size()-2).get(0)).get(coordinaten.get(coordinaten.size()-2).get(1)));
  59. lines.get(lines.size()-1).add(label);
  60. repaint();
  61. }
  62. }else if(label.isSelected() && e.getButton() == e.BUTTON3){
  63. if(coordinaten.size() > 1){
  64. if(label.getXcoord() == coordinaten.get(coordinaten.size()-1).get(0) && label.getYcoord() == coordinaten.get(coordinaten.size()-1).get(1) ){
  65. lines.remove(lines.size() -1);
  66. coordinaten.remove(coordinaten.size() -1);
  67. label.isSelected(false);
  68. repaint();
  69. }
  70. }
  71. }
  72. }
  73. }
  74. });
  75. }
  76. }
  77. boolean first = true;
  78. for(int i = 0; i < coordinaten.size(); i++){
  79. MapLabel label = labels.get(coordinaten.get(i).get(0)).get(coordinaten.get(i).get(1));
  80. label.isSelected(true);
  81. if(!first){
  82. lines.add(new ArrayList<MapLabel>());
  83. lines.get(lines.size()-1).add(labels.get(coordinaten.get(i-1).get(0)).get(coordinaten.get(i-1).get(1)));
  84. lines.get(lines.size()-1).add(label);
  85. }
  86. first = false;
  87. }
  88. repaint();
  89. }
  90. public void paintComponent(Graphics g){
  91. super.paintComponent(g);
  92. Graphics2D graphics = (Graphics2D) g;
  93. graphics.setStroke(new BasicStroke(3));
  94. if( rows > 0 && columns > 0){
  95. int rowheight = getHeight()/rows + 1;
  96. int columnwidth = getWidth()/columns + 1;
  97. int x = 0;
  98. int y = rowheight;
  99. for(ArrayList<MapLabel> row : labels){
  100. for(final MapLabel label : row){
  101. label.setSize(x += columnwidth, y, 5, 5);
  102. }
  103. x = 0;
  104. y += rowheight;
  105. }
  106. x = y = 0;
  107. for(int i = 1; i <= rows + 1 ; i++){
  108. graphics.drawLine(0, y += rowheight, getWidth(), y);
  109. }
  110. for(int i = 1; i <= columns + 1; i++){
  111. graphics.drawLine(x += columnwidth, 0, x, getHeight());
  112. }
  113. graphics.setColor(new Color(255,0,0));
  114. for(ArrayList<MapLabel> labels: lines){
  115. graphics.drawLine(labels.get(0).getX1(), labels.get(0).getY1(), labels.get(1).getX1(), labels.get(1).getY1());
  116. }
  117. }
  118. }
  119. public void clear(){
  120. for(ArrayList<MapLabel> row : labels){
  121. for(MapLabel label : row){
  122. label.isSelected(false);
  123. }
  124. }
  125. lines.clear();
  126. coordinaten.clear();
  127. steps.clear();
  128. orientation = 0;
  129. repaint();
  130. }
  131. public boolean checkInRange(int x, int y){
  132. int currentx = coordinaten.get(coordinaten.size() -1).get(0);
  133. int currenty = coordinaten.get(coordinaten.size() -1).get(1);
  134. if(currenty == y ){
  135. if(currentx + 1 == x){
  136. setStep("down");
  137. return true;
  138. }else if(currentx - 1 == x){
  139. setStep("up");
  140. return true;
  141. }
  142. }else if(currentx == x){
  143. if(currenty + 1 == y){
  144. setStep("right");
  145. return true;
  146. }else if(currenty - 1 == y){
  147. setStep("left");
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. public void setStep(String orient){
  154. }
  155. public void setOrientation(int o){
  156. orientation += o;
  157. if (orientation == 360)
  158. {
  159. orientation = 0;
  160. }else if(orientation == 450)
  161. {
  162. orientation = 90;
  163. }
  164. }
  165. public boolean isAutomatisch() {
  166. return automatisch;
  167. }
  168. public void setAutomatisch(boolean automatisch) {
  169. this.automatisch = automatisch;
  170. }
  171. }