/*** * Prova A2 - Ismael * */ import javax.swing.*; import javax.swing.BorderFactory; import javax.swing.table.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; /* * Classe principal */ public class Matriz extends JFrame { private File arqTemp; private File arqOrigemA; private File arqOrigemB; private File arqResult; public static void main(String[] args) { Matriz frame = new Matriz(); frame.pack(); frame.setVisible(true); } /* * Construtor */ public Matriz() { super("Matriz"); setSize(600, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container cont = getContentPane(); cont.setLayout(new BorderLayout()); final JPanel painel_tables = new JPanel(new GridLayout(1, 3, 10, 10)); DefaultTableModel model = new DefaultTableModel(); final JTable tabA = new JTable(3,3); tabA.setPreferredScrollableViewportSize(new Dimension(200, 70)); JScrollPane scrollPaneA = new JScrollPane(tabA); final JTable tabB = new JTable(3,3); tabB.setPreferredScrollableViewportSize(new Dimension(200, 70)); JScrollPane scrollPaneB = new JScrollPane(tabB); final JTable tabC = new JTable(3,3); tabC.setPreferredScrollableViewportSize(new Dimension(200, 70)); JScrollPane scrollPaneC = new JScrollPane(tabC); painel_tables.add(scrollPaneA); painel_tables.add(scrollPaneB); painel_tables.add(scrollPaneC); cont.add(painel_tables, BorderLayout.CENTER); final JLabel label1 = new JLabel(); JScrollPane scrollPane1 = new JScrollPane(label1); final JLabel label2 = new JLabel(); JScrollPane scrollPane2 = new JScrollPane(label2); final JLabel label3 = new JLabel(); JScrollPane scrollPane3 = new JScrollPane(label3); JButton btLoadA = new JButton("Carrega A"); JButton btLoadB = new JButton("Carrega B"); JButton btCalcula = new JButton("Calcula"); final JButton btSave = new JButton("Save"); btLoadA.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent E) { CarregaMatrizes(arqOrigemA, label1, tabA); btSave.setVisible(false); } }); btLoadB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent E) { CarregaMatrizes(arqOrigemB, label2, tabB); btSave.setVisible(false); } }); btSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent E) { SalvarResultado(arqResult, label3, tabC); } }); btCalcula.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Resultado(tabA, tabB, tabC); btSave.setVisible(true); } }); JPanel painel_botoes = new JPanel(new GridLayout(2, 3, 10, 10)); painel_botoes.add(btLoadA); painel_botoes.add(btLoadB); JPanel p = new JPanel(); p.setBorder(BorderFactory.createEtchedBorder()); p.add(btCalcula); p.add(btSave); btSave.setVisible(false); painel_botoes.add(p); painel_botoes.add(scrollPane1); painel_botoes.add(scrollPane2); painel_botoes.add(scrollPane3); cont.add(painel_botoes, BorderLayout.SOUTH); show(); } public void CarregaMatrizes(File arquivo, JLabel lbl, JTable tab) { arqTemp = ObtemArquivo(); try { if (arqTemp != null && !(arqTemp.getName().equals(""))) { arquivo = arqTemp; double tMatriz[][] = LerArquivo(arquivo); for (int i = 0; i < tMatriz.length; i++) { for (int j = 0; j < tMatriz[0].length; j++) { tab.setValueAt(new Double(tMatriz[i][j]), i, j); } } lbl.setText(arquivo.getAbsolutePath()); JOptionPane.showMessageDialog(this, "Matriz carregada!"); } } catch (Exception E2) { } } public void SalvarResultado(File arquivo, JLabel lbl, JTable tab) { arqTemp = ObtemArquivo(); try { if (arqTemp != null && !(arqTemp.getName().equals(""))) { arquivo = arqTemp; double tMatriz[][] = new double[tab.getRowCount()][tab.getColumnCount()]; for (int i = 0; i < tMatriz.length; i++) { for (int j = 0; j < tMatriz[0].length; j++) { tMatriz[i][j] = ((Double)tab.getValueAt(i, j)).doubleValue(); } } SalvarArquivo(arquivo, tMatriz); lbl.setText(arquivo.getAbsolutePath()); JOptionPane.showMessageDialog(this, "Matriz salva!"); } } catch (Exception E2) { } } public void Resultado(JTable tabA, JTable tabB, JTable tabC) { int i, j, k, nLins, nCols, nInner; double res = 0; double v1, v2; nLins = tabA.getRowCount(); nCols = tabB.getColumnCount(); nInner= tabA.getColumnCount(); // == tabB..getRowCount(); for (i = 0; i < nLins; i++) { for (j = 0; j < nCols; j++) { res = 0; for (k = 0; k < nInner; k++) { v1 = ((Double)tabA.getValueAt(i, k)).doubleValue(); v2 = ((Double)tabB.getValueAt(k, j)).doubleValue(); res += v1 * v2; tabC.setValueAt(new Double(res), i, j); } } } } /* * Le o arquivo passado como argumento e retorna a matriz. */ public double[][] LerArquivo(File arquivo) throws IOException { BufferedReader temp = null; String linha; StringTokenizer token; int nLins, nCols; double[][] tMatriz, tOk; tOk = new double[1][1]; try { temp = new BufferedReader(new FileReader(arquivo.getName())); linha = temp.readLine(); token = new StringTokenizer(linha); nLins = Integer.parseInt(token.nextToken()); nCols = Integer.parseInt(token.nextToken()); tMatriz = new double[nLins][nCols]; for (int i = 0; i < nLins; ++i) { linha = temp.readLine(); token = new StringTokenizer(linha); for (int j = 0; j < nCols; ++j) { tMatriz[i][j] = (new Double(token.nextToken())).doubleValue(); } } return tMatriz; } catch(Exception E ) { JOptionPane.showMessageDialog(null, "falhou"); } finally { if (temp != null) try { temp.close(); } catch(IOException e) {;} } return tOk; } /* * Salva o arquivo passado como argumento */ public void SalvarArquivo(File arquivo, double[][] matrix) throws IOException { int nLins = matrix.length, nCols = matrix[0].length; PrintWriter dest = null; try{ dest = new PrintWriter(new FileWriter(arquivo.getName())); dest.println("" + nLins + " " + nCols); dest.flush(); for( int i = 0; i < nLins; ++i ) { for( int j = 0; j < nCols; ++j ) { dest.print(matrix[i][j] + ((j == nCols-1) ? "" : " ")); } dest.println(""); } dest.flush(); System.out.println(" " + arquivo.getName() + " has been saved !"); } catch( IOException e ) { throw new IOException(); } } /* * Exibe a caixa de escolha e retorna o arquivo escolhido. */ public File ObtemArquivo() { String caminho = "."; int resposta; JFileChooser seletor = new JFileChooser(caminho); seletor.setFileSelectionMode(JFileChooser.FILES_ONLY); resposta = seletor.showOpenDialog(this); if(resposta == JFileChooser.CANCEL_OPTION) return null; else return seletor.getSelectedFile(); } }