Advertisement
dcndrd

Untitled

Nov 22nd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 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. mainPanel.setLayout(new BorderLayout());
  69. windows.add(mainPanel);
  70. }
  71.  
  72. private void prepareButtonLoad() {
  73. buttonLoad = new JButton("Carregar arquivo");
  74. buttonLoad.addActionListener(new ActionListener() {
  75. @Override
  76. public void actionPerformed(ActionEvent e) {
  77. JFileChooser chooser = new JFileChooser();
  78. chooser.setFileFilter(new FileNameExtensionFilter("Apenas txt", "txt"));
  79. if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
  80. try {
  81. gf.importSettingsFile(chooser.getSelectedFile().getAbsolutePath());
  82. prepareOriginCombo(gf.getSpots());
  83. prepareDestinyCombo(gf.getSpots());
  84. JOptionPane.showMessageDialog(null, "Arquivo Carregado");
  85. } catch (FileNotFoundException ex) {
  86. JOptionPane.showMessageDialog(null, "Nao foi possivel ler o arquivo");
  87. }
  88. }
  89. }
  90. });
  91. }
  92.  
  93. private void prepareButtonShowPath(){
  94. buttonShowPaths = new JButton("Calcular Caminhos");
  95. buttonShowPaths.addActionListener(new ActionListener() {
  96. @Override
  97. public void actionPerformed(ActionEvent e) {
  98. fillPathFields(originCombo.getSelectedItem().toString(), destinyCombo.getSelectedItem().toString());
  99. }
  100. });
  101. }
  102.  
  103. private void prepareCombos() {
  104. prepareOriginCombo(gf.getSpots());
  105. prepareDestinyCombo(gf.getSpots());
  106.  
  107. JPanel originPanel = new JPanel(new GridLayout(1, 2));
  108. originPanel.add(new Label("Escolha a origem"));
  109. originPanel.add(destinyCombo);
  110.  
  111. JPanel destinyPanel = new JPanel(new GridLayout(1, 2));
  112. destinyPanel.add(new Label("Escolha o destino"));
  113. destinyPanel.add(originCombo);
  114.  
  115. JPanel combos = new JPanel(new BorderLayout());
  116. combos.add(originPanel, BorderLayout.EAST);
  117. combos.add(destinyPanel, BorderLayout.WEST);
  118. mainPanel.add(combos, BorderLayout.NORTH);
  119. mainPanel.updateUI();
  120. }
  121.  
  122. private void prepareOriginCombo(Iterator spots) {
  123. if (originCombo == null) {
  124. originCombo = new JComboBox();
  125. originCombo.addItem("Vazio!");
  126. originCombo.setSize(70, 30);
  127. } else {
  128. originCombo.removeItem("Vazio!");
  129. }
  130. while (spots.hasNext()) {
  131. originCombo.addItem(spots.next().toString());
  132. }
  133. }
  134.  
  135. private void prepareDestinyCombo(Iterator spots) {
  136. if (destinyCombo == null) {
  137. destinyCombo = new JComboBox();
  138. destinyCombo.addItem("Vazio!");
  139. destinyCombo.setSize(70, 30);
  140. destinyCombo.updateUI();
  141. } else {
  142. destinyCombo.removeItem("Vazio!");
  143. }
  144. while (spots.hasNext()) {
  145. destinyCombo.addItem(spots.next().toString());
  146. }
  147. }
  148.  
  149. private void showWindows() {
  150. windows.pack();
  151. windows.setSize(540, 540);
  152. windows.setVisible(true);
  153. }
  154.  
  155. private void prepareRouteFields() {
  156. allPathsPane = new JEditorPane();
  157. bestPathPane = new JEditorPane();
  158.  
  159. JScrollPane bestPathScroll = new JScrollPane(bestPathPane);
  160. JScrollPane allPathsScroll = new JScrollPane(allPathsPane);
  161.  
  162. JPanel panel = new JPanel(new GridLayout(2, 1));
  163.  
  164. JPanel bPPanel = new JPanel(new BorderLayout());
  165. bPPanel.add(new Label("Melhor Caminho"), BorderLayout.NORTH);
  166. bPPanel.add(bestPathScroll);
  167.  
  168. JPanel aPPanel = new JPanel(new BorderLayout());
  169. aPPanel.add(new Label("Todos os Caminhos"), BorderLayout.NORTH);
  170. aPPanel.add(allPathsScroll);
  171.  
  172. panel.add(bPPanel);
  173. panel.add(aPPanel);
  174.  
  175. mainPanel.add(panel, BorderLayout.WEST);
  176. mainPanel.updateUI();
  177. }
  178.  
  179. private void fillPathFields(String a, String b){
  180. //Preenchendo todos os caminhos
  181. try {
  182. ArrayList<ArrayList<String>> paths = gf.getPossibleRoutes(a, b);
  183. String aText="";
  184. for(int i=0; i<paths.size(); i++){
  185. ArrayList<String> p = paths.get(i);
  186. aText+="Caminho "+i+1+":\n";
  187. for (String p1 : p) {
  188. aText += p1 + "\n";
  189. }
  190. aText+="\n";
  191. }
  192. allPathsPane.setText(aText);
  193. } catch (VertexNotFoundException ex) {
  194. }
  195. try {
  196. //Preenchendo melhor caminho
  197. ArrayList<String> bestPath = gf.getSmallerPath(a, b);
  198. String bText="";
  199. for(String s: bestPath){
  200. bText+=s+"\n";
  201. }
  202.  
  203. bestPathPane.setText(bText);
  204. } catch (PathlessException ex) {
  205. }
  206. }
  207.  
  208. public void prepareButtonDistance(){
  209. buttonDistance = new JButton();
  210. buttonDistance.setText("Calcular distância");
  211. buttonDistance.addActionListener(new ActionListener() {
  212.  
  213. @Override
  214. public void actionPerformed(ActionEvent e) {
  215. try {
  216. String origin = originCombo.getSelectedItem().toString();
  217. String destiny = destinyCombo.getSelectedItem().toString();
  218. double distance = gf.getLinearDistance(origin, destiny);
  219. JOptionPane.showMessageDialog(null, "A distância euclidiane entre "+origin+" e "+destiny+" é de "+distance);
  220. } catch (VertexNotFoundException ex) {
  221. }
  222. }
  223. });
  224. }
  225.  
  226. private void loadButtons() {
  227. JPanel panel = new JPanel(new GridLayout(1, 3));
  228. panel.add(buttonLoad);
  229. panel.add(buttonShowPaths);
  230. panel.add(buttonDistance);
  231. mainPanel.add(panel, BorderLayout.SOUTH);
  232. mainPanel.updateUI();
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement