domingo, 10 de enero de 2010

Velocimetro

Clase Sensor:

import javax.swing.JOptionPane;

/*
* To change this template, choose Tools Templates
* and open the template in the editor.
*/

/**
*
* @author Madelaine
*/
public class Sensor {

private int velocidad;

public Sensor(){

}

public void setVelocidad(int v) {
velocidad = v;
}

public int getVelocidad(){
return velocidad;
}

public void leerVelocidad(){
String aux;
aux = JOptionPane.showInputDialog("Valor de la Velocidad");
velocidad = Integer.parseInt(aux);
}

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

}



-------------------------------------------------------------------------------

Clase VelKm

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

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

public void paint(Graphics g){
int r;
super.paint(g);
g.setColor(Color.BLACK);
g.drawOval(100, 150, 150, 150);
g.setColor(Color.BLACK);
g.drawString("0 Km/h", 100, 300);
g.drawString("280 km/h", 210, 300);
g.drawString("!!!Alerta!!!", 170, 90);
g.drawOval(170, 100, 20, 20);

g.setColor(Color.pink);
g.fillOval(100, 150, 150, 150);


Sensor s = new Sensor();
s.leerVelocidad();
if (s.getVelocidad()>0 & s.getVelocidad()< 60){
g.setColor(Color.LIGHT_GRAY);

r=320-s.getVelocidad();
//g.fillArc(100,r+s.getVelocidad(),150,150,225,315);
g.fillArc(100,150,150,150,225,-(320-r));
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" km/h", 150,220);


g.setColor(Color.GREEN);
g.fillOval(170, 100, 20, 20);//ovalao pequeño
}
if(s.getVelocidad()>=60 & s.getVelocidad()<100){
g.setColor(Color.cyan);
r=315-s.getVelocidad();
g.fillArc(100,150,150,150,225,-(320-r));
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" Km/h", 150, 220);
g.setColor(Color.GREEN);
g.fillOval(170, 100, 20, 20);
}
if(s.getVelocidad()>=100 & s.getVelocidad()<=280){
g.setColor(Color.YELLOW);
r=315-s.getVelocidad();
g.fillArc(100, 150,150,150,225,-(320-r));
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" Km/h",150, 220);

g.setColor(Color.RED);
g.fillOval(170, 100, 20, 20);
}

}

public static void main(String args[]){
VelKm vel = new VelKm();
vel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


}







3 comentarios:

  1. Hola Made en tu codigo del VelKM
    Sensor s = new Sensor();
    s.leerVelocidad();
    if (s.getVelocidad()>0 & s.getVelocidad()< r="320-s.getVelocidad();">=60 & s.getVelocidad()<100){ r="315-s.getVelocidad();">=100 & s.getVelocidad()<=280){ g.setColor(Color.YELLOW); r=315-s.getVelocidad(); g.fillArc(100, 150,150,150,225,-(320-r)); g.setColor(Color.BLACK); g.drawString(s.getVelocidad()+" Km/h",150, 220); g.setColor(Color.RED); g.fillOval(170, 100, 20, 20); } } public static void main(String args[]){ VelKm vel = new VelKm(); vel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
    tambien podrias utilizar:

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

    ResponderEliminar
  2. Hola Made, la mayoría del código que utikizas esta bien pero tienes un error en esta parte del codigo:

    s.leerVelocidad();
    if (s.getVelocidad()>0 & s.getVelocidad()< r="320-s.getVelocidad();">=60 & s.getVelocidad()<100){ r="315-s.getVelocidad();">=100 & s.getVelocidad()<=280){ g.setColor(Color.YELLOW); r=315-s.getVelocidad(); g.fillArc(100, 150,150,150,225,-(320-r)); g.setColor(Color.BLACK); g.drawString(s.getVelocidad()+" Km/h",150, 220); g.setColor(Color.RED); g.fillOval(170, 100, 20, 20); } } public static void main(String args[]){ VelKm vel = new VelKm(); vel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }

    El programa no corre, esta con errores, creo que te confundes con la variable r que defines antes de esta parte del codigo, no es necesario definir esta variable puedes hacerlo asi:

    Sensor s = new Sensor();
    s.leerVel();

    if (s.getVel()>0 & s.getVel()<80{
    g.setColor(Color.BLUE);
    g.fillArc(100, 150,150,150,225,-(320-(315-s.getVel())));
    g.setColor(Color.WHITE);
    g.drawString(s.getVel()+" Km/h", 150, 220);
    g.setColor(Color.GREEN);
    g.fillOval(170, 100, 20, 20);
    }
    if(s.getVel()>=80 & s.getVel()<150){
    g.setColor(Color.YELLOW);
    g.fillArc(100, 150,150,150,225,-(320-(315-s.getVel())));
    g.setColor(Color.WHITE);
    g.drawString(s.getVel()+" Km/h",150, 220);
    g.setColor(Color.GREEN);
    g.fillOval(170, 100, 20, 20);
    }
    if(s.getVel()>=150 & s.getVel()<=240){
    g.setColor(Color.RED);
    g.fillArc(100, 150,150,150,225,-(320-(315-s.getVel())));
    g.setColor(Color.WHITE);
    g.drawString(s.getVel()+" Km/h", 150,220);
    g.setColor(Color.RED);
    g.fillOval(170, 100, 20, 20);
    }

    ResponderEliminar
  3. Esta medio rara la parte de el codigo del sensor,pero si le cambias esa parte seria igual a como te dice el jose, ya funciona bien.

    ResponderEliminar