//VELOCIMETRO
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(400,400);
show();
}
public void paint(Graphics g){
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("240 km/h", 210, 300);
g.drawString("!!!Alerta!!!", 170, 90);
g.drawOval(170, 100, 20, 20);
g.setColor(Color.gray);
g.fillOval(100, 150, 150, 150);
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);
}
}
public static void main(String args[]){
Interfase2 vel = new Interfase2();
vel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
5 comentarios:
Hola José mira yo tambien utilise un fillArc y el consejo que te puedo dar es que en lugar de poner :
g.fillArc(100, 150,150,150,225,-(320-(315-s.getVel()))) solo podrias poner esto:
g.fillArc(100, 150, 100,100,360, s.getTemp());
que es dar la posicion de tu ovalo y el numero de grados+s.getTemp());
es otra manera de llegar a lo mismo aqui esta mi codigo si te sirve:
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);
}
}
que tal si ases asi te evitarias un podo para no confunmdirte con los numeros y los parentesis en tu prugrama
x=320-s.getVelocidad();
g.fillArc(80,150,150,150,225,-(320-x));
para que no te confundas ocupa letras men si por que o si no es facil confundirse
En tu código
g.fillArc(100, 150,150,150,225,-(320-(315-s.getVel())));
pordrias utilizar tambien esta opcion
r=320-s.getVelocidad();
g.fillArc(100,150,150,150,225,-(320-r));
g.setColor(Color.BLACK);
g.drawString(s.getVelocidad()+" km/h", 150,220);
bueno si mijo en esa parte tu programa esta bien confuso no esta mal pero confuso hay formas mas sencillas de plantear como:
g.fillArc(100, 150,150,150,225,-(320-(315-s.getVel())))
r=315-s.getVelocidad();
g.fillArc(100,150,150,150,225,-(320-r));
no se es mas sencillo y mas facil de comprender
Realmente el programa se haria mucho mas entendible si remplazas lineas de codigo por una variable de tal forma que solo pongas la variable y no repitas el codigo como al obtener la velocidad:
x = 320-s.getVelocidad();
pero el codigo esta bien solo seria para que sea mas facil de editar y entender.
Publicar un comentario