Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jcine; import java.awt.Color; import java.awt.Component; import java.io.IOException; import java.net.MalformedURLException; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.json.simple.parser.ParseException; /** * * @author Michael */ public class CineForm extends javax.swing.JFrame { /** * Creates new form CineForm */ public CineForm() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); tEntryPoint = new javax.swing.JTextField(); bLoad = new javax.swing.JButton(); pHall = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("URL de servicio"); tEntryPoint.setText("http://localhost:3000/asientos.json"); tEntryPoint.setToolTipText(""); bLoad.setText("Cargar"); bLoad.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bLoadActionPerformed(evt); } }); javax.swing.GroupLayout pHallLayout = new javax.swing.GroupLayout(pHall); pHall.setLayout(pHallLayout); pHallLayout.setHorizontalGroup(pHallLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE)); pHallLayout.setVerticalGroup(pHallLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 568, Short.MAX_VALUE)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(48, 48, 48).addComponent(jLabel1).addGap(18, 18, 18) .addComponent(tEntryPoint, javax.swing.GroupLayout.PREFERRED_SIZE, 507, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(bLoad) .addContainerGap(307, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addComponent(pHall, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(tEntryPoint, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(bLoad)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pHall, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void bLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoadActionPerformed try { // TODO add your handling code here: AsientosClient client = new AsientosClient(tEntryPoint.getText()); Asiento[] asientos = client.getAsientos(); clearHall(); buildHall(asientos); } catch (MalformedURLException ex) { JOptionPane.showMessageDialog(this, "Direccin de servicio est incorrecta!"); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "No se puede conectarse al servidor!"); } catch (ParseException ex) { JOptionPane.showMessageDialog(this, "La respuesta del servidor est incorrecta!"); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Error desconocido"); } }//GEN-LAST:event_bLoadActionPerformed //eliminar todas las sillas private void clearHall() { Component[] children = pHall.getComponents(); for (int i = 0; i < children.length; i++) { pHall.remove(children[i]); } } void chairPressed(java.awt.event.ActionEvent evt) { if (evt.getSource() instanceof JButton) { JButton btn = (JButton) evt.getSource(); if (btn.getBackground() == Color.white) { btn.setBackground(null); } else { btn.setBackground(Color.white); } } } // private void buildHall(Asiento[] asientos) { //Encontraremos las dimensiones de sala Integer maxX = 0; Integer maxY = 0; for (int i = 0; i < asientos.length; i++) { if (asientos[i].getPosX() > maxX) { maxX = asientos[i].getPosX(); } if (asientos[i].getPosY() > maxY) { maxY = asientos[i].getPosY(); } } //los tamaos de sillas Integer sizeX = pHall.getWidth() / maxX; Integer sizeY = pHall.getHeight() / maxY; Boolean[][] matrix = new Boolean[maxX][maxY]; //sillas for (int i = 0; i < asientos.length; i++) { JButton btn = new JButton("btn_" + Integer.toString(i)); btn.setText(asientos[i].getName()); btn.setLocation(sizeX * (asientos[i].getPosX() - 1), sizeY * (asientos[i].getPosY() - 1)); btn.setSize(sizeX - SPACER, sizeY - SPACER); btn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { chairPressed(evt); } }); matrix[asientos[i].getPosX() - 1][asientos[i].getPosY() - 1] = true; pHall.add(btn); } //pasillos for (int x = 0; x < maxX; x++) { for (int y = 0; y < maxY; y++) { if (matrix[x][y] == null) { JPanel panel = new JPanel(); panel.setBackground(Color.cyan); panel.setLocation(x * sizeX - SPACER, y * sizeY - SPACER); panel.setSize(sizeX + 2 * SPACER, sizeY + 2 * SPACER); pHall.add(panel); } } } pHall.repaint(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(CineForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CineForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CineForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CineForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new CineForm().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton bLoad; private javax.swing.JLabel jLabel1; private javax.swing.JPanel pHall; private javax.swing.JTextField tEntryPoint; // End of variables declaration//GEN-END:variables private static final int SPACER = 5; //Espacio entre sillas }