martes, 12 de enero de 2010

Trabajo Final pregunta 4

Clase Sensor de Velocidad

import javax.swing.*;

public class Sensor {
private int vel;

public Sensor(){

}

public void setVel(int v) {
vel = v;
}

public int getVel(){
return vel;
}

public void leerVel(){
String aux;
aux = JOptionPane.showInputDialog("Ingrese el valor de la temperatura");
vel = Integer.parseInt(aux);
}

public static void main(String args[]){
Sensor s = new Sensor();
s.leerVel();
System.out.println("Temperatura = "+s.getVel());
}
}

****************************************************************************

Clase Velocimetro
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Interface2 extends JFrame {
public Interface2(){
super("Sistema de monitoreo de temperatura");
setSize(700,700);
show();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.BLUE);
g.drawRect(100, 200, 20, 100);
g.setColor(Color.BLACK);
g.drawString("280 Km/h", 75, 150);
g.drawString("0 Km/h", 75, 400);
g.drawString("Alerta", 140, 90);
g.drawOval(150, 100, 20, 20);

g.setColor(Color.BLACK);
g.drawOval(400, 150, 150, 150);
g.setColor(Color.BLACK);
g.drawString("!!!Alerta!!!", 470, 90);
g.drawOval(470, 100, 20, 20);
g.setColor(Color.PINK);
g.fillOval(400, 150, 150, 150);
g.setColor(Color.BLACK);{
g.drawLine(300, 0, 300, 1000);
g.drawString("Sensor de Velocidad Lineal",60 , 50);
g.drawString("Sensor de Velocidad pastel ", 400, 50);

}

Sensor s = new Sensor();
s.leerVel();
if(s.getVel()>0& s.getVel()<80){

g.setColor(Color.YELLOW);
g.fillArc(400, 150, 150, 150, 210, ((((s.getVel()*100)/280)*360))/100);
g.setColor(Color.PINK);
g.drawString(((s.getVel()*100)/280)+"% de la Vel Max", 430, 220);
g.setColor(Color.GREEN);
g.fillOval(470, 100, 20, 20);
}
if(s.getVel()>=80& s.getVel()<150){
g.setColor(Color.RED);
g.fillArc(400, 150, 150, 150, 210, ((((s.getVel()*100)/280)*360))/100);
g.setColor(Color.WHITE);
g.drawString(((s.getVel()*100)/280)+"% de la Vel Max", 430, 220);
g.setColor(Color.GREEN);
g.fillOval(470, 100, 20, 20);
}
if(s.getVel()>=150& s.getVel()<=280){
g.setColor(Color.RED);
g.fillArc(400, 150, 150, 150, 210, ((((s.getVel()*100)/280)*360))/100);
g.setColor(Color.WHITE);
g.drawString(((s.getVel()*100)/280)+"% de la Vel Max", 430, 220);
g.setColor(Color.RED);
g.fillOval(470, 100, 20, 20);
}


if (s.getVel()>0& s.getVel()<100){

g.setColor(Color.BLUE);
g.fillRect(100, 300-s.getVel(), 20, s.getVel());
g.drawString(s.getVel()+" Km/h", 75, 380-s.getVel());
g.setColor(Color.GREEN);
g.fillOval(150, 100, 20, 20);
}
if(s.getVel()>=100& s.getVel()<180){

g.setColor(Color.YELLOW);
g.fillRect(100, 300-s.getVel(), 20, s.getVel());
g.drawString(s.getVel()+" Km/h", 75, 380-s.getVel());
g.setColor(Color.GREEN);
g.fillOval(150, 100, 20, 20);
}
if(s.getVel()>=180& s.getVel()<=280){
g.setColor(Color.RED);
g.fillRect(100, 300-s.getVel(), 20, s.getVel());
g.drawString(s.getVel()+" Km/h", 75, 380-s.getVel());
g.setColor(Color.RED);
g.fillOval(150, 100, 20, 20);
}
}
public static void main(String args[]){
Interface2 vel = new Interface2();


vel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Trabajo Final pregunta 3

Clase Matriz

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Matriz {
public double[][] value=null;
protected String name=null;

public int numeroFilas;
public int numeroColumnas;
public double [][]matriz;

public Matriz(){
}

public Matriz(int nF, int nC){

numeroFilas=nF;
numeroColumnas=nC;
matriz=new double[numeroFilas][numeroColumnas];
for(int i=0;i < numeroFilas;i++)
for(int j=0; j < numeroColumnas; j++)
matriz [i][j]=0;
}

public Matriz suma(Matriz b){
Matriz resultado;
if((this.numeroFilas == b.numeroFilas)&& (this.numeroColumnas == b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas, this.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
resultado.matriz[i][j] = this.matriz[i][j]+ b.matriz[i][j];
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}

public Matriz resta(Matriz b){
Matriz resultado;
if ((this.numeroFilas == b.numeroFilas)&(this.numeroFilas == b.numeroColumnas)){
resultado = new Matriz (this.numeroFilas,this.numeroColumnas);//construyo la caja donde almaceno el resultado
for(int i = 0;i < this.numeroFilas;i++)
for(int j=0;j < this.numeroColumnas;j++)
resultado.matriz[i][j] = this.matriz[i][j]-b.matriz[i][j];
return resultado;
}

else{
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}}
public Matriz Transpuesta(){
Matriz resultado;
resultado=new Matriz(this.numeroColumnas,this.numeroFilas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
resultado.matriz[j][i]=this.matriz[i][j];
return resultado;
}
public Matriz Multiplicacion(Matriz b){
Matriz resultado;
if(this.numeroColumnas==b.numeroFilas){
resultado=new Matriz(this.numeroFilas, b.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < b.numeroColumnas; j++){
for(int k=0; k < this.numeroColumnas; k++)
resultado.matriz[i][j]+=this.matriz[i][k]*b.matriz[k][j];
}}
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}
public Matriz Inversa(){
Matriz r;
r=new Matriz(this.numeroColumnas,this.numeroFilas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
r=new Matriz(this.numeroColumnas,this.numeroFilas);
r.matriz[0][0]=((this.matriz[1][1]*this.matriz[2][2])-(this.matriz[2][1]*this.matriz[1][2]));
r.matriz[0][1]=((this.matriz[1][0]*this.matriz[2][2])-(this.matriz[2][0]*this.matriz[1][2]));
r.matriz[0][2]=((this.matriz[1][0]*this.matriz[2][1])-(this.matriz[2][0]*this.matriz[1][1]));
r.matriz[1][0]=((this.matriz[0][1]*this.matriz[2][2])-(this.matriz[2][1]*this.matriz[0][2]));
r.matriz[1][1]=((this.matriz[1][0]*this.matriz[2][2])-(this.matriz[2][0]*this.matriz[1][2]));
r.matriz[1][2]=((this.matriz[0][0]*this.matriz[2][1])-(this.matriz[2][0]*this.matriz[0][1]));
r.matriz[2][0]=((this.matriz[0][1]*this.matriz[1][2])-(this.matriz[1][1]*this.matriz[0][2]));
r.matriz[2][1]=((this.matriz[0][0]*this.matriz[1][2])-(this.matriz[1][0]*this.matriz[0][2]));
r.matriz[2][2]=((this.matriz[0][0]*this.matriz[1][1])-(this.matriz[1][0]*this.matriz[0][1]));
return r;
}

public void leer(){
String aux;
for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < this.numeroColumnas; j++){
aux = JOptionPane.showInputDialog(null,"INGRESO DE VALORES","INGRESE EL VALOR: "+(i+1)+","+(j+1),JOptionPane.DEFAULT_OPTION);
this.matriz[i][j]=Double.parseDouble(aux);
}}
}
public double deteminantes(){
double suma=0;
for(int i=0; i for(int j=0; j suma = this.matriz[i][j+1]-this.matriz[i+1][j]));
return suma;
}

public String toString(){

String aux="\n";
DecimalFormat df = new DecimalFormat("0.000");
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux+=df.format(matriz[i][j])+" ";
}
aux+="\n";
}
aux+=" ";
return aux;
}
}

public class OperacionesMatrices extends javax.swing.JApplet {

/** Initializes the applet OperacionesMatrices */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jTextField6 = new javax.swing.JTextField();
jButton6 = new javax.swing.JButton();

jLabel1.setText("Operaciones de Matrices");

jLabel2.setText("Operaciones Binarias");

jLabel3.setText("Matriz 1");

jLabel4.setText("Matriz 2");

jLabel5.setText("Numero de Filas");

jLabel6.setText("Numero de Columnas");

jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});

jLabel7.setText("Numero Filas");

jLabel8.setText("Numero Columnas");

jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField3ActionPerformed(evt);
}
});

jButton1.setText("Suma");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Resta");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Multiplicación");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton4.setText("Transpuesta");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText("Inversa");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);

jLabel9.setText("Matriz");

jLabel10.setText("Numero de Filas");

jLabel11.setText("Numero de columnas");

jTextField5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField5ActionPerformed(evt);
}
});

jButton6.setText("Determinante");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(jButton3)
.addContainerGap(880, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addContainerGap(1045, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(345, 345, 345)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(672, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel6)
.addComponent(jLabel5)
.addComponent(jLabel8)
.addComponent(jLabel7)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField4)
.addComponent(jTextField3)
.addComponent(jTextField2)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel9)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4)
.addComponent(jLabel10)
.addComponent(jLabel11))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton5)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField6, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)))))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(128, 128, 128)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6)))))
.addGap(492, 492, 492))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(57, 57, 57)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(45, 45, 45)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7))
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(60, 60, 60)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton5)
.addComponent(jButton6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel11)
.addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(113, 113, 113))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
.addContainerGap())))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(20, 20, 20))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}//

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aux = jTextField1.getText();
String aux1="";
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux1 += m1.toString();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
aux1 += m2.toString();
jTextArea1.setText("Suma de Matrices: \n"+aux1+(m1.suma(m2)).toString());
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String aux = jTextField1.getText();
String aux1="";
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux1 += m1.toString();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
aux1 += m2.toString();
jTextArea1.setText("Resta de Matrices: \n"+aux1+(m1.resta(m2)).toString());
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String aux = jTextField1.getText();
String aux1="";
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux1 += m1.toString();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
aux1 += m2.toString();
jTextArea1.setText("Multiplicacion de Matrices: \n"+aux1+(m1.Multiplicacion(m2)).toString());
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String aux = jTextField5.getText();
String aux1="";
int nF = Integer.parseInt(aux);
aux = jTextField6.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux1+= m1.toString();

jTextArea2.setText("Transpuesta de Matrices: \n"+aux1+(m1.Transpuesta()).toString());

}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

String aux = jTextField5.getText();
int nF = Integer.parseInt(aux);
aux = jTextField6.getText();
int nC = Integer.parseInt(aux);
m1= new Matriz(nF,nC);
m1.leer();
jTextArea2.setText("\nMATRIZ: \n"+(m1.toString())+"INVERSA DE MATRICES: \n"+(m1.Inversa().toString()));
}

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {

}

public Matriz m1;
public Matriz m2;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
}

Libreria AWT ,SWIT y Applet

Universidad Tecnológica Equinoccial

Trabajo Final de Programación II

1 Describa las características principales de la librería AWT, SWING.

Librería AWT

Se trata de una biblioteca de clases Java para el desarrollo de Interfaces de usuario Gráficas las cuales permiten generar componentes gráficos y gestionar eventos.Su aspecto visual depende de la plataforma.

Caracteristicas:

1. Genera interfaces gráficas, independiente de la plataforma en que se ejecute la aplicación.
2. Los Contenedores contienen a los controles básicos
3. Depende de la máquina en que se ejecuta la aplicación
4. No tiene formato de recursos sin poder separar el código de la interface.
5. No se usan posiciones fijas están situados a través de una disposición controlada (layouts)
6. Alto nivel de abstracción al entorno de ventanas en que se ejecute la aplicación
7. La arquitectura de la aplicación depende del entorno de ventanas, no tiene un tamaño fijo

Estructura

Container: Contenedor.
Label: Etiqueta de texto
Button: Botón de comando
Canvas: Area de dibujo.
Choice: Lista de selección.
List: Lista de datos.
Scrollbar: Barra de desplazamiento.
TextComponent: Componente de texto.
Container: Contienen en su interior al resto de componentes
Button: Botón de comando
Canvas: área de dibujo.
Checkbox: Caja de comprobación.
CheckboxGroup: Grupo de cajas de chequeo.

Librería Swing

Proporciona componentes de presentación visual independiente a la plataforma en la que se ejecuta. Swing extiende AWT y añade nuevas características, mejoras y componentes para interactuar con el usuario, tales como árboles, pestañas, tablas, etc.
Existen muchas versiones de swing en el mercado

Caracteristicas

1.Cambia la gestión del texto creando texto de colores con distintos tipos de caracteres.
2. Se puede introducir imágenes y decodificar los archivos de html, rtf.
3. Gestionar sucesos con el paquete de java.awt.event.

*************************************************************************************

2 Defina un applet, describa su funcionamiento

APPLTET:

Un applet es un componente de una aplicación que se ejecuta en el contexto de otro programa, por ejemplo un navegador web. El applet debe ejecutarse en un contenedor, que lo proporciona un programa anfitrión com aplicaciones como teléfonos móviles que soportan el modelo de programación por applets.

Un applet no puede ejecutarse de manera independiente, ofrece información gráfica y interactúa con el usuario. Un applet lleva a cabo una función muy específica que carece de uso independiente.

Un Java applet es un código JAVA que carece de un método main ya que es un pequeño programa que es utilizado en una página HTML y representado por una pequeña pantalla gráfica.


FUNCIONAMIENTO DEL APPLET

El fichero de código fuente puede ser escrito meidante cualquier editor convencional o con el editor suministrado con el paquete del lenguaje Java.

Una vez creado el fichero java con el código del programa, se compila, generándose un fichero intermedio con los bytecodes, de extensión .class.

Una vez generado el fichero .class, éste ya puede ser interpretado en cualquier máquina virtual de Java. Tratándose de applets, este fichero se descargará típicamente desde la Red. Para indicárselo al navegador se utilizan unas etiquetas especiales del lenguaje HTML:
Ejemplo:


applet_parameters
alternate_content



El navegador carga las clases especificadas en la etiqueta < applet > dinámicamente, a medida que van siendo necesitadas y se les pasa al código fuente el verificador de código de bytes.

Ahora será ya la máquina virtual de Java la que vaya interpretando los bytecodes y generando las instrucciones para su propia arquitectura.

EJEMPLO:
Los ejemplos mas comunes de applets son las Java applets y las animaciones Flash. Otro ejemplo es el Windows Media Player utilizado para desplegar archivos de video incrustados en los navegadores como el Internet Explorer. Otros plugins permiten mostrar modelos 3D que funcionan con una applet.

domingo, 10 de enero de 2010

Sensor de Temperatura

De: Carolina Navarrete


Clase Sensor


import javax.swing.*;

public class Sensor {
private int temp;
public Sensor(){
}
public void setTemp(int t) {
temp = t;
}
public int getTemp(){
return temp;
}
public void leerTemp(){
String aux;
aux = JOptionPane.showInputDialog("Ingrese el valor de la temperatura");
temp = Integer.parseInt(aux);
}
public static void main(String args[]){
Sensor s = new Sensor();
s.leerTemp();
System.out.println("Temperatura = "+s.getTemp());
}
}
------------------------------------------------------------------------------------

Clase interface2

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Interface2 extends JFrame {

public Interface2(){
super("Sistema de monitoreo de temperatura");
setSize(400,400);
show();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.BLUE);
g.drawOval(100, 150, 100, 100);
g.setColor(Color.BLACK);
g.drawString("0 ºC", 200, 200);
g.drawString("360ºC", 200, 210);
g.drawString("Alerta", 150, 70);
g.drawOval(150, 100, 20, 20);
Sensor s = new Sensor();
s.leerTemp();
if (s.getTemp() > 0 & s.getTemp() < = 180){
g.setColor(Color.BLUE);
g.fillArc(100, 150, 100,100,360, s.getTemp());
g.drawString(s.getTemp() + " ºC", 150, 90);
g.setColor(Color.GREEN);
g.fillOval(150, 100, 20, 20);
}
if(s.getTemp() > = 180 & s.getTemp() < 270){
g.setColor(Color.PINK);
g.fillArc(100,150,100,100,360, s.getTemp());
g.drawString(s.getTemp() + " ºC",150,90);
g.setColor(Color.GREEN);
g.fillOval(150, 100, 20, 20);
}
if(s.getTemp() > = 270 & s.getTemp() < = 360){
g.setColor(Color.GREEN);
g.fillArc(100,150,100,100,360, s.getTemp());
g.drawString(s.getTemp()+" ºC", 150,90);
g.setColor(Color.RED);
g.fillOval(150, 100, 20, 20);
}
}
public static void main(String args[]){
Interface2 in = new Interface2();
in.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Resultados:
Caso 1:

lunes, 14 de diciembre de 2009

Calculadora

Clase Operaciones

public class Operaciones {
static double resultado;
public Operaciones(){
resultado = 0;
}

public static double seno(double x){
resultado = Math.sin(x);
return resultado;
}

public static double coseno(double x){
resultado = Math.cos(x);
return resultado;
}

public static double tangente(double x){
resultado = Math.tan(x);
return resultado;
}

public static double raiz(double x){
if(x > = 0){
resultado = Math.sqrt(x);
}else{
System.out.println("Solo valores postivos");
resultado = 0;
}
return resultado;

}
public static double logaritmo (double x){
resultado = Math.log(x);
return resultado;
}

public static double suma (double x,double y){
resultado = x+y;
return resultado;
}

public static double resta (double x,double y){
resultado = x-y;
return resultado;
}

public static double multiplicacion (double x,double y){
resultado = x*y;
return resultado;
}

public static double division (double x,double y){
resultado = x/y;
return resultado;
}
public static double exponente (double x,double y){
resultado = Math.pow(x, y);
return resultado;
}

public static void main(String args[]){
double y;
//Operaciones p = new Operaciones();
y = seno(3.141516);
System.out.println("El seno "+y);
}

}

------------------------------------------------------------
APPLET
public class OperacionesCalculadora extends javax.swing.JApplet {
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}});
} catch (Exception ex) {
ex.printStackTrace();
}}

@SuppressWarnings("unchecked")
//
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();
jButton19 = new javax.swing.JButton();
jButton20 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextField1 = new javax.swing.JTextField();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();

jButton1.setBackground(new java.awt.Color(204, 0, 204));
jButton1.setText("seno");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("coseno");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("tangente");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("raiz");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText("logaritmo");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setText("1");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jButton7.setText("4");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

jButton8.setText("2");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});

jButton9.setText("3");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});

jButton10.setText("5");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});

jButton11.setText("6");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});

jButton12.setText("7");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});

jButton13.setText("8");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});

jButton14.setText("9");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});

jButton15.setText("0");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});

jButton16.setText("+");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});

jButton17.setText("-");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});

jButton18.setText("^");
jButton18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton18ActionPerformed(evt);
}
});

jButton19.setText("*");
jButton19.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton19ActionPerformed(evt);
}
});

jButton20.setText("/");
jButton20.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton20ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton21.setText("=");
jButton21.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton21ActionPerformed(evt);
}
});

jButton22.setText("borrar");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton6))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton12)
.addComponent(jButton21))))
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton9))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton11))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton13)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton14))
.addComponent(jButton15)
.addComponent(jButton22))
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton17, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton18, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton19, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton16)
.addComponent(jButton6)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton17)
.addComponent(jButton7)
.addComponent(jButton10)
.addComponent(jButton11)
.addComponent(jButton2))
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton18)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton19)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton20))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton12)
.addComponent(jButton13)
.addComponent(jButton14))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5))
.addComponent(jButton15))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton21)
.addComponent(jButton22))
.addGap(140, 140, 140))))
);
}//

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
r = Operaciones.coseno(r);
jTextArea1.setText("El coseno es :\n "+r);
aux = "";
jTextField1.setText(aux);
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
r = Operaciones.seno(r);
jTextArea1.setText("El seno es :\n "+r);
aux = "";
jTextField1.setText(aux);
}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 1;
jTextField1.setText(aux);
}

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
aux += 2;
jTextField1.setText(aux);
}

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 3;
jTextField1.setText(aux);
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 4;
jTextField1.setText(aux);
}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 5;
jTextField1.setText(aux);
}

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 6;
jTextField1.setText(aux);
}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 7;
jTextField1.setText(aux);
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 8;
jTextField1.setText(aux);
}

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 9;
jTextField1.setText(aux);
}

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
aux += 0;
jTextField1.setText(aux);
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
r = Operaciones.tangente(r);
jTextArea1.setText("La tangente es :\n "+r);
aux = "";
jTextField1.setText(aux);
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
r = Operaciones.raiz(r);
jTextArea1.setText("La raiz es :\n "+r);
aux = "";
jTextField1.setText(aux);
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
r = Operaciones.logaritmo(r);
jTextArea1.setText("El logaritmo es :\n "+r);
aux = "";
jTextField1.setText(aux);
}

private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
x = Double.parseDouble(aux1);
aux2 = "+";
aux="";
jTextField1.setText("");
}

private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
x = Double.parseDouble(aux1);
aux2 = "-";
aux="";
jTextField1.setText("");
}

private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
x = Double.parseDouble(aux1);
aux2 = "^";
aux="";
jTextField1.setText("");
}

private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
x = Double.parseDouble(aux1);
aux2 = "*";
aux="";
jTextField1.setText("");
}

private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
String aux1 = jTextField1.getText();
x = Double.parseDouble(aux1);
aux2 = "/";
aux="";
jTextField1.setText("");
}

private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
if(aux2 == "+"){
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
jTextArea1.setText("La suma es: \n" +Operaciones.suma(x, r));
}

if(aux2 == "-"){
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
jTextArea1.setText("La resta es:\n" +Operaciones.resta(x,r));
}

if(aux2 == "^"){
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
jTextArea1.setText("La potencia es:\n" +Operaciones.exponente(x, r));
}

if(aux2 == "*"){
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
jTextArea1.setText("La multiplicacion es:\n" +Operaciones.multiplicacion(x, r));
}

if(aux2 == "/"){
String aux1 = jTextField1.getText();
double r = Double.parseDouble(aux1);
jTextArea1.setText("La division es:\n" +Operaciones.division(x,r));
}
}

private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
aux="";
jTextField1.setText("");
}

public String aux = "";
public String aux2 = "";
double x;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton19;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton20;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

}

RESULTADO:

domingo, 6 de diciembre de 2009

Operaciones con Matrices

Resultados:





CLASE MATRIZ

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Matriz {
public int numeroFilas;
public int numeroColumnas;
public double [][]matriz;

public Matriz(){
}
public Matriz(int nF, int nC){

numeroFilas=nF;
numeroColumnas=nC;
matriz=new double[numeroFilas][numeroColumnas];//construyo un sitio para almacenar ceros
for(int i=0;i < numeroFilas;i++)
for(int j=0; j < numeroColumnas; j++)
matriz [i][j]=0;
}
public Matriz suma(Matriz b){
Matriz resultado;
if((this.numeroFilas == b.numeroFilas)&& (this.numeroColumnas == b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas, this.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
resultado.matriz[i][j] = this.matriz[i][j]+ b.matriz[i][j];
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}
public Matriz resta(Matriz b){
Matriz resultado;
if ((this.numeroFilas == b.numeroFilas)&(this.numeroFilas == b.numeroColumnas)){
resultado = new Matriz (this.numeroFilas,this.numeroColumnas);//construyo la caja donde almaceno el resultado
for(int i = 0;i < this.numeroFilas;i++)
for(int j=0;j < this.numeroColumnas;j++)
resultado.matriz[i][j] = this.matriz[i][j]-b.matriz[i][j];
return resultado;
}

else{
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}
}

//el numero de filas se cambia al numero de columnas
public Matriz Transpuesta(){
Matriz resultado;
resultado=new Matriz(this.numeroColumnas,this.numeroFilas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
resultado.matriz[j][i]= this.matriz[i][j];
return resultado;
}
public Matriz Multiplicacion(Matriz b){
Matriz resultado;
if(this.numeroColumnas==b.numeroFilas){
resultado=new Matriz(this.numeroFilas, b.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < b.numeroColumnas; j++){
for(int k=0; k < this.numeroColumnas; k++)
resultado.matriz[i][j]+=this.matriz[i][k]*b.matriz[k][j];
}
}
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}
public Matriz Inversa(){
Matriz r;
r=new Matriz(this.numeroColumnas,this.numeroFilas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
r=new Matriz(this.numeroColumnas,this.numeroFilas);
r.matriz[0][0]=((this.matriz[1][1]*this.matriz[2][2])-(this.matriz[2][1]*this.matriz[1][2]));
r.matriz[0][1]=((this.matriz[1][0]*this.matriz[2][2])-(this.matriz[2][0]*this.matriz[1][2]));
r.matriz[0][2]=((this.matriz[1][0]*this.matriz[2][1])-(this.matriz[2][0]*this.matriz[1][1]));
r.matriz[1][0]=((this.matriz[0][1]*this.matriz[2][2])-(this.matriz[2][1]*this.matriz[0][2]));
r.matriz[1][1]=((this.matriz[1][0]*this.matriz[2][2])-(this.matriz[2][0]*this.matriz[1][2]));
r.matriz[1][2]=((this.matriz[0][0]*this.matriz[2][1])-(this.matriz[2][0]*this.matriz[0][1]));
r.matriz[2][0]=((this.matriz[0][1]*this.matriz[1][2])-(this.matriz[1][1]*this.matriz[0][2]));
r.matriz[2][1]=((this.matriz[0][0]*this.matriz[1][2])-(this.matriz[1][0]*this.matriz[0][2]));
r.matriz[2][2]=((this.matriz[0][0]*this.matriz[1][1])-(this.matriz[1][0]*this.matriz[0][1]));
return r;
}


public void leer(){
String aux;
for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < this.numeroColumnas; j++){
aux = JOptionPane.showInputDialog(null,"INGRESO DE VALORES","INGRESE EL VALOR: "+(i+1)+","+(j+1),JOptionPane.DEFAULT_OPTION);
this.matriz[i][j]=Double.parseDouble(aux);
}
}
}
public String toString(){

String aux="\n";
DecimalFormat df = new DecimalFormat("0.0000");
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux+=df.format(matriz[i][j])+" ";
}
aux+="\n";
}
aux+=" ";
return aux;
}
}
APPLET


public class TodasOperaciones extends javax.swing.JApplet {

/** Initializes the applet TodasOperaciones */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jScrollPane3 = new javax.swing.JScrollPane();
jTextArea3 = new javax.swing.JTextArea();
Multiplicacion = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();

jPanel1.setBackground(new java.awt.Color(204, 255, 255));

jLabel1.setForeground(new java.awt.Color(255, 51, 51));
jLabel1.setText("Operaciones Matrices");

jLabel2.setText("Ingrese las Matrices");

jLabel3.setText("Matriz 1");

jLabel4.setText("Numero de Filas");

jLabel5.setText("Numero de Columnas");

jLabel6.setText("Matriz 2");

jLabel7.setText("Numero de Filas");

jLabel8.setText("Numero de Columnas");

jButton1.setText("Leer Matriz 1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Imprimir Matriz 1");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);

jButton3.setBackground(new java.awt.Color(255, 204, 255));
jButton3.setText("Leer Matriz 2");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText("Suma");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setBackground(new java.awt.Color(255, 204, 255));
jButton5.setText("Imprimir Matriz 2");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setText("Resta");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jTextArea3.setColumns(20);
jTextArea3.setRows(5);
jScrollPane3.setViewportView(jTextArea3);

Multiplicacion.setText("Multiplicacion");
Multiplicacion.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MultiplicacionActionPerformed(evt);
}
});

jButton8.setText("M. Transpuesta");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(305, 305, 305)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(98, 98, 98)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(39, 39, 39)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField2)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(191, 191, 191)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton6)
.addComponent(jButton4))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton8)
.addComponent(Multiplicacion))
.addGap(86, 86, 86)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(jLabel8))
.addGap(42, 42, 42)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)))
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(35, 35, 35)
.addComponent(jButton5)))))
.addContainerGap(2029, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(26, 26, 26)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(16, 16, 16)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(13, 13, 13)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(Multiplicacion))
.addGap(30, 30, 30)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton8)
.addComponent(jButton6)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(55, 55, 55)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(125, 125, 125))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 571, Short.MAX_VALUE)
.addContainerGap())
);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String aux = jTextField1.getText();
nF = Integer.parseInt(aux);
aux = jTextField2.getText();
nC = Integer.parseInt(aux);
m = new Matriz(nF,nC);
m.leer();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea2.setText(m.toString());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
s = new Matriz(nF,nC);
s.leer();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
E = new Matriz(nF,nC);
E = m.suma(s);
jTextArea1.setText(E.toString());
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea3.setText(s.toString());
}
private void MultiplicacionActionPerformed(java.awt.event.ActionEvent evt) {
B = new Matriz(nF,nC);
B = m.Multiplicacion(s);
jTextArea1.setText(B.toString());
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {

}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {

A = new Matriz(nF,nC);
A = m.resta(s);
jTextArea1.setText(A.toString());
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

String aux = jTextField5.getText();
int nF = Integer.parseInt(aux);
aux = jTextField6.getText();
int nC = Integer.parseInt(aux);
m3= new Matriz(nF,nC);
m3.leerMatriz();
jTextArea2.setText("\nMATRIZ: \n"+(m3.toString())+"INVERSA DE MATRICES: \n"+(m3.Inversa().toString()));
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

String aux = jTextField5.getText();
int nF = Integer.parseInt(aux);
aux = jTextField6.getText();
int nC = Integer.parseInt(aux);
m3= new Matriz(nF,nC);
m3.leerMatriz();

jTextArea2.setText("\nMATRIZ: \n"+(m3.toString())+"TRANSPUESTA DE MATRIZ: \n"+(m3.Transpuesta().toString()));
}


public int nF;
public int nC;
public int m3;
public Matriz m;
public Matriz s;
public Matriz E;
public Matriz A;
public Matriz B;

// Variables declaration - do not modify
private javax.swing.JButton Multiplicacion;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton8;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JTextArea jTextArea3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}

martes, 1 de diciembre de 2009

Applet Suma de Matrices



Clase Matriz

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Matriz {
public int numeroFilas;
public int numeroColumnas;
public double [][]matriz;

public Matriz(){
}
public Matriz(int nF, int nC){
numeroFilas=nF;
numeroColumnas=nC;
matriz=new double[numeroFilas][numeroColumnas];//construyo un sitio para almacenar ceros
for(int i=0;i < numeroFilas;i++)
for(int j=0; j < numeroColumnas; j++)
matriz [i][j]=0;
}
public Matriz suma(Matriz b){
Matriz resultado;
if((this.numeroFilas == b.numeroFilas)&& (this.numeroColumnas == b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas, this.numeroColumnas);
for(int i=0; i < this.numeroFilas; i++)
for(int j=0; j < this.numeroColumnas; j++)
resultado.matriz[i][j] = this.matriz[i][j]+ b.matriz[i][j];
return resultado;
}
else
System.out.println("ERROR EN DIMENSIONES DE LAS MATRICES");
resultado=null;
return resultado;
}
public void leer(){
String aux;
for(int i=0; i < this.numeroFilas; i++){
for(int j=0; j < this.numeroColumnas; j++){
aux = JOptionPane.showInputDialog(null,"INGRESO DE VALORES","INGRESE EL VALOR: "+(i+1)+","+(j+1),JOptionPane.DEFAULT_OPTION);
this.matriz[i][j]=Double.parseDouble(aux);
}
}
}
public String toString(){
String aux="\n";
DecimalFormat df = new DecimalFormat("0.0000");
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux+=df.format(matriz[i][j])+" ";
}
aux+="\n";
}
aux+=" ";
return aux;
}
}

APPLET
public class OperacionesMatrices extends javax.swing.JApplet {

/** Initializes the applet OperacionesMatrices */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();

jPanel1.setBackground(new java.awt.Color(204, 255, 204));

jLabel1.setForeground(new java.awt.Color(255, 51, 51));
jLabel1.setText("Operaciones de Matrices");

jLabel2.setForeground(new java.awt.Color(255, 153, 255));
jLabel2.setText("Suma de Matrices");

jLabel3.setText("Matriz 1");

jLabel4.setText("Matriz 2");

jLabel5.setText("Numero de Filas");

jLabel6.setText("Numero de Columnas");

jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

jTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2ActionPerformed(evt);
}
});

jLabel7.setText("Numero Filas");

jLabel8.setText("Numero Columnas");

jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField3ActionPerformed(evt);
}
});

jButton1.setBackground(new java.awt.Color(204, 204, 255));
jButton1.setText("Suma");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(65, 65, 65)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField2)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
.addGap(58, 58, 58)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(jLabel8)
.addComponent(jLabel4)))
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField3)
.addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)))
.addComponent(jLabel2)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(jButton1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(203, 203, 203)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(164, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jLabel2))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(43, 43, 43)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(23, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}//


private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aux = jTextField1.getText();
String aux1="";
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux1 += m1.toString();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
aux1 += m2.toString();
jTextArea1.setText("Suma de Matrices: \n Matriz 1 , Matriz 2"+aux1+(m1.suma(m2)).toString());
}

public Matriz m1;
public Matriz m2;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration

}
RESULTADOS: