Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package br.uefs.ecomp.Game.GraphicInterface;
- import br.uefs.ecomp.Game.facade.GameFacade;
- import br.uefs.ecomp.Game.util.PathlessException;
- import br.uefs.ecomp.Game.util.VertexNotFoundException;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- import javax.swing.filechooser.FileNameExtensionFilter;
- /**
- *
- * @author dcandrade
- */
- public class MainInterface {
- private static GameFacade gf;
- private JFrame windows;
- private JPanel mainPanel;
- JComboBox originCombo;
- JComboBox destinyCombo;
- private JEditorPane allPathsPane;
- private JEditorPane bestPathPane;
- JButton buttonLoad;
- JButton buttonShowPaths;
- JButton buttonDistance;
- public MainInterface() {
- gf = new GameFacade();
- }
- public static void main(String[] args) {
- new MainInterface().createScreen();
- }
- private void createScreen() {
- prepareWindows();
- prepareMainPanel();
- prepareButtonLoad();
- showWindows();
- prepareCombos();
- prepareRouteFields();
- prepareButtonShowPath();
- prepareButtonDistance();
- loadButtons();
- }
- private void prepareWindows() {
- windows = new JFrame("CGoes no Mundo da Fantasia");
- windows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- private void prepareMainPanel() {
- mainPanel = new JPanel();
- BorderLayout bl = new BorderLayout();
- mainPanel.setLayout(new BorderLayout());
- windows.add(mainPanel);
- }
- private void prepareButtonLoad() {
- buttonLoad = new JButton("Carregar arquivo");
- buttonLoad.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setFileFilter(new FileNameExtensionFilter(".txt", "txt"));
- if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
- try {
- gf.importSettingsFile(chooser.getSelectedFile().getAbsolutePath());
- prepareOriginCombo(gf.getSpots());
- prepareDestinyCombo(gf.getSpots());
- JOptionPane.showMessageDialog(null, "Arquivo Carregado");
- } catch (FileNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "Nao foi possivel ler o arquivo");
- }
- }
- }
- });
- }
- private void prepareButtonShowPath() {
- buttonShowPaths = new JButton("Calcular Caminhos");
- buttonShowPaths.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (!originCombo.getSelectedItem().toString().equals("Vazio!")) {
- fillPathFields(originCombo.getSelectedItem().toString(), destinyCombo.getSelectedItem().toString());
- } else {
- JOptionPane.showMessageDialog(null, "Carregue um arquivo primeiro");
- }
- }
- });
- }
- private void prepareCombos() {
- prepareOriginCombo(gf.getSpots());
- prepareDestinyCombo(gf.getSpots());
- JPanel originPanel = new JPanel(new GridLayout(1, 2));
- originPanel.add(new Label("Escolha a origem"));
- originPanel.add(destinyCombo);
- JPanel destinyPanel = new JPanel(new GridLayout(1, 2));
- destinyPanel.add(new Label("Escolha o destino"));
- destinyPanel.add(originCombo);
- JPanel combos = new JPanel(new BorderLayout());
- combos.add(originPanel, BorderLayout.EAST);
- combos.add(destinyPanel, BorderLayout.WEST);
- mainPanel.add(combos, BorderLayout.NORTH);
- mainPanel.updateUI();
- }
- private void prepareOriginCombo(Iterator spots) {
- if (originCombo == null) {
- originCombo = new JComboBox();
- originCombo.addItem("Vazio!");
- originCombo.setSize(70, 30);
- } else {
- originCombo.removeItem("Vazio!");
- }
- while (spots.hasNext()) {
- originCombo.addItem(spots.next().toString());
- }
- }
- private void prepareDestinyCombo(Iterator spots) {
- if (destinyCombo == null) {
- destinyCombo = new JComboBox();
- destinyCombo.addItem("Vazio!");
- destinyCombo.setSize(70, 30);
- destinyCombo.updateUI();
- } else {
- destinyCombo.removeItem("Vazio!");
- }
- while (spots.hasNext()) {
- destinyCombo.addItem(spots.next().toString());
- }
- }
- private void showWindows() {
- windows.pack();
- windows.setSize(540, 540);
- windows.setVisible(true);
- }
- private void prepareRouteFields() {
- allPathsPane = new JEditorPane();
- bestPathPane = new JEditorPane();
- JScrollPane bestPathScroll = new JScrollPane(bestPathPane);
- JScrollPane allPathsScroll = new JScrollPane(allPathsPane);
- JPanel panel = new JPanel(new GridLayout(2, 1));
- JPanel bPPanel = new JPanel(new BorderLayout());
- bPPanel.add(new Label("Melhor Caminho"), BorderLayout.NORTH);
- bPPanel.add(bestPathScroll);
- JPanel aPPanel = new JPanel(new BorderLayout());
- aPPanel.add(new Label("Todos os Caminhos"), BorderLayout.NORTH);
- aPPanel.add(allPathsScroll);
- panel.add(bPPanel);
- panel.add(aPPanel);
- mainPanel.add(panel, BorderLayout.WEST);
- mainPanel.updateUI();
- }
- private void fillPathFields(String a, String b) {
- //Preenchendo todos os caminhos
- try {
- ArrayList<ArrayList<String>> paths = gf.getPossibleRoutes(a, b);
- String aText = "";
- for (int i = 0; i < paths.size(); i++) {
- ArrayList<String> p = paths.get(i);
- aText += "Caminho " + i + 1 + ":\n";
- for (String p1 : p) {
- aText += p1 + "\n";
- }
- aText += "\n";
- }
- allPathsPane.setText(aText);
- } catch (VertexNotFoundException ex) {
- allPathsPane.setText("Não existem caminhos!");
- }
- try {
- //Preenchendo melhor caminho
- ArrayList<String> bestPath = gf.getSmallerPath(a, b);
- String bText = "";
- for (String s : bestPath) {
- bText += s + "\n";
- }
- bestPathPane.setText(bText);
- } catch (PathlessException ex) {
- }
- }
- public void prepareButtonDistance() {
- buttonDistance = new JButton();
- buttonDistance.setText("Calcular distância");
- buttonDistance.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- String origin = originCombo.getSelectedItem().toString();
- String destiny = destinyCombo.getSelectedItem().toString();
- if (!originCombo.getSelectedItem().toString().equals("Vazio!")) {
- double distance = gf.getLinearDistance(origin, destiny);
- JOptionPane.showMessageDialog(null, "A distância euclidiane entre " + origin + " e " + destiny + " é de " + distance);
- } else {
- JOptionPane.showMessageDialog(null, "Carregue um arquivo primeiro");
- }
- } catch (VertexNotFoundException ex) {
- }
- }
- });
- }
- private void loadButtons() {
- JPanel panel = new JPanel(new GridLayout(1, 3));
- panel.add(buttonLoad);
- panel.add(buttonShowPaths);
- panel.add(buttonDistance);
- mainPanel.add(panel, BorderLayout.SOUTH);
- mainPanel.updateUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement