martes, 12 de enero de 2010

Caracteristicas awt y swing




• Son clases que permiten generar entornos con componentes gráficos comunes a todas las plataformas, y gestionar eventos de teclado y ratón, entre otros. El aspecto visual depende de la plataforma.
• Se trata de una biblioteca de clases Java para el desarrollo de Interfaces de Usuario Gráficas
Caracteristicas:
Genera interfaces gráficas, independientemente de la plataforma en que se ejecute la aplicación.
• Los Contenedores contienen Componentes, que son los controles básicos
• No se usan posiciones fijas de los Componentes, sino que están situados a través de una disposición controlada (layouts)
• El común denominador de más bajo nivel se acerca al teclado, ratón y manejo de eventos
• Alto nivel de abstracción respecto al entorno de ventanas en que se ejecute la aplicación (no hay áreas cliente, ni llamadas a X, ni hWnds, etc.)
• La arquitectura de la aplicación es dependiente del entorno de ventanas, en vez de tener un tamaño fijo
• Es bastante dependiente de la máquina en que se ejecuta la aplicación (no puede asumir que un diálogo tendrá el mismo tamaño en cada máquina)
• Carece de un formato de recursos. No se puede separar el código de lo que es propiamente interface. No hay ningún diseñador de interfaces (todavía)

Estructura básica
Container, se encargan de contener en su interior al resto de componentes e, incluso, a otros contenedores.
Component, define el comportamiento de los componentes implementando métodos de gestión de eventos, y también la presentación de los mismos mediante la definición de sus tamaños, posiciones, colores y fuentes entre otras.
Subclases directas
Button: Botón de comando o de pulsación.
Canvas: Lienzo o área de dibujo.
Checkbox: Caja de chequeo o de comprobación.
CheckboxGroup: Grupo de cajas de chequeo.
Choice: Lista de desplegable o de selección.
Container: Contenedor.
Label: Etiqueta de texto.
List: Lista de datos.
Scrollbar: Barra de desplazamiento.
TextComponent: Componente de texto.
Ejemplo:
/*********************************************************/
/* Autor: Rafael Hernamperez Martin */
/* Fecha: 07-05-2000 */
/* Obj : Ejemplo basico de AWT */
/*********************************************************/
import java.awt.*;
import java.applet.Applet;
public class EjemploAWT1 extends Applet
{
public void init ()
{
// Creacion de un boton
Button btnBoton = new Button ("Boton");
add (btnBoton);
}
} // Fin de la clase EjemploAWT1


Swing
•Era el nombre clave del proyecto que desarrolló los nuevos componentes. Aunque no es un nombre oficial, frecuentemente se usa para referirse a los nuevos componentes y al API relacionado. Está inmortalizado en los nombres de paquete del API Swing, que empiezan con "javax.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.
Versiones de swing
 Swing 0.2
 Swing 1.0.3
 Swing 1.1 Beta
 Swing 1.1 Beta 3
 Swing 1.1
Caracteristicas
• Swing cambia completamente la gestión del texto. Se pueden crear texto de colores, con distintos tipos de caracteres.
• Es posible introducir incluso imágenes y, además, se pueden leer y decodificar directamente los archivos de html, rtf (Un primo de .doc).
• Es necesario gestionar los sucesos. Para esto está el paquete de java.awt.event que gestiona dichos sucesos.


Interfaces
Action
BoundedRangeModel
ButtonModel
CellEditor
ComboBoxEditor
ComboBoxModel
DesktopManager

Clases
AbstractAction
AbstractButton
AbstractCellEditor
AbstractListModel
ActionMap
BorderFactory
Box

Ejemplo de lo que podemos hacer con un apllet

martes, 5 de enero de 2010

OperacionesConMatrices

public class Matriz {
public int numeroFilas;
public int numeroColumnas;
public int [][]matriz;
public Matriz(){
}
public Matriz(int nF, int nC){
numeroFilas=nF;
numeroColumnas=nC;
matriz=new int[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;
}
}
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 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]=Integer.parseInt(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;
}
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aux = jTextField1.getText();
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
jTextArea1.setText("Suma: \n"+(m1.suma(m2)).toString());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aux = jTextField1.getText();
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
jTextArea1.setText("Resta: \n"+(m1.resta(m2)).toString());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
String aux = jTextField1.getText();
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
jTextArea1.setText("La transpuesta de la matriz uno es: \n"+(m1.Transpuesta()).toString());
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String aux = jTextField1.getText();
int nF = Integer.parseInt(aux);
aux = jTextField2.getText();
int nC = Integer.parseInt(aux);
m1 = new Matriz(nF,nC);
m1.leer();
aux = jTextField3.getText();
nF = Integer.parseInt(aux);
aux = jTextField4.getText();
nC = Integer.parseInt(aux);
m2 = new Matriz(nF,nC);
m2.leer();
jTextArea1.setText("Multiplicacion de Matrices: \n"+(m1.Multiplicacion(m2)).toString());
}
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.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
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
}

CALCULADORA

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

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

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

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

public static double logaritmo(double x){
if(x==0){
System.out.println("SOLO VALORES MAYORES A CERO");
resul = 0;
}else{
resul = Math.log(x);
}
return resul;
}

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

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

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

public static double division(double x, double y){
resul = x/y;
return resul;
}
}

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 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 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 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 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 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
}

miércoles, 2 de diciembre de 2009

SUMA BOTON

MATRIZ
import javax.swing.JOptionPane;

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\n";
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux+=matriz[i][j]+" ";
}
aux+="\n";
}
aux+=" ";
return aux;
}

}

APPLET
public class NewJApplet extends javax.swing.JApplet {

/** Initializes the applet NewJApplet */
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")
// //GEN-BEGIN:initComponents
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel4 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();

jLabel1.setBackground(new java.awt.Color(204, 255, 0));
jLabel1.setFont(new java.awt.Font("Bradley Hand ITC", 3, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(0, 0, 255));
jLabel1.setText("SUMA DE MATRICES");

jLabel2.setBackground(new java.awt.Color(255, 51, 51));
jLabel2.setText("numero de filas");

jLabel3.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);
}
});

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

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

jLabel4.setText("resultados");

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

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

jButton4.setText("sumar");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(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()
.addGap(23, 23, 23)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)
.addComponent(jButton4)
.addComponent(jButton3)
.addComponent(jButton2))
.addGap(53, 53, 53)
.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)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
.addComponent(jLabel4))
.addContainerGap(77, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(108, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(48, 48, 48))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.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.TRAILING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addComponent(jLabel4))
.addGap(11, 11, 11)
.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)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)))
.addContainerGap(31, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
}//
//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
//metodo esucha al dar click en el botton leer
String aux = jTextField1.getText();
nF = Integer.parseInt(aux);
aux = jTextField2.getText();
nC = Integer.parseInt(aux);//metodo para cambiar de string a entero
m = new Matriz(nF,nC);//matriz llena de ceros
m.leer();
}//GEN-LAST:event_jButton1ActionPerformed

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

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
jTextArea1.setText(m.toString());
jTextArea1.setText(n.toString());
jTextArea1.setText(r.toString());
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
String aux = jTextField1.getText();
nF = Integer.parseInt(aux);
aux = jTextField2.getText();
nC = Integer.parseInt(aux);//metodo para cambiar de string a entero
n = new Matriz(nF,nC);//matriz llena de ceros
n.leer();
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
r = new Matriz(nF,nC);
r = m.suma(n);
}//GEN-LAST:event_jButton4ActionPerformed


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
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;
// End of variables declaration//GEN-END:variables
public int nF;
public int nC;
public Matriz m;
public Matriz n;
public Matriz r;
}

movimiento rectilineo uniformemente variado

MATRIZ
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];
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 multiplicacion(Matriz b){
Matriz resultado;
if((this.numeroFilas == b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas, this.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 String toString(){
String aux="\n[\n";
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux += matriz[i][j]+" ";
}
aux+="\n";
}
aux+= "]";
return aux;
}
}

VECTOR
public class Vector3D extends Matriz {
double coordenadaX;
double coordenadaY;
double coordenadaZ;

public Vector3D(){
super(1,3);
}

public Vector3D(double x, double y, double z){
super(1,3);
this.matriz[0][0] = x;
this.matriz[0][1] = y;
this.matriz[0][2] = z;
coordenadaX = x;
coordenadaY = y;
coordenadaZ = z;
}

public double magnitud(){
double resultado = 0;
for(int i=0; i < 3; i++){
resultado += this.matriz[0][i]*this.matriz[0][i];
}
resultado = Math.sqrt(resultado);
return resultado;
}

public Vector3D unitario(){
Vector3D unitario = new Vector3D();
for(int i=0; i < 3; i++)
unitario.matriz[0][i] = this.matriz[0][i]/this.magnitud();
return unitario;
}

public double productoEscalar(Vector3D v){
double resultado = 0;
for(int i=0; i < 3; i++)
resultado += this.matriz[0][i]*v.matriz[0][i];
return resultado;
}

public Vector3D productoEscalarVector(double a){
Vector3D resultado = new Vector3D();
for(int i=0; i < 3; i++)
resultado.matriz[0][i] = this.matriz[0][i]*a;
return resultado;
}

public Vector3D productoCruz(Vector3D v){
Vector3D resultado;
resultado = new Vector3D();
resultado.matriz[0][0] = this.matriz[0][1]*v.matriz[0][2]-this.matriz[0][2]*v.matriz[0][1];
resultado.matriz[0][1] = this.matriz[0][2]*v.matriz[0][0]-this.matriz[0][0]*v.matriz[0][2];
resultado.matriz[0][2] = this.matriz[0][0]*v.matriz[0][1]-this.matriz[0][1]*v.matriz[0][0];
return resultado;
}

public static void main(String args[]){
Vector3D v1 = new Vector3D(-1,1,-1);
Vector3D v2 = new Vector3D(0,2,0);
System.out.println(v1+"\n"+v2);
System.out.println("LA COORDENADA EN X ES: "+v1.coordenadaX);
System.out.println("\nLA SUMA DE LOS VECTORES ES: "+(v1.suma(v2)));
System.out.println("\nLA MAGNITUD DEL VECTOR ES: "+v1.magnitud());
System.out.println("\nEL VECTOR UNITARIO ES: "+v1.unitario());
System.out.println("\nEL PRODUCTO ESCALAR ENTRE LOS DOS VECTORES ES: "+v1.productoEscalar(v2));
System.out.println("\nEL PRODUCTO CRUZ ENTRE LOS DOS VECTORES ES: "+v1.productoCruz(v2));
}
}

MRUV
public class MRUV extends Vector3D{
Matriz posicion;
Matriz velocidad;
Matriz aceleracion;
Matriz desplazamiento;
Matriz velocidad2;

public MRUV(){
}

public MRUV (Vector3D pos, Vector3D vel0, Vector3D ace, Vector3D des){
posicion = pos;
velocidad = vel0;
aceleracion = ace;
desplazamiento = des;
}

public void calcularPosicion(Vector3D ro, Vector3D vel0, Vector3D ace, double t){
posicion = ro.suma((vel0.productoEscalarVector(t)).suma((ace.productoEscalarVector(Math.pow(t,2))).productoEscalarVector(1/2)));
}

public void calcularVelocidad(Vector3D vel0, double t, Vector3D ace){
velocidad = vel0.suma(ace.productoEscalarVector(t));
}

public static void main(String args[]){
MRUV m = new MRUV();
Vector3D r0 = new Vector3D (1,0,-1);
Vector3D vel0 = new Vector3D (0,5,0);
Vector3D ace = new Vector3D (-4,0,9);
double t = 2;
m.calcularPosicion(r0, vel0, ace, t);
m.calcularVelocidad(vel0, t, ace);
System.out.println("LA POSICION A t = 2s rO = i-k v0 = 5j a = -4i+9k ES: \n" +m.posicion);
System.out.println("LA VELOCIDAD A t = 2s v0 = 5j a = -4i+9k ES: \n" +m.velocidad);
}
}

martes, 24 de noviembre de 2009

Que es un Applet

*VENTAJAS DE UN APPLET

Es un componente de una aplicación que se ejecuta en el contexto de otro programa, debe ejecutarse en un contenedor, proporciona un programa anfitrión, mediante un plugin. Un applet no pùede ejecutarse de manera independiente, son mucho menos dependientes del navegador e incluso independientes del sistema operativo del ordenador donde se ejecutan.

*PARA QUE SIRVE UN APPLET

Se puede conseguir efectos visuales y sonoros(incluso ambos a la vez), textos un movimiento utiliddades, pequeños programas interactivos, juegos interactivos, presentaciones multimedia , etc.


*DESVENTAJAS DE UN APPLET

Un applet no puede escribir o leer de un disco duro del cliente
La descarga de applet consume tiempo

BIBLIOGRAFIA
http://www.proactiva-calidad.com/java/intro_applet/que_es_un_applet.html
es.wikipedia.org/wiki/Applet
http://www.ldc.usb.ve/~vtheok/webmaestro/docs/cap20.html

domingo, 15 de noviembre de 2009

vector

Clase matriz
package matrices;

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

//crea una matriz sin dimensiones
public Matriz(){
}

//constructor con parametros
//nF es el numero de filas
//nc es el numero de columnas
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;
}


//metodo de suma de matrices
//b es el primer sumando
//retorna una matriz resultado de suma
public Matriz suma(Matriz b){
Matriz resultado;
//primero revisamos que las filas y las columnas sean iguales
//this referencia a un objeto que au no esta creado pero que alguien algun momento lo va a crear
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;
}

//metodo de multiplicacion de matrices
//retorna una matriz resultado resta
public Matriz multiplicacion(Matriz b){
Matriz resultado;
if(this.numeroFilas == b.numeroColumnas){
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.numeroFilas; 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;
}
//devuelve el objeto matriz en texto para poderlo imprimir
public String toString(){
String aux="\n[\n";
for(int i=0; i < numeroFilas; i++){
for(int j=0; j < numeroColumnas; j++){
aux += matriz[i][j]+" ";
}
aux+="\n";
}
aux+= "]";
return aux;
}
}

Clase vector3D

package matrices;


public class Vector3D extends Matriz {

double coordenadaX;
double coordenadaY;
double coordenadaZ;
double a;
double b;
double c;




public Vector3D(){
super(1,3);
}

public Vector3D(double x, double y, double z){
super(1,3);
this.matriz[0][0] = x;
this.matriz[0][1] = y;
this.matriz[0][2] = z;
coordenadaX = x;
coordenadaY = y;
coordenadaZ = z;
a = matriz[0][0];
b = matriz[0][1];
c = matriz[0][2];

}

public double magnitud(){
double resultado = 0;
for(int i=0; i < 3; i++){
resultado += this.matriz[0][i]*this.matriz[0][i];
}
resultado = Math.sqrt(resultado);
return resultado;
}

//segunda forma de hacer la magnitud
public double magnitud1(){
double resultado;
resultado = this.coordenadaX*this.coordenadaX+this.coordenadaY*this.coordenadaY+this.coordenadaZ*this.coordenadaZ;
resultado = Math.sqrt(resultado);
return resultado;
}

//tercera forma de hacer la magnitud
public double magnitud2(){
double resultado;
resultado = Math.pow(this.coordenadaX, 2)+Math.pow(this.coordenadaY, 2)+Math.pow(this.coordenadaZ, 2);
resultado = Math.sqrt(resultado);
return resultado;
}

public Vector3D unitario(){
Vector3D unitario = new Vector3D();
for(int i=0; i < 3; i++)
unitario.matriz[0][i] = this.matriz[0][i]/this.magnitud2();
return unitario;
}

public double productoEscalar(Vector3D v){
double resultado = 0;
for(int i=0; i < 3; i++)
resultado += this.matriz[0][i]*v.matriz[0][i];
return resultado;
}

public Vector3D productoCruz(Vector3D v){
Vector3D resultado;
resultado = new Vector3D();
resultado.matriz[0][0] = b *v.c-c*v.b;
resultado.matriz[0][1] = c*v.a-a*v.c;
resultado.matriz[0][2] = a*v.b-b*v.a;
return resultado;
}

public static void main(String args[]){
Vector3D v1 = new Vector3D(-1,1,-1);
Vector3D v2 = new Vector3D(0,2,0);
System.out.println(v1+"\n"+v2);
System.out.println("COORDENADA EN X : "+v1.coordenadaX);
System.out.println("\nLA SUMA DE LOS VECTORES: "+(v1.suma(v2)));
System.out.println("\nLA MAGNITUD DEL VECTOR: "+v1.magnitud());
System.out.println("\nLA MAGNITUD DEL VECTOR: "+v1.magnitud1());
System.out.println("\nLA MAGNITUD DEL VECTOR: "+v1.magnitud2());
System.out.println("\nVECTOR UNITARIO: "+v1.unitario());
System.out.println("\nPRODUCTO ESCALAR ENTRE LOS DOS VECTORES: "+v1.productoEscalar(v2));
System.out.println("\nPRODUCTO CRUZ ENTRE LOS DOS VECTORES: "+v1.productoCruz(v2));
}
}