Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fsiap_ui;
- import fsiap_logic.snell;
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.BoxLayout;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.KeyStroke;
- import javax.swing.border.TitledBorder;
- public class main_ui extends JFrame {
- private Container c = getContentPane();
- private JMenuBar menuBar;
- private JMenu menu;
- private JMenuItem menuItem;
- private CardLayout cardLayout;
- private JPanel cardLayers;
- private snell s;
- Line LinesRefraction = new Line(300, 300);
- public main_ui() {
- /**
- * Frame definitions*
- */
- super("FSIAP 2013/2014");
- c.removeAll();
- setSize(800, 600);
- setVisible(true);
- setLocationRelativeTo(null);
- setResizable(false);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- /**/
- c.setLayout(new BorderLayout());
- cardLayout = new CardLayout();
- cardLayers = new JPanel(cardLayout);
- cardLayers.setOpaque(false);
- menu();
- menuEntrada();
- menuSnell();
- fundoMain fundo = new fundoMain();
- fundo.setLayout(new GridLayout(1, 1));
- fundo.setOpaque(false);
- fundo.add(cardLayers);
- c.add(fundo, BorderLayout.CENTER);
- }
- private void menu() {
- menuBar = new JMenuBar();
- /**
- * Menu Modulos*
- */
- menu = new JMenu("Módulos");
- menu.setMnemonic('M');
- menuBar.add(menu);
- menuItem = new JMenuItem("Ecrã Principal", 'P');
- menuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- cardLayout.show(cardLayers, "Entrada");
- }
- });
- menu.add(menuItem);
- menuItem = new JMenuItem("Lei de Snell", 'N');
- menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl N"));
- menuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- cardLayout.show(cardLayers, "Snell");
- }
- });
- menu.add(menuItem);
- /**/
- /**
- * Menu Sobre*
- */
- menu = new JMenu("Sobre");
- menu.setMnemonic('S');
- menuBar.add(menu);
- menuItem = new JMenuItem("Autores", 'A');
- menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
- menuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JOptionPane.showMessageDialog(main_ui.this, "Programa realizado por\n1090571 - Filipe Tedim\n", "Autores", JOptionPane.INFORMATION_MESSAGE);
- }
- });
- menu.add(menuItem);
- /**/
- /**
- * Menu Sair*
- */
- menu = new JMenu("Sair");
- menu.setMnemonic('S');
- menuBar.add(menu);
- menuItem = new JMenuItem("Sair", 'S');
- menuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
- menuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- menu.add(menuItem);
- /**/
- c.add(menuBar, BorderLayout.NORTH);
- }
- private void menuEntrada() {
- JPanel pEntrada = new JPanel();
- pEntrada.setOpaque(false);
- cardLayers.add(pEntrada, "Entrada");
- }
- private void menuSnell() {
- // Criação painel
- JPanel pSnell = new JPanel();
- pSnell.setLayout(new BoxLayout(pSnell, BoxLayout.Y_AXIS));
- pSnell.setOpaque(true);
- pSnell.setBackground(Color.red);
- JPanel pInsertValues = new JPanel();
- pInsertValues.setPreferredSize(new Dimension(350, 65));
- pInsertValues.setMaximumSize(new Dimension(350, 65));
- pInsertValues.setOpaque(false);
- pInsertValues.setBorder(BorderFactory.createTitledBorder(null, "Lei de Snell - Inserir Valores", TitledBorder.LEFT, TitledBorder.TOP, null, Color.white));
- // Criação das text areas (n1=0 a1=1 n2=2 a2=3)
- final JTextArea inputText[] = new JTextArea[4];
- for (int i = 0; i < 4; i++) {
- inputText[i] = new JTextArea("");
- inputText[i].setPreferredSize(new Dimension(50, 20));
- pInsertValues.add(inputText[i]);
- }
- // Criação botaao calcular
- JButton buttonCalcular = new JButton("Calcular");
- buttonCalcular.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // vector de booleans para saber quais text areas estao preenchidas e booleans para verificar quantos estao preenchidos
- boolean testBools[] = new boolean[4];
- boolean checkOnlyOneEmpty = true;
- boolean checkAllFieldsUsed = false;
- for (int i = 0; i < 4; i++) {
- testBools[i] = false;
- }
- for (int i = 0; i < 4; i++) {
- if (inputText[i].getText().isEmpty() || inputText[i].getText().equals("0")) {
- inputText[i].setText("0");
- testBools[i] = true;
- }
- }
- for (int i = 0; i < 4; i++) {
- if (testBools[i] == true) {
- for (int j = 0; j < 4; j++) {
- if (j != i && testBools[j] == true) {
- checkOnlyOneEmpty = false;
- }
- }
- }
- }
- if (testBools[0] == testBools[1] == testBools[2] == testBools[3] == false) {
- checkAllFieldsUsed = true;
- }
- if (checkOnlyOneEmpty) {
- if (checkAllFieldsUsed) {
- s = new snell(Double.parseDouble(inputText[0].getText()),
- Double.parseDouble(inputText[1].getText()),
- Double.parseDouble(inputText[2].getText()),
- Double.parseDouble(inputText[3].getText()));
- if (testBools[0] == true) {
- s.calcular_n1();
- inputText[0].setText(Double.toString(s.getN1()));
- }
- if (testBools[1] == true) {
- s.calcular_a1();
- inputText[1].setText(Double.toString(s.getA1()));
- }
- if (testBools[2] == true) {
- s.calcular_n2();
- inputText[2].setText(Double.toString(s.getN2()));
- }
- if (testBools[3] == true) {
- s.calcular_a2();
- inputText[3].setText(Double.toString(s.getA2()));
- }
- // Desenha as linhas de refra e refle
- LinesRefraction.addLine((int) (Math.tan(Math.toRadians(Double.parseDouble(inputText[1].getText()))) * 125), 25, 150, 150);
- } else {
- JOptionPane.showMessageDialog(rootPane, "Erro: Todos os campos foram preenchidos!");
- }
- } else {
- JOptionPane.showMessageDialog(rootPane, "Erro: Não há informação suficiente para calcular!");
- }
- }
- });
- pInsertValues.add(buttonCalcular);
- pSnell.add(pInsertValues);
- // Desenho dos eixos do graph
- LinesRefraction.addLine(150 , 25 , 150 , 275);
- LinesRefraction.addLine(25, 150, 275, 150);
- // Desenho indice/angulo incidencia
- pSnell.add(LinesRefraction);
- cardLayers.add(pSnell, "Snell");
- }
- private class fundoMain extends JPanel {
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- ImageIcon i1 = new ImageIcon("fundoMain.png");
- Image i2 = i1.getImage().getScaledInstance(800, 560, Image.SCALE_SMOOTH);
- Image i3 = new ImageIcon(i2).getImage();
- g.drawImage(i3, 0, 0, this);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement