martes, 12 de enero de 2010

Trabajo Final (Ejerc. 4)


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 la velocidad en Km/h");
vel = Integer.parseInt(aux);
}

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

}


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

public class Interfase2 extends JFrame{
public Interfase2(){
super("VELOCIMETRO");
setSize(600,400);
show();
}

public void paint(Graphics g){

super.paint(g);
g.setColor(Color.BLUE);
g.drawRect(50, 100, 20, 280);
g.setColor(Color.BLACK);
g.drawString("280 Km/h", 75, 100);
g.drawString("0 Km/h", 75, 380);
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.gray);
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", 400, 50);
g.drawString("PASTEL DE PORCENTAJE", 400,70);
}

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.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()>=80& s.getVel()<150){
g.setColor(Color.ORANGE);
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(50, 380-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(50, 380-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(50, 380-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[]){
Interfase2 vel = new Interfase2();

vel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


1 comentario:

MaDeLa!nE dijo...

Hola!!! podrías haber hecho la grafica horizontal y sería de este modo:


g.drawRect(15, 330, 280, 20);
g.drawString("280 km/h", 270, 375);
g.drawString("0 km/h", 10,375);


r=280-s.getVelocidad();
g.fillRect(15,330,280-r,20);
g.setColor(Color.black);
g.drawString(s.getVelocidad()+" km/h",280-r,325);

Asi obtendrias un sensor de velocidad lineal de forma horizontal!