ELEVADOR

Ejercicio que simula el funcionamiento de un elevador aplicando conocimientos en gráficos en java.

Codigo del elevador
Jpanel 

package elevador;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

public class JPanel extends javax.swing.JPanel {
  
    private boolean abierto=false;
    private boolean sol=false;
  
  
    private String m = "Maximo 5 personas";
    private String cara = "() ()";
    private String car1 = "('o')";
    private String car2 = "((')(')";
    private int persona =0;
    private int piso=1;
    private int p=0;
    //private int x=0;

  
    public JPanel() {
        initComponents();
    }
  
   
    @Override
    public void paint(Graphics g) {
        super.paint(g);
         String c="PISO: "+String.valueOf(piso);
         g.drawString(c, 400,100);
         if(persona>5){g.drawString(m, 400, 50);}

          
      
        for(int i=0;i<5;i++){
        g.drawLine(50+i, 0, 50+i, 299);//Lineas derechas
        g.drawLine(349-i, 0, 349-i, 299);//Lineas izquierdas
        g.drawLine(50, 0+i, 349-i, 0+i);//Lineas superiores
        g.drawLine(50, 299-i, 349-i, 299-i);
        }

        if(abierto==true){
                for(int i=0;i<147;i++){
                     g.setColor(Color.GRAY);
                     g.drawLine(200-i, 2, 200-i, 300);
                     g.drawLine(199+i, 2, 199+i, 300);}     }

                   
  
        if(abierto==false){
      
                for(int i=0;i<147;i++){
                     g.setColor(Color.BLUE);
                     g.drawLine(200-i, 2, 200-i, 300);
                     g.drawLine(199+i, 2, 199+i, 300);}  
               for(int i=0;i<3;i++){
                     g.setColor(Color.BLACK);
                 g.drawLine(201-i, 0,201-i, 299);}//Lineas centrales
                }
      
      
         if((persona<11)&&(abierto)){
             p=0;
            for(int i=0;i<persona;i++){
             if(i<5){ 
             g.setColor(Color.WHITE);
             g.drawString(cara, 150, 15+p);
             g.drawString(car1, 150, 25+p);
             g.drawString(car2, 150, 35+p);
             p+=50;}
             if(i==5){ p=0;}
             if(i>=5){
             g.setColor(Color.WHITE);
             g.drawString(cara, 250, 15+p);
             g.drawString(car1, 250, 25+p);
             g.drawString(car2, 250, 35+p);
             p+=50; }  }     }
      
      

   }   //Redefinicion del metodo Paint
 

  
  
   @Override
   public Dimension getPreferredSize()
   {return new Dimension(550, 300);}
    //Dimensiones del panel en pixeles
  
  
  public void solicitar(){
  abierto=true;
  sol=true;
  repaint();}//Abrir
  
  public void entrar(){
  if((persona<10)&&(abierto)) {persona++;
  repaint();}
  }
 
   public void sacar(){
  if(persona>0) {persona--;
  repaint();}
  }
 
  public void subir(){
 
  if((persona<6)&&(abierto)&&(piso<10)) {
      piso++;
  sol=false;  
  abierto=false;
  persona=0;
  repaint();
  }}
 
  public void bajar(){
  if((persona<6)&&(abierto)&&(piso>1)) {
      piso--;
  sol=false;  
  abierto=false;
  persona=0;
  repaint();
  }}
 
 
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 412, Short.MAX_VALUE)
        );
    }// </editor-fold>                      
    // Variables declaration - do not modify                   
    // End of variables declaration                 
}


 Jframe


package elevador;

public class JFrame extends javax.swing.JFrame {

    public JFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jPanel1 = new elevador.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(204, 204, 0));

        jPanel1.setBackground(new java.awt.Color(255, 255, 102));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 550, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jButton1.setText("SUBIR PISO");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 36));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("CLASE ELEVADOR");

        jButton2.setText("ENTRAR PERSONA");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("SACAR PERSONA");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("BAJAR PISO");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setForeground(new java.awt.Color(255, 0, 51));
        jButton5.setText("SOLICITAR ASCENSOR");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(jButton5)
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                        .addGap(27, 27, 27))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(43, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton1))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton4)
                            .addComponent(jButton3)))
                    .addComponent(jButton5))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
       jPanel1.subir(); 
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        jPanel1.entrar();
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
         jPanel1.sacar();
    }                                       

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        jPanel1.solicitar();
    }                                       

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        jPanel1.bajar();
    }                                       

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrame().setVisible(true);
            }
        });
    }
   
   
   
   
   
   
    // 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.JLabel jLabel1;
    private elevador.JPanel jPanel1;
    // End of variables declaration                  
}
 








1 comentarios:

oscar moreno dijo...

tendras el proyecto para descargar

Publicar un comentario en la entrada