Share
Explore BrainMass

Programming > Java

Applet program that finds an inverse matrix

Applet program that accepts a 2x2 matrix and finds its inverse matrix if any. It seems easy, but I am having a problem getting it to work.

Solution Preview

Dear my friend
We are so happy that you have choosed Brainmass to get help.
We are so interested to help you and your friend to get what you need.
I have found this code in my files for you, which is completely usefull for any operaion on metices. I hope to be usefull.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MatrixCalculator {

private boolean DEBUG = true;

private boolean INFO = true;

private static int max = 100;

private static int decimals = 3;

private JLabel statusBar;

private JTextArea taA, taB, taC;

private int iDF = 0;

private int n = 4;

private static NumberFormat nf;

public Component createComponents() {

/* == MATRICES == */
taA = new JTextArea();
taB = new JTextArea();
taC = new JTextArea();

JPanel paneMs = new JPanel();
paneMs.setLayout(new BoxLayout(paneMs, BoxLayout.X_AXIS));
paneMs.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
paneMs.add(MatrixPane("Matrix A", taA));
paneMs.add(Box.createRigidArea(new Dimension(10, 0)));
paneMs.add(MatrixPane("Matrix B", taB));
paneMs.add(Box.createRigidArea(new Dimension(10, 0)));
paneMs.add(MatrixPane("Matrix C", taC));

/* == OPERATION BUTTONS == */
JPanel paneBtn = new JPanel();
paneBtn.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
paneBtn.setLayout(new GridLayout(3, 3));
JButton btnApB = new JButton("A + B = C");
JButton btnAmB = new JButton("A * B = C");
JButton btnBmA = new JButton("B * A = C");
JButton btnAdjA = new JButton("adjoint(A) = C");
JButton btnInvA = new JButton("inverse(A) = C");
JButton btnInvB = new JButton("inverse(B) = C");
JButton btnTrnsA = new JButton("transpose(A) = C");
JButton btnDetA = new JButton("determ(A) = C");
JButton btnDetB = new JButton("determ(B) = C");
paneBtn.add(btnApB);
paneBtn.add(btnAmB);
paneBtn.add(btnBmA);
paneBtn.add(btnAdjA);
paneBtn.add(btnInvA);
paneBtn.add(btnInvB);
paneBtn.add(btnTrnsA);
paneBtn.add(btnDetA);
paneBtn.add(btnDetB);

/* == ADD BUTTON Listeners == */
btnApB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
DisplayMatrix(AddMatrix(ReadInMatrix(taA),
ReadInMatrix(taB)), taC);
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
});

btnAmB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
DisplayMatrix(MultiplyMatrix(
ReadInMatrixNotSquare(taA),
ReadInMatrixNotSquare(taB)), taC);
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
});

btnBmA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
DisplayMatrix(MultiplyMatrix(ReadInMatrixNotSquare(taB),
ReadInMatrixNotSquare(taA)), taC);
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
});

btnInvA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
DisplayMatrix(Inverse(ReadInMatrix(taA)), taC);
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
});

btnInvB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try ...

$2.19