Advertisement
dcndrd

Untitled

Nov 22nd, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package br.uefs.ecomp.Game.GraphicInterface;
  7.  
  8. import br.uefs.ecomp.Game.facade.GameFacade;
  9. import br.uefs.ecomp.Game.util.PathlessException;
  10. import br.uefs.ecomp.Game.util.VertexNotFoundException;
  11. import java.awt.BorderLayout;
  12. import java.awt.GridLayout;
  13. import java.awt.Label;
  14. import java.awt.event.ActionEvent;
  15. import java.awt.event.ActionListener;
  16. import java.io.FileNotFoundException;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import javax.swing.*;
  22. import javax.swing.filechooser.FileNameExtensionFilter;
  23.  
  24. /**
  25. *
  26. * @author dcandrade
  27. */
  28. public class MainInterface {
  29.  
  30. private static GameFacade gf;
  31. private JFrame windows;
  32. private JPanel mainPanel;
  33. JComboBox originCombo;
  34. JComboBox destinyCombo;
  35. private JEditorPane allPathsPane;
  36. private JEditorPane bestPathPane;
  37. JButton buttonLoad;
  38. JButton buttonShowPaths;
  39. JButton buttonDistance;
  40.  
  41. public MainInterface() {
  42. gf = new GameFacade();
  43. }
  44.  
  45. public static void main(String[] args) {
  46. new MainInterface().createScreen();
  47. }
  48.  
  49. private void createScreen() {
  50. prepareWindows();
  51. prepareMainPanel();
  52. prepareButtonLoad();
  53. showWindows();
  54. prepareCombos();
  55. prepareRouteFields();
  56. prepareButtonShowPath();
  57. prepareButtonDistance();
  58. loadButtons();
  59. }
  60.  
  61. private void prepareWindows() {
  62. windows = new JFrame("CGoes no Mundo da Fantasia");
  63. windows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64. }
  65.  
  66. private void prepareMainPanel() {
  67. mainPanel = new JPanel();
  68. BorderLayout bl = new BorderLayout();
  69. mainPanel.setLayout(new BorderLayout());
  70. windows.add(mainPanel);
  71. }
  72.  
  73. private void prepareButtonLoad() {
  74. buttonLoad = new JButton("Carregar arquivo");
  75. buttonLoad.addActionListener(new ActionListener() {
  76. @Override
  77. public void actionPerformed(ActionEvent e) {
  78. JFileChooser chooser = new JFileChooser();
  79. chooser.setFileFilter(new FileNameExtensionFilter(".txt", "txt"));
  80. if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  81. try {
  82. gf.importSettingsFile(chooser.getSelectedFile().getAbsolutePath());
  83. prepareOriginCombo(gf.getSpots());
  84. prepareDestinyCombo(gf.getSpots());
  85. JOptionPane.showMessageDialog(null, "Arquivo Carregado");
  86. } catch (FileNotFoundException ex) {
  87. JOptionPane.showMessageDialog(null, "Nao foi possivel ler o arquivo");
  88. }
  89. }
  90. }
  91. });
  92. }
  93.  
  94. private void prepareButtonShowPath() {
  95. buttonShowPaths = new JButton("Calcular Caminhos");
  96. buttonShowPaths.addActionListener(new ActionListener() {
  97. @Override
  98. public void actionPerformed(ActionEvent e) {
  99. if (!originCombo.getSelectedItem().toString().equals("Vazio!")) {
  100. fillPathFields(originCombo.getSelectedItem().toString(), destinyCombo.getSelectedItem().toString());
  101. } else {
  102. JOptionPane.showMessageDialog(null, "Carregue um arquivo primeiro");
  103. }
  104. }
  105. });
  106. }
  107.  
  108. private void prepareCombos() {
  109. prepareOriginCombo(gf.getSpots());
  110. prepareDestinyCombo(gf.getSpots());
  111.  
  112. JPanel originPanel = new JPanel(new GridLayout(1, 2));
  113. originPanel.add(new Label("Escolha a origem"));
  114. originPanel.add(destinyCombo);
  115.  
  116. JPanel destinyPanel = new JPanel(new GridLayout(1, 2));
  117. destinyPanel.add(new Label("Escolha o destino"));
  118. destinyPanel.add(originCombo);
  119.  
  120. JPanel combos = new JPanel(new BorderLayout());
  121. combos.add(originPanel, BorderLayout.EAST);
  122. combos.add(destinyPanel, BorderLayout.WEST);
  123. mainPanel.add(combos, BorderLayout.NORTH);
  124. mainPanel.updateUI();
  125. }
  126.  
  127. private void prepareOriginCombo(Iterator spots) {
  128. if (originCombo == null) {
  129. originCombo = new JComboBox();
  130. originCombo.addItem("Vazio!");
  131. originCombo.setSize(70, 30);
  132. } else {
  133. originCombo.removeItem("Vazio!");
  134. }
  135. while (spots.hasNext()) {
  136. originCombo.addItem(spots.next().toString());
  137. }
  138. }
  139.  
  140. private void prepareDestinyCombo(Iterator spots) {
  141. if (destinyCombo == null) {
  142. destinyCombo = new JComboBox();
  143. destinyCombo.addItem("Vazio!");
  144. destinyCombo.setSize(70, 30);
  145. destinyCombo.updateUI();
  146. } else {
  147. destinyCombo.removeItem("Vazio!");
  148. }
  149. while (spots.hasNext()) {
  150. destinyCombo.addItem(spots.next().toString());
  151. }
  152. }
  153.  
  154. private void showWindows() {
  155. windows.pack();
  156. windows.setSize(540, 540);
  157. windows.setVisible(true);
  158. }
  159.  
  160. private void prepareRouteFields() {
  161. allPathsPane = new JEditorPane();
  162. bestPathPane = new JEditorPane();
  163.  
  164. JScrollPane bestPathScroll = new JScrollPane(bestPathPane);
  165. JScrollPane allPathsScroll = new JScrollPane(allPathsPane);
  166.  
  167. JPanel panel = new JPanel(new GridLayout(2, 1));
  168.  
  169. JPanel bPPanel = new JPanel(new BorderLayout());
  170. bPPanel.add(new Label("Melhor Caminho"), BorderLayout.NORTH);
  171. bPPanel.add(bestPathScroll);
  172.  
  173. JPanel aPPanel = new JPanel(new BorderLayout());
  174. aPPanel.add(new Label("Todos os Caminhos"), BorderLayout.NORTH);
  175. aPPanel.add(allPathsScroll);
  176.  
  177. panel.add(bPPanel);
  178. panel.add(aPPanel);
  179.  
  180. mainPanel.add(panel, BorderLayout.WEST);
  181. mainPanel.updateUI();
  182. }
  183.  
  184. private void fillPathFields(String a, String b) {
  185. //Preenchendo todos os caminhos
  186. try {
  187. ArrayList<ArrayList<String>> paths = gf.getPossibleRoutes(a, b);
  188. String aText = "";
  189. for (int i = 0; i < paths.size(); i++) {
  190. ArrayList<String> p = paths.get(i);
  191. aText += "Caminho " + i + 1 + ":\n";
  192. for (String p1 : p) {
  193. aText += p1 + "\n";
  194. }
  195. aText += "\n";
  196. }
  197. allPathsPane.setText(aText);
  198. } catch (VertexNotFoundException ex) {
  199. allPathsPane.setText("Não existem caminhos!");
  200. }
  201. try {
  202. //Preenchendo melhor caminho
  203. ArrayList<String> bestPath = gf.getSmallerPath(a, b);
  204. String bText = "";
  205. for (String s : bestPath) {
  206. bText += s + "\n";
  207. }
  208.  
  209. bestPathPane.setText(bText);
  210. } catch (PathlessException ex) {
  211. }
  212. }
  213.  
  214. public void prepareButtonDistance() {
  215. buttonDistance = new JButton();
  216. buttonDistance.setText("Calcular distância");
  217. buttonDistance.addActionListener(new ActionListener() {
  218.  
  219. @Override
  220. public void actionPerformed(ActionEvent e) {
  221. try {
  222.  
  223. String origin = originCombo.getSelectedItem().toString();
  224. String destiny = destinyCombo.getSelectedItem().toString();
  225. if (!originCombo.getSelectedItem().toString().equals("Vazio!")) {
  226. double distance = gf.getLinearDistance(origin, destiny);
  227. JOptionPane.showMessageDialog(null, "A distância euclidiane entre " + origin + " e " + destiny + " é de " + distance);
  228. } else {
  229. JOptionPane.showMessageDialog(null, "Carregue um arquivo primeiro");
  230. }
  231. } catch (VertexNotFoundException ex) {
  232. }
  233. }
  234. });
  235. }
  236.  
  237. private void loadButtons() {
  238. JPanel panel = new JPanel(new GridLayout(1, 3));
  239. panel.add(buttonLoad);
  240. panel.add(buttonShowPaths);
  241. panel.add(buttonDistance);
  242. mainPanel.add(panel, BorderLayout.SOUTH);
  243. mainPanel.updateUI();
  244. }
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement