PROGRAMACIÓN ORIENTADA A OBJETOS
SIMULACION DE ELEVADOR EN JAVA
Este programa simula la rutina de un elevador utilizando polimorfismo herencia e interfaz y por ultimo manejo de excepciones:
PANEL
package eleva;
import java.awt.Color;
import java.awt.Graphics;
public class Panel extends javax.swing.JPanel {
private int x1=201;
private int y1=50;
private int a1=201;
private int b1=150;
private int i=0;
private int r=0;
private int u=0;
private int z=0;
private int n;
private int n1;
private int x=199;
private int y=50;
private int am=199;
private int b=150;
private int x11=0;
private int y11=0;
private int a11=0;
private int b11=0;
private int va;
private int[] gg=new int[6];
private int[] vv=new int[6];
public void setva(int va) {
this.va = va;}
public void are(){
gg[0]=60;gg[1]=60;gg[2]=60;gg[3]=60;gg[4]=60;gg[5]=60;
vv[0]=60;vv[1]=80;vv[2]=100;vv[3]=120;vv[4]=140;vv[5]=160;}
public void entrar(){
this.are();int l=50;
if (this.va<=5){
for (int i=0;i<this.va;i++){
gg[i]+=l;
vv[i]=100;
l+=20;
}
}else{ n=50;n1=50;
} repaint();
}
public Panel() { initComponents();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawRoundRect(130,23 , 130,23,40,40);
g.drawString(" piso", 134, 35);
String [] jj=new String[6];
jj[0]="1";
jj[1]=" 2";
jj[2]=" 3";
jj[3]=" 4";
jj[4]=" 5";
jj[5]=" 6";
try{
if(this.sub){
for(int i=0;i<50;i++){
Thread.sleep(10);
g.drawString(jj[0], 180, 40);
}
for(int i=0;i<40;i++){
Thread.sleep(20);
g.drawString(jj[1], 180, 40);
}
for(int i=0;i<50;i++){
Thread.sleep(10);
g.drawString(jj[2], 180, 40);
}
for(int i=0;i<50;i++){
Thread.sleep(20);
g.drawString(jj[3], 180, 40);
}
}repaint();
}
catch(InterruptedException e){ e.printStackTrace();}
g.drawRoundRect(130,23 , 130,23,40,40);
g.drawString(" piso", 134, 35);
jj[0]="4";
jj[1]=" 3";
jj[2]=" 2";
jj[3]=" 1";
try{
if(this.sub1){
for(int i=0;i<50;i++){
Thread.sleep(10);
g.drawString(jj[3], 180, 40);
}
for(int i=0;i<40;i++){
Thread.sleep(20);
g.drawString(jj[2], 180, 40);
}
for(int i=0;i<50;i++){
Thread.sleep(10);
g.drawString(jj[1], 180, 40);
}
for(int i=0;i<50;i++){
Thread.sleep(20);
g.drawString(jj[0], 180, 40);
}
}repaint();
}
catch(InterruptedException e){ e.printStackTrace();}
g.setColor(Color.blue);
g.drawRoundRect(100,50 , 200,100,40,40);
g.drawLine(x11, y11, a11,b11);
//x11=0;y11=0;a11=0;b11=0;
g.drawLine(x, y, am, b);
g.drawLine(x1, y1, a1,b1 );
String [] a=new String [6];
a[0]="(o,o)";
a[1]="(o,o)";
a[2]="(o,o)";
a[3]="(o,o)";
a[4]="(o,o)";
a[5]="(o,o)";
clase obj=new clase (false);
String y11=obj.info();
g.drawString(y11, n,n1);
n=0;n1=0;
this.are();
this.entrar();
g.setColor(Color.blue);
g.drawString(a[0], gg[0],vv[0]);
g.drawString(a[1], gg[1],vv[1]);g.drawString(a[2],gg[2],vv[2]);
g.drawString(a[3], gg[3],vv[3]);g.drawString(a[4], gg[4],vv[4]);g.drawString(a[5],gg[5],vv[5]);
g.setColor(Color.red);
g.clearRect(i,u , r,z);
g.setColor(Color.YELLOW);
g.drawLine(x, y, am, b);
g.drawLine(x1, y1, a1,b1 );
}
public void Izquierda(){
this.x1=279;this.a1=279;this.x11=121;this.a11=279;this.y11=70;this.b11=70;this.x=121;this.am=121;
i=0;u=0;r=0;z=0;
repaint();
}
public void derecha(){
i=100;u=50;r=205;z=105;
this.x1=201;this.a1=201;this.x11=0;this.a11=0;this.y11=0;this.b11=0;this.x=199;this.am=199;
repaint();
}
private boolean sub,sub1;
public void su(){
if(sub==false)sub=true;
else sub=false;
repaint();
}
public void su1(){
if(sub1==false)sub1=true;
else sub1=false;
repaint();
}
}
package eleva;
public class robotpri1 extends javax.swing.JFrame {
public robotpri1() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
CLASE EJECUTORA
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int Xo = Integer.parseInt(this.textX1.getText());
panel1.setva(Xo);
panel1.entrar(); // TODO add your handling code here:
} private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
panel1.su();
// TODO add your handling code here:
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
panel1.su1();
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new robotpri1().setVisible(true);
}
package eleva;
public class clase extends classs implements inter {
private static boolean a;
public clase() {
}
public clase(boolean a) {
this.a=a;
}
@Override
public void calculo(int s) {
if(s<=5)
this.a=true;
else this.a=false;
}
@Override
public String info() {
String d;
if (a){
d="ELEVADOR SUBIENDO";return d;
}else d="SOBRECARGA DE PESO";return d;}}
CLASE ABSTRACTA
package eleva;
public abstract class classs {
public abstract String info();
}
CLASE INTERFAZ
package eleva;
public interface inter {
public void calculo(int s);
}
EJECUCIÓN: