RouteSplitpanePlanner.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. ArrayList<ArrayList<Integer>> automatischcor = new ArrayList<ArrayList<Integer>>();
  20. private boolean automatisch;
  21. private int orientation;
  22. private int x, y, ex, ey;
  23. public RouteSplitpanePlanner(){
  24. setLayout(new FlowLayout());
  25. coordinaten = new ArrayList<ArrayList<Integer>>();
  26. automatisch = false;
  27. orientation = 0;
  28. }
  29. public void drawField(int c, int r, ArrayList<ArrayList<Integer>> cor, ArrayList<Character> stappen){
  30. clear();
  31. labels.clear();
  32. this.removeAll();
  33. coordinaten = cor;
  34. this.steps = stappen;
  35. this.rows = r + 1;
  36. this.columns = c + 1;
  37. for(int h = 0; h < rows -1; h++){
  38. labels.add(new ArrayList<MapLabel>());
  39. for(int v = 0; v < columns -1 ; v++){
  40. labels.get(h).add(new MapLabel(0 ,0, h, v));
  41. }
  42. }
  43. for(ArrayList<MapLabel> row : labels){
  44. for(final MapLabel label : row){
  45. add(label);
  46. label.addMouseListener(new MouseAdapter() {
  47. @Override
  48. public void mouseClicked(MouseEvent e) {
  49. labelClick(label, e.getButton());
  50. }
  51. });
  52. }
  53. }
  54. boolean first = true;
  55. for(int i = 0; i < coordinaten.size(); i++){
  56. MapLabel label = labels.get(coordinaten.get(i).get(0)).get(coordinaten.get(i).get(1));
  57. label.isSelected(true);
  58. if(!first){
  59. lines.add(new ArrayList<MapLabel>());
  60. lines.get(lines.size()-1).add(labels.get(coordinaten.get(i-1).get(0)).get(coordinaten.get(i-1).get(1)));
  61. lines.get(lines.size()-1).add(label);
  62. }
  63. first = false;
  64. }
  65. revalidate();
  66. repaint();
  67. }
  68. public void labelClick(MapLabel label, int e){
  69. if(automatisch){
  70. if(automatischcor.size() == 0 ){
  71. Iconbar.setMessage("Geef 2e coordinaat", 0);
  72. automatischcor.add(new ArrayList<Integer>());
  73. automatischcor.get(0).add(rows-label.xcoord-1);
  74. automatischcor.get(0).add(label.ycoord);
  75. }else{
  76. Iconbar.setMessage("Route gegenereerd!", 2000);
  77. label.isSelected(true);
  78. System.out.println( automatischcor.get(0).get(0));
  79. System.out.println( automatischcor.get(0).get(1));
  80. System.out.println(label.xcoord);
  81. System.out.println(label.ycoord);
  82. berekenRoute(label.ycoord, rows-label.xcoord-1, automatischcor.get(0).get(1), automatischcor.get(0).get(0), 0);
  83. System.out.println(coordinaten);
  84. System.out.println(steps);
  85. drawField(columns-1, rows-1, new ArrayList<ArrayList<Integer>>(coordinaten), new ArrayList<Character>(steps));
  86. }
  87. }else{
  88. if(e == 0x01){
  89. if(coordinaten.size() == 0){
  90. label.isSelected(true);
  91. coordinaten.add(new ArrayList<Integer>());
  92. coordinaten.get(coordinaten.size()-1).add(label.getXcoord());
  93. coordinaten.get(coordinaten.size()-1).add(label.getYcoord());
  94. coordinaten.get(coordinaten.size()-1).add(0);
  95. }else if(checkInRange(label.getXcoord(), label.getYcoord())){
  96. label.isSelected(true);
  97. coordinaten.add(new ArrayList<Integer>());
  98. coordinaten.get(coordinaten.size()-1).add(label.getXcoord());
  99. coordinaten.get(coordinaten.size()-1).add(label.getYcoord());
  100. lines.add(new ArrayList<MapLabel>());
  101. lines.get(lines.size()-1).add(labels.get(coordinaten.get(coordinaten.size()-2).get(0)).get(coordinaten.get(coordinaten.size()-2).get(1)));
  102. lines.get(lines.size()-1).add(label);
  103. setStep();
  104. repaint();
  105. }
  106. }else if(label.isSelected() && e == 0x03){
  107. if(coordinaten.size() > 1){
  108. if(label.getXcoord() == coordinaten.get(coordinaten.size()-1).get(0) && label.getYcoord() == coordinaten.get(coordinaten.size()-1).get(1) ){
  109. lines.remove(lines.size() -1);
  110. coordinaten.remove(coordinaten.size() -1);
  111. label.isSelected(false);
  112. orientation = coordinaten.get(coordinaten.size()-1).get(2);
  113. steps.remove(steps.size() -1);
  114. repaint();
  115. }
  116. }
  117. }
  118. }
  119. }
  120. public void stepBackwards(){
  121. MapLabel label =labels.get(coordinaten.get(coordinaten.size()-1).get(0)).get(coordinaten.get(coordinaten.size()-1).get(1));
  122. labelClick(label, 0x03);
  123. }
  124. public void paintComponent(Graphics g){
  125. super.paintComponent(g);
  126. Graphics2D graphics = (Graphics2D) g;
  127. graphics.setStroke(new BasicStroke(3));
  128. if( rows > 0 && columns > 0){
  129. int rowheight = getHeight()/rows + 1;
  130. int columnwidth = getWidth()/columns + 1;
  131. int x = 0;
  132. int y = rowheight;
  133. for(ArrayList<MapLabel> row : labels){
  134. for(final MapLabel label : row){
  135. label.setSize(x += columnwidth, y, 5, 5);
  136. }
  137. x = 0;
  138. y += rowheight;
  139. }
  140. x = y = 0;
  141. for(int i = 1; i <= rows + 1 ; i++){
  142. graphics.drawLine(0, y += rowheight, getWidth(), y);
  143. }
  144. for(int i = 1; i <= columns + 1; i++){
  145. graphics.drawLine(x += columnwidth, 0, x, getHeight());
  146. }
  147. graphics.setColor(new Color(255,0,0));
  148. for(ArrayList<MapLabel> labels: lines){
  149. graphics.drawLine(labels.get(0).getX1(), labels.get(0).getY1(), labels.get(1).getX1(), labels.get(1).getY1());
  150. }
  151. }
  152. }
  153. public void clear(){
  154. for(ArrayList<MapLabel> row : labels){
  155. for(MapLabel label : row){
  156. label.isSelected(false);
  157. }
  158. }
  159. automatisch = false;
  160. lines.clear();
  161. coordinaten.clear();
  162. steps.clear();
  163. orientation = 0;
  164. automatischcor.clear();
  165. ex = ey = x = y = 0;
  166. repaint();
  167. }
  168. public boolean checkInRange(int x, int y){
  169. int currentx = coordinaten.get(coordinaten.size() -1).get(0);
  170. int currenty = coordinaten.get(coordinaten.size() -1).get(1);
  171. if(currenty == y ){
  172. if(currentx + 1 == x){
  173. return true;
  174. }else if(currentx - 1 == x){
  175. return true;
  176. }
  177. }else if(currentx == x){
  178. if(currenty + 1 == y){
  179. return true;
  180. }else if(currenty - 1 == y){
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. public void setStep(){
  187. int last = coordinaten.size() -1;
  188. int x1 = coordinaten.get(last-1).get(1);
  189. int y1 = rows - coordinaten.get(last-1).get(0) -1;
  190. int x2 = coordinaten.get(last).get(1);
  191. int y2 = rows - coordinaten.get(last).get(0) - 1;
  192. if(x1 < x2)
  193. {
  194. if(orientation == 90)
  195. {
  196. steps.add('v');
  197. }
  198. if(orientation == 0)
  199. {
  200. turnright();
  201. steps.add('r');
  202. }
  203. if(orientation == 270)
  204. {
  205. keren();
  206. steps.add('a');
  207. }
  208. if(orientation == 180)
  209. {
  210. turnleft();
  211. steps.add('l');
  212. }
  213. }
  214. else
  215. {
  216. if(x1 > x2)
  217. {
  218. if(orientation == 270)
  219. {
  220. steps.add('v');
  221. }
  222. if(orientation == 0)
  223. {
  224. turnleft();
  225. steps.add('l');
  226. }
  227. if(orientation == 90)
  228. {
  229. keren();
  230. steps.add('k');
  231. }
  232. if(orientation == 180)
  233. {
  234. turnright();
  235. steps.add('r');
  236. }
  237. }
  238. }
  239. if(y1 < y2)
  240. {
  241. if(orientation == 0)
  242. {
  243. steps.add('v');
  244. }
  245. if(orientation == 270)
  246. {
  247. turnright();
  248. steps.add('r');
  249. }
  250. if(orientation == 180)
  251. {
  252. steps.add('a');
  253. }
  254. if(orientation == 90)
  255. {
  256. turnleft();
  257. steps.add('l');
  258. }
  259. }
  260. else
  261. {
  262. if(y1 > y2)
  263. {
  264. if(orientation == 180)
  265. {
  266. steps.add('v');
  267. }
  268. if(orientation == 90)
  269. {
  270. turnright();
  271. steps.add('r');
  272. }
  273. if(orientation == 0)
  274. {
  275. keren();
  276. steps.add('a');
  277. }
  278. if(orientation == 270)
  279. {
  280. turnleft();
  281. steps.add('l');
  282. }
  283. }
  284. }
  285. coordinaten.get(coordinaten.size()-1).add(orientation);
  286. System.out.println(steps);
  287. }
  288. public void turnleft()
  289. {
  290. orientation = orientation - 90;
  291. if (orientation < 0)
  292. {
  293. orientation = 270;
  294. }
  295. }
  296. public void turnright()
  297. {
  298. orientation = orientation + 90;
  299. if (orientation > 270)
  300. {
  301. orientation = 0;
  302. }
  303. }
  304. public void keren()
  305. {
  306. orientation = orientation + 180;
  307. if (orientation == 360)
  308. {
  309. orientation = 0;
  310. }
  311. if(orientation == 450)
  312. {
  313. orientation = 90;
  314. }
  315. }
  316. public void vooruit()
  317. {
  318. if (orientation == 0 && y < rows)
  319. {
  320. y ++;
  321. }
  322. if (orientation == 90 && x < columns)
  323. {
  324. x ++;
  325. }
  326. if (orientation == 180 && y > 0)
  327. {
  328. y --;
  329. }
  330. if (orientation == 270 && x > 0)
  331. {
  332. x --;
  333. }
  334. coordinaten.add(new ArrayList<Integer>());
  335. coordinaten.get(coordinaten.size()-1).add(rows-y-1);
  336. coordinaten.get(coordinaten.size()-1).add(x);
  337. coordinaten.get(coordinaten.size()-1).add(orientation);
  338. }
  339. public int getOrientation() {
  340. return orientation;
  341. }
  342. public void setOrientation(int o){
  343. orientation += o;
  344. if (orientation == 360)
  345. {
  346. orientation = 0;
  347. }else if(orientation == 450)
  348. {
  349. orientation = 90;
  350. }
  351. }
  352. public boolean isAutomatisch() {
  353. return automatisch;
  354. }
  355. public void setAutomatisch(boolean automatisch) {
  356. boolean labelselected = false;
  357. for(ArrayList<MapLabel> row:labels){
  358. for(MapLabel label1:row){
  359. if(label1.isSelected()){
  360. labelselected = true;
  361. }
  362. }
  363. }
  364. if(labelselected){
  365. Iconbar.setMessage("Zet tweede coordinaat", 0);
  366. automatischcor.add(new ArrayList<Integer>());
  367. automatischcor.get(0).add(coordinaten.get(coordinaten.size()-1).get(0));
  368. automatischcor.get(0).add(coordinaten.get(coordinaten.size()-1).get(1));
  369. System.out.println(automatischcor.get(0).get(0));
  370. System.out.println(automatischcor.get(0).get(1));
  371. }
  372. else{
  373. Iconbar.setMessage("Zet eerste coordinaat", 0);
  374. }
  375. this.automatisch = automatisch;
  376. }
  377. public void berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie)
  378. {
  379. coordinaten.add(new ArrayList<Integer>());
  380. coordinaten.get(coordinaten.size()-1).add(rows-beginy-1);
  381. coordinaten.get(coordinaten.size()-1).add(beginx);
  382. coordinaten.get(coordinaten.size()-1).add(rotatie);
  383. char[] route = new char[40];
  384. int teller = 0;
  385. orientation = rotatie;
  386. int lengte = 0;
  387. if(eindx <= columns && eindy <= rows && eindx >= 0 && eindy >= 0)
  388. {
  389. ex = eindx;
  390. ey = eindy;
  391. }else{
  392. }
  393. if(beginx <= columns && beginy <= rows && beginx >= 0 && beginy >= 0)
  394. {
  395. x = beginx;
  396. y = beginy;
  397. }else{
  398. }
  399. while(ex != x)
  400. {
  401. if(x < ex)
  402. {
  403. if(orientation == 90)
  404. {
  405. vooruit();
  406. steps.add('v');
  407. teller ++;
  408. }
  409. if(orientation == 0)
  410. {
  411. turnright();
  412. vooruit();
  413. steps.add('r');
  414. teller ++;
  415. }
  416. if(orientation == 270)
  417. {
  418. keren();
  419. vooruit();
  420. steps.add('k');
  421. teller ++;
  422. }
  423. if(orientation == 180)
  424. {
  425. turnleft();
  426. vooruit();
  427. steps.add('l');
  428. teller ++;
  429. }
  430. }
  431. else
  432. {
  433. if(x > ex)
  434. {
  435. if(orientation == 270)
  436. {
  437. vooruit();
  438. steps.add('v');
  439. teller ++;
  440. }
  441. if(orientation == 0)
  442. {
  443. turnleft();
  444. vooruit();
  445. steps.add('l');
  446. teller ++;
  447. }
  448. if(orientation == 90)
  449. {
  450. keren();
  451. vooruit();
  452. steps.add('k');
  453. teller ++;
  454. }
  455. if(orientation == 180)
  456. {
  457. turnright();
  458. vooruit();
  459. steps.add('r');
  460. teller ++;
  461. }
  462. }
  463. }
  464. }
  465. while(ey != y)
  466. {
  467. if(y < ey)
  468. {
  469. if(orientation == 0)
  470. {
  471. vooruit();
  472. steps.add('v');
  473. teller ++;
  474. }
  475. if(orientation == 270)
  476. {
  477. turnright();
  478. vooruit();
  479. steps.add('r');
  480. teller ++;
  481. }
  482. if(orientation == 180)
  483. {
  484. keren();
  485. vooruit();
  486. steps.add('k');
  487. teller ++;
  488. }
  489. if(orientation == 90)
  490. {
  491. turnleft();
  492. vooruit();
  493. steps.add('l');
  494. teller ++;
  495. }
  496. }
  497. else
  498. {
  499. if(y > ey)
  500. {
  501. if(orientation == 180)
  502. {
  503. vooruit();
  504. steps.add('v');
  505. teller ++;
  506. }
  507. if(orientation == 90)
  508. {
  509. turnright();
  510. vooruit();
  511. steps.add('r');
  512. teller ++;
  513. }
  514. if(orientation == 0)
  515. {
  516. keren();
  517. vooruit();
  518. steps.add('k');
  519. teller ++;
  520. }
  521. if(orientation == 270)
  522. {
  523. turnleft();
  524. vooruit();
  525. steps.add('l');
  526. teller ++;
  527. }
  528. }
  529. }
  530. }
  531. }
  532. }