martes, 12 de enero de 2010

sensor de velocidad


SENSOR

import javax.swing.*;

public class Sensor {
private int velo;

public Sensor(){

}

public void setVelo(int v) {
velo = v;
}

public int getVelo(){
return velo;
}

public void leerVelo(){
String aux;
aux = JOptionPane.showInputDialog("Ingrese el valor de la velocidad");
velo = Integer.parseInt(aux);
}

public static void main(String args[]){
Sensor s = new Sensor();
s.leerVelo();
System.out.println("Velocidad = "+s.getVelo());
}
}

INTERFACE

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

public class Interfase extends JFrame {

public Interfase(){
super("Sistema de monitoreo de velocidad");
setSize(500,500);
show();
}

public void paint(Graphics g){
int r,p,y;
super.paint(g);
g.setColor(Color.BLACK);{
g.drawOval(80, 150, 150, 150);
}
g.setColor(Color.BLACK);{
g.drawString("Alerta", 140, 90);
}
g.setColor(Color.white);{
g.fillOval(80, 150, 150, 150);
}
g.setColor(Color.BLACK);{
g.drawString("SENSOR DE VELOCIDAD", 160, 180);
}


Sensor s = new Sensor();
s.leerVelo();
if (s.getVelo()>0 & s.getVelo()<= 80);
g.setColor(Color.BLACK);
//g.drawString(s.getVelo()+" km/h", 140,180);
r=315-s.getVelo();
g.setColor(Color.blue);
g.fillArc(80,150,150,150,225,-(320-r));
g.setColor(Color.GREEN);
g.fillOval(150, 100, 20, 20);
if(s.getVelo()>80& s.getVelo()<=160);{
g.setColor(Color.BLACK);
g.drawString(s.getVelo()+" Km/h", 150, 220);
r=315-s.getVelo();
g.setColor(Color.ORANGE);
g.fillArc(80,150,150,150,225,-(320-r));
g.setColor(Color.yellow);
g.fillOval(150, 100, 20, 20);
}
if(s.getVelo()>160& s.getVelo()<=280){
g.setColor(Color.BLACK);
g.drawString(s.getVelo()+" Km/h",150, 220);
r=315-s.getVelo();
g.setColor(Color.CYAN);
g.fillArc(80,150,150,150,225,-(420-r));
g.setColor(Color.RED);
g.fillOval(150, 100, 20, 20);
}
}
public static void main(String args[]){
Interfase velocidad = new Interfase();
velocidad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

trabajo final pregunta 1

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

AWT

Es una librería de clases Java para desarrollar interfaces de usuario gráficas. La librería AWT esta compuesta por:

- Los Componentes (java.awt.Component), como :
Button
Canvas
Checkbox
Choice
Container
Panel
ScrollPane
Window
Dialog
FileDialog
Frame
Label
List
Scrollbar
TextComponent
TextArea
TextField

- Los Contenedores (java,awt.containers), contienen componentes como:
Panel
Applet
ScrollPane
Window
Dialog
FileDialog
Frame

- Los gestores de posición (java.awt.LayoutManager), que posiciona los componentes
dentro de los contenedores.
- Los eventos (java.awt.AWTEvent), que nos indican las acciones del usuario.

BIBLIOGRAFIA:
http://usuarios.multimania.es/java_2000/pagina_nueva_1.htm
http://www.alegsa.com.ar/Dic/awt.php


SWING

Es la libreria de componentes visuales mas nueva que proporciona java, usando esta libreria se podran construir los tres tipos de programas o aplicaciones que son JFRAME, JWINDOW y JAPPLET.
PARA PROPOSITOS DE ESTE CURSO NOS CONCENTRAMOS EN JAVA FRAME Y JAVA APPLET derivados de JAVA AWT (la libreria visual antigua), por la triste razón de que el modelo de ejecución de estos programas, esta muy asociado al compilador de java que traen los browsers.

BIBLIOGRAFIA:
http://yosh35.tripod.com/id5.html



2 Defina un applet, describa su funcionamiento:
un appelet es un componente de una aplicacion que se ejecuta en un programa no puede trabajar de manera independiente y realiza una funcion especifica.
un ejemplo claro es el:

java applet que es un código java que no tiene el método main, utilizado principalmente para el trabajo de páginas web, ya que es un pequeño programa que es utilizado en una página HTML y representado por una pequeña pantalla gráfica dentro de ésta.
Cuando un applet se inicia, se llaman en este orden a los siguientes métodos:
init: son instrucciones para inicializar el applet.
start: llamado cuando se inicualiza un applet igual que el init pero tambien cuando se vuelve a ocupar un applet
paint: Se encarga de mostrar el contenido del applet. .
Para terminar la ejecución o pausarse se llama a los siguientes métodos:
stop: Suspende la ejecución del programa.
destroy: Cuando no se va a necesitar más el applet. Se usa para liberar recursos.

BIBLIOGRAFIA
http://es.wikipedia.org/wiki/Applet
http://www.desarrolloweb.com/articulos/731.php
http://es.wikipedia.org/wiki/Applet_Java

trabajo final pregunta 4


4 Realizar un Frame Cada uno de los sensores deberá indicar la velocidad lineal de un automóvil, la cual varia de 0 a 280 Km/h:

import javax.swing.*;
import java.awt.*;
import trabajofinal.Sensor;
import java.text.DecimalFormat.*;

public class Velometro extends JFrame{
public Velometro(){
super("SENSOR DE VELOCIDAD");
setSize(600,400);
show();
}

public void paint(Graphics g){
int r,p,y;
super.paint(g);
g.setColor(Color.BLACK);{
g.drawRect(80, 150, 30, 150);
g.drawOval(370, 150, 150, 150);
}
g.setColor(Color.BLACK);{
g.drawString("0 Km/h", 80, 320);
g.drawString("280 km/h", 80, 140);
g.drawString("Alerta", 140, 90);
}
g.setColor(Color.white);{
g.fillOval(370, 150, 150, 150);
g.fillRect(80, 150, 30, 150);
}
g.setColor(Color.BLACK);{
g.drawLine(300, 0, 300, 400);
g.drawString("SENSOR DE VELOCIDAD LINEAL",60 , 50);
g.drawString("SENSOR DE VELOCIDAD", 370, 50);
g.drawString("PASTEL DE PORCENTAJE", 370,70);
}


Sensor s = new Sensor();
s.leerVelocidad();
if (s.getVelocidad()>0 & s.getVelocidad()<= 80){
g.setColor(Color.LIGHT_GRAY);
g.fillRect(80, 300-s.getVelocidad(), 30, s.getVelocidad());
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" km/h", 150,220);
r=315-s.getVelocidad();
p=((s.getVelocidad()*100)/280);
g.setColor(Color.blue);
g.fillArc(370,150,150,150,225,-(320-r));
g.setColor(Color.black);
g.drawString(p+"%", 430, 220);
g.setColor(Color.GREEN);
g.fillOval(150, 100, 20, 20);//ovalao pequeño
}
if(s.getVelocidad()>80& s.getVelocidad()<=160){
g.setColor(Color.cyan);
g.fillRect(80, 150, s.getVelocidad(), -(150-s.getVelocidad()));
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" Km/h", 150, 220);
r=315-s.getVelocidad();
p=((s.getVelocidad()*100)/280);
g.setColor(Color.ORANGE);
g.fillArc(370,150,150,150,225,-(320-r));
g.setColor(Color.black);
g.drawString(p+"%", 430, 220);
g.setColor(Color.yellow);
g.fillOval(150, 100, 20, 20);
}
if(s.getVelocidad()>160& s.getVelocidad()<=280){
g.setColor(Color.YELLOW);
g.fillRect(80, 150, 30, 150-s.getVelocidad());
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" Km/h",150, 220);
r=315-s.getVelocidad();
p=((s.getVelocidad()*100)/280);
g.setColor(Color.CYAN);
g.fillArc(370,150,150,150,225,-(420-r));
g.setColor(Color.black);
g.drawString(p+"%", 430, 220);
g.setColor(Color.RED);
g.fillOval(150, 100, 20, 20);
}
}
public static void main(String args[]){
Velometro velocidad = new Velometro();
velocidad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

trabajo final pregunta 3



















3 Realizar un applet para operaciones con matrices:

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];
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);
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 resultado = new Matriz (this.numeroFilas, this.numeroColumnas);
for(int i=0; i for(int j=0; jresultado.matriz[0][0]=((this.matriz[1][1]*this.matriz[2][2])-(this.matriz[2][1]*this.matriz[1][2]));
resultado.matriz[0][1]=((this.matriz[1][0]*this.matriz[2][2])-(this.matriz[2][0]*this.matriz[1][2]));
resultado.matriz[0][2]=((this.matriz[1][0]*this.matriz[2][1])-(this.matriz[2][0]*this.matriz[1][1]));
resultado.matriz[1][0]=((this.matriz[0][1]*this.matriz[2][2])-(this.matriz[2][1]*this.matriz[0][2]));
resultado.matriz[1][1]=((this.matriz[1][0]*this.matriz[2][2])-(this.matriz[2][0]*this.matriz[1][2]));
resultado.matriz[1][2]=((this.matriz[0][0]*this.matriz[2][1])-(this.matriz[2][0]*this.matriz[0][1]));
resultado.matriz[2][0]=((this.matriz[0][1]*this.matriz[1][2])-(this.matriz[1][1]*this.matriz[0][2]));
resultado.matriz[2][1]=((this.matriz[0][0]*this.matriz[1][2])-(this.matriz[1][0]*this.matriz[0][2]));
resultado.matriz[2][2]=((this.matriz[0][0]*this.matriz[1][1])-(this.matriz[1][0]*this.matriz[0][1]));
return resultado;
}

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

public static void main(String args[]){
Matriz a= new Matriz(3,3);
a.matriz[0][0]=1; a.matriz[0][1]=2;
a.matriz[1][0]=3; a.matriz[1][1]=7;
a.matriz[2][0]=3; a.matriz[2][2]=7;
System.out.println("la inversa es " +(a.deteminantes()));
}
}

APPLET
public class Operaciones extends javax.swing.JApplet {
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) {
// 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("Resta de Matrices: \n"+aux1+(m1.resta(m2)).toString());

}

private void jButton3ActionPerformed(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("Multiplicacion de Matrices: \n"+aux1+(m1.Multiplicacion(m2)).toString());

}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
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("La transpuesta de la matriz es: \n"+aux1+(m1.Transpuesta()).toString());
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
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("La inversa de la matriz es: \n"+aux1+(m1.inversa()).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.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JLabel jLabel10;
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 jLabel9;
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;
// End of variables declaration
}