Advertisement
Guest User

Untitled

a guest
Jan 7th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.90 KB | None | 0 0
  1. package fsiap_ui;
  2.  
  3. import fsiap_logic.snell;
  4. import java.awt.BorderLayout;
  5. import java.awt.CardLayout;
  6. import java.awt.Color;
  7. import java.awt.Container;
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import java.awt.GridLayout;
  11. import java.awt.Image;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.BoxLayout;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JButton;
  18. import javax.swing.JFrame;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JOptionPane;
  23. import javax.swing.JPanel;
  24. import javax.swing.JTextArea;
  25. import javax.swing.KeyStroke;
  26. import javax.swing.border.TitledBorder;
  27.  
  28. public class main_ui extends JFrame {
  29.  
  30.     private Container c = getContentPane();
  31.     private JMenuBar menuBar;
  32.     private JMenu menu;
  33.     private JMenuItem menuItem;
  34.     private CardLayout cardLayout;
  35.     private JPanel cardLayers;
  36.     private snell s;
  37.     Line LinesRefraction = new Line(300, 300);
  38.  
  39.     public main_ui() {
  40.         /**
  41.          * Frame definitions*
  42.          */
  43.         super("FSIAP 2013/2014");
  44.         c.removeAll();
  45.         setSize(800, 600);
  46.         setVisible(true);
  47.         setLocationRelativeTo(null);
  48.         setResizable(false);
  49.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  50.         /**/
  51.  
  52.         c.setLayout(new BorderLayout());
  53.  
  54.         cardLayout = new CardLayout();
  55.         cardLayers = new JPanel(cardLayout);
  56.         cardLayers.setOpaque(false);
  57.  
  58.         menu();
  59.         menuEntrada();
  60.         menuSnell();
  61.  
  62.         fundoMain fundo = new fundoMain();
  63.         fundo.setLayout(new GridLayout(1, 1));
  64.         fundo.setOpaque(false);
  65.         fundo.add(cardLayers);
  66.         c.add(fundo, BorderLayout.CENTER);
  67.     }
  68.  
  69.     private void menu() {
  70.  
  71.         menuBar = new JMenuBar();
  72.  
  73.         /**
  74.          * Menu Modulos*
  75.          */
  76.         menu = new JMenu("Módulos");
  77.         menu.setMnemonic('M');
  78.         menuBar.add(menu);
  79.  
  80.         menuItem = new JMenuItem("Ecrã Principal", 'P');
  81.  
  82.         menuItem.addActionListener(new ActionListener() {
  83.             public void actionPerformed(ActionEvent e) {
  84.                 cardLayout.show(cardLayers, "Entrada");
  85.             }
  86.         });
  87.         menu.add(menuItem);
  88.  
  89.         menuItem = new JMenuItem("Lei de Snell", 'N');
  90.         menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
  91.         menuItem.addActionListener(new ActionListener() {
  92.  
  93.             public void actionPerformed(ActionEvent e) {
  94.                 cardLayout.show(cardLayers, "Snell");
  95.             }
  96.         });
  97.         menu.add(menuItem);
  98.         /**/
  99.  
  100.         /**
  101.          * Menu Sobre*
  102.          */
  103.         menu = new JMenu("Sobre");
  104.         menu.setMnemonic('S');
  105.         menuBar.add(menu);
  106.  
  107.         menuItem = new JMenuItem("Autores", 'A');
  108.         menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
  109.         menuItem.addActionListener(new ActionListener() {
  110.  
  111.             public void actionPerformed(ActionEvent e) {
  112.                 JOptionPane.showMessageDialog(main_ui.this, "Programa realizado por\n1090571 - Filipe Tedim\n", "Autores", JOptionPane.INFORMATION_MESSAGE);
  113.             }
  114.         });
  115.         menu.add(menuItem);
  116.         /**/
  117.  
  118.         /**
  119.          * Menu Sair*
  120.          */
  121.         menu = new JMenu("Sair");
  122.         menu.setMnemonic('S');
  123.         menuBar.add(menu);
  124.  
  125.         menuItem = new JMenuItem("Sair", 'S');
  126.         menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
  127.         menuItem.addActionListener(new ActionListener() {
  128.  
  129.             public void actionPerformed(ActionEvent e) {
  130.                 System.exit(0);
  131.             }
  132.         });
  133.         menu.add(menuItem);
  134.         /**/
  135.  
  136.         c.add(menuBar, BorderLayout.NORTH);
  137.     }
  138.  
  139.     private void menuEntrada() {
  140.  
  141.         JPanel pEntrada = new JPanel();
  142.         pEntrada.setOpaque(false);
  143.         cardLayers.add(pEntrada, "Entrada");
  144.  
  145.     }
  146.  
  147.     private void menuSnell() {
  148.  
  149.         // Criação painel
  150.         JPanel pSnell = new JPanel();
  151.         pSnell.setLayout(new BoxLayout(pSnell, BoxLayout.Y_AXIS));
  152.         pSnell.setOpaque(true);
  153.         pSnell.setBackground(Color.red);
  154.         JPanel pInsertValues = new JPanel();
  155.         pInsertValues.setPreferredSize(new Dimension(350, 65));
  156.         pInsertValues.setMaximumSize(new Dimension(350, 65));
  157.         pInsertValues.setOpaque(false);
  158.         pInsertValues.setBorder(BorderFactory.createTitledBorder(null, "Lei de Snell - Inserir Valores", TitledBorder.LEFT, TitledBorder.TOP, null, Color.white));
  159.  
  160.         // Criação das text areas (n1=0  a1=1  n2=2  a2=3)
  161.         final JTextArea inputText[] = new JTextArea[4];
  162.         for (int i = 0; i < 4; i++) {
  163.             inputText[i] = new JTextArea("");
  164.             inputText[i].setPreferredSize(new Dimension(50, 20));
  165.             pInsertValues.add(inputText[i]);
  166.         }
  167.  
  168.         // Criação botaao calcular
  169.         JButton buttonCalcular = new JButton("Calcular");
  170.         buttonCalcular.addActionListener(new ActionListener() {
  171.             @Override
  172.             public void actionPerformed(ActionEvent e) {
  173.  
  174.                 // vector de booleans para saber quais text areas estao preenchidas e booleans para verificar quantos estao preenchidos
  175.                 boolean testBools[] = new boolean[4];
  176.                 boolean checkOnlyOneEmpty = true;
  177.                 boolean checkAllFieldsUsed = false;
  178.  
  179.                 for (int i = 0; i < 4; i++) {
  180.                     testBools[i] = false;
  181.                 }
  182.  
  183.                 for (int i = 0; i < 4; i++) {
  184.                     if (inputText[i].getText().isEmpty() || inputText[i].getText().equals("0")) {
  185.                         inputText[i].setText("0");
  186.                         testBools[i] = true;
  187.                     }
  188.                 }
  189.  
  190.                 for (int i = 0; i < 4; i++) {
  191.                     if (testBools[i] == true) {
  192.                         for (int j = 0; j < 4; j++) {
  193.                             if (j != i && testBools[j] == true) {
  194.                                 checkOnlyOneEmpty = false;
  195.                             }
  196.                         }
  197.                     }
  198.                 }
  199.  
  200.                 if (testBools[0] == testBools[1] == testBools[2] == testBools[3] == false) {
  201.                     checkAllFieldsUsed = true;
  202.                 }
  203.                 if (checkOnlyOneEmpty) {
  204.                     if (checkAllFieldsUsed) {
  205.                         s = new snell(Double.parseDouble(inputText[0].getText()),
  206.                                 Double.parseDouble(inputText[1].getText()),
  207.                                 Double.parseDouble(inputText[2].getText()),
  208.                                 Double.parseDouble(inputText[3].getText()));
  209.  
  210.                         if (testBools[0] == true) {
  211.                             s.calcular_n1();
  212.                             inputText[0].setText(Double.toString(s.getN1()));
  213.                         }
  214.                         if (testBools[1] == true) {
  215.                             s.calcular_a1();
  216.                             inputText[1].setText(Double.toString(s.getA1()));
  217.                         }
  218.                         if (testBools[2] == true) {
  219.                             s.calcular_n2();
  220.                             inputText[2].setText(Double.toString(s.getN2()));
  221.                         }
  222.                         if (testBools[3] == true) {
  223.                             s.calcular_a2();
  224.                             inputText[3].setText(Double.toString(s.getA2()));
  225.                         }
  226.                         // Desenha as linhas de refra e refle
  227.                         LinesRefraction.addLine((int) (Math.tan(Math.toRadians(Double.parseDouble(inputText[1].getText()))) * 125), 25, 150, 150);
  228.                     } else {
  229.                         JOptionPane.showMessageDialog(rootPane, "Erro: Todos os campos foram preenchidos!");
  230.                     }
  231.                 } else {
  232.                     JOptionPane.showMessageDialog(rootPane, "Erro: Não há informação suficiente para calcular!");
  233.                 }
  234.             }
  235.         });
  236.         pInsertValues.add(buttonCalcular);
  237.         pSnell.add(pInsertValues);
  238.  
  239.         // Desenho dos eixos do graph
  240.         LinesRefraction.addLine(150 , 25 , 150 , 275);
  241.         LinesRefraction.addLine(25, 150, 275, 150);
  242.        
  243.         // Desenho indice/angulo incidencia
  244.        
  245.        
  246.         pSnell.add(LinesRefraction);
  247.        
  248.         cardLayers.add(pSnell, "Snell");
  249.     }
  250.  
  251.     private class fundoMain extends JPanel {
  252.  
  253.         @Override
  254.         public void paintComponent(Graphics g) {
  255.             super.paintComponent(g);
  256.             ImageIcon i1 = new ImageIcon("fundoMain.png");
  257.             Image i2 = i1.getImage().getScaledInstance(800, 560, Image.SCALE_SMOOTH);
  258.             Image i3 = new ImageIcon(i2).getImage();
  259.  
  260.             g.drawImage(i3, 0, 0, this);
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement