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 taximetro; import es.javiergarbedo.coordinateslib.CoordinatesConverter; import java.text.DecimalFormat; import java.util.Calendar; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.commons.validator.routines.CreditCardValidator; /** * * @author Manuel Caamaque */ public class Taximetro extends javax.swing.JFrame { int numeroTicket = 1; Calendar ahora; /** * Creates new form Taximetro */ public Taximetro() { initComponents(); setIconImage(new ImageIcon(getClass().getResource("/imagenes/icono.png")).getImage()); botonFinal.setEnabled(false); } /** * 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() { jPanel1 = new javax.swing.JPanel(); label1 = new javax.swing.JLabel(); label2 = new javax.swing.JLabel(); latitud = new javax.swing.JTextField(); longitud = new javax.swing.JTextField(); latitudFormato = new javax.swing.JLabel(); longitudFormato = new javax.swing.JLabel(); jPanel3 = new javax.swing.JPanel(); label3 = new javax.swing.JLabel(); direccion = new javax.swing.JTextField(); jScrollPane1 = new javax.swing.JScrollPane(); area = new javax.swing.JTextArea(); label4 = new javax.swing.JLabel(); numTarjeta = new javax.swing.JTextField(); Comprobar = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); mapa = new javax.swing.JLabel(); botonInicio = new javax.swing.JButton(); botonFinal = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Taxmetro"); setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Posicin GPS Inicio", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.ABOVE_TOP)); label1.setText("Latitud:"); label2.setText("Longitud:"); latitud.addFocusListener(new java.awt.event.FocusAdapter() { public void focusLost(java.awt.event.FocusEvent evt) { latitudFocusLost(evt); } }); longitud.addFocusListener(new java.awt.event.FocusAdapter() { public void focusLost(java.awt.event.FocusEvent evt) { longitudFocusLost(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(label2).addComponent(label1)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(longitud) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(latitud, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(latitudFormato, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE) .addComponent(longitudFormato, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(48, 48, 48))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(label1).addComponent(latitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(latitudFormato, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(label2).addComponent(longitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(longitudFormato, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(37, Short.MAX_VALUE))); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del trayecto", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.ABOVE_TOP)); label3.setText("Destino"); direccion.addFocusListener(new java.awt.event.FocusAdapter() { public void focusLost(java.awt.event.FocusEvent evt) { direccionFocusLost(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(label3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(direccion, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE) .addContainerGap())); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(label3) .addComponent(direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); area.setColumns(20); area.setRows(5); jScrollPane1.setViewportView(area); label4.setText("Tarjeta de crdito:"); Comprobar.setText("Comprobar"); Comprobar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ComprobarActionPerformed(evt); } }); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Mapa", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.ABOVE_TOP)); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout .setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addComponent(mapa, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE) .addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addComponent(mapa, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); botonInicio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Bandera_verde.png"))); // NOI18N botonInicio.setBorder(null); botonInicio.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { botonInicioActionPerformed(evt); } }); botonFinal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/BANDERA.png"))); // NOI18N botonFinal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { botonFinalActionPerformed(evt); } }); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/icon175x175.png"))); // NOI18N 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(layout .createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(botonInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(botonFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent( jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(label4) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addComponent(numTarjeta, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(Comprobar))) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(42, 42, 42).addComponent(jLabel1)) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 322, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(label4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(numTarjeta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Comprobar)) .addContainerGap(22, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(botonInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(botonFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())))); pack(); }// </editor-fold>//GEN-END:initComponents private void botonInicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonInicioActionPerformed //Activacin de botones botonInicio.setEnabled(false); botonFinal.setEnabled(true); //Aqui va el ticket ahora = Calendar.getInstance(); String fechaTexto = String.valueOf(ahora.get(Calendar.DAY_OF_MONTH)); fechaTexto += "/" + (String.valueOf((ahora.get(Calendar.MONTH)) + 1)); fechaTexto += "/" + (String.valueOf(ahora.get(Calendar.YEAR))); String horaTexto = String.valueOf(ahora.get(Calendar.HOUR_OF_DAY)); horaTexto += ":" + (String.valueOf(ahora.get(Calendar.MINUTE))); horaTexto += ":" + (String.valueOf(ahora.get(Calendar.SECOND))); DecimalFormat formatoTicket = new DecimalFormat("00000"); area.setText("TICKET\n======\nNumero de Ticket: " + String.valueOf(formatoTicket.format(numeroTicket)) + "\nFecha: " + fechaTexto + "\n\nHora bajada de bandera: " + horaTexto); }//GEN-LAST:event_botonInicioActionPerformed private void botonFinalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonFinalActionPerformed //Activacion de botones botonInicio.setEnabled(true); botonFinal.setEnabled(false); //Aqui va el ticket ++numeroTicket; Calendar ahora2 = Calendar.getInstance(); String horaTexto2 = String.valueOf(ahora2.get(Calendar.HOUR_OF_DAY)); horaTexto2 += ":" + (String.valueOf(ahora2.get(Calendar.MINUTE))); horaTexto2 += ":" + (String.valueOf(ahora2.get(Calendar.SECOND))); //Tiempo transcurrido String formato = "HH:mm:ss"; long inicioMilis = (ahora.get(Calendar.HOUR) * 3600000L) + (ahora.get(Calendar.MINUTE) * 60000L) + (ahora.get(Calendar.SECOND) * 1000L); long finalMilis = (ahora2.get(Calendar.HOUR) * 3600000L) + (ahora2.get(Calendar.MINUTE) * 60000L) + (ahora2.get(Calendar.SECOND) * 1000L); String duracion = DurationFormatUtils.formatPeriod(inicioMilis, finalMilis, formato); double precio = 0.5 + (((finalMilis / 1000) - (inicioMilis / 1000)) * (0.5 / 60)); double iva = precio * 0.21; double total = (precio + iva); DecimalFormat formatoEuros = new DecimalFormat("####.##"); area.append("\nHora bajada de bandera: " + horaTexto2 + "\nDuracin del trayecto: " + duracion + "\nTarifa por minuto: 0.50 " + "\nCoste bajada de bandera: 0.50 " + "\n\nPrecio: " + (formatoEuros.format(precio)) + " " + "\nIVA(21%): " + (formatoEuros.format(iva)) + " " + "\nImporte Total: " + (formatoEuros.format(total)) + " "); }//GEN-LAST:event_botonFinalActionPerformed private void ComprobarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComprobarActionPerformed CreditCardValidator validadorTarjeta = new CreditCardValidator(); boolean validacionTarjeta = validadorTarjeta.isValid(numTarjeta.getText()); String estadoTarjeta = (validacionTarjeta == true) ? "La Tarjeta es Valida" : "La Tarjeta no es Valida"; JOptionPane.showMessageDialog(this, estadoTarjeta, "Validez de Tarjeta", JOptionPane.INFORMATION_MESSAGE); }//GEN-LAST:event_ComprobarActionPerformed private void latitudFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_latitudFocusLost String latitudConvertida = CoordinatesConverter.decimalToDMS(Double.valueOf(latitud.getText())); latitudFormato.setText(latitudConvertida); }//GEN-LAST:event_latitudFocusLost private void longitudFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_longitudFocusLost String longitudConvertida = CoordinatesConverter.decimalToDMS(Double.valueOf(longitud.getText())); longitudFormato.setText(longitudConvertida); }//GEN-LAST:event_longitudFocusLost private void direccionFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_direccionFocusLost //Cambia los siguientes valores por las direciones de origen y destino deseadas String origen = latitudFormato.getText() + "," + longitudFormato.getText(); String destino = direccion.getText(); //Hay que sustituir los espacios por el carcter %20 para que se // interprete bien la URL que se generar destino = destino.replaceAll(" ", "%20"); //Cambia el nombre [etiqueta] por el nombre de un objeto JLabel (etiqueta) que // has debido colocar antes en el formulario y que ser donde se muestre el mapa. // Deja la etiqueta del formulario sin ningn texto en el diseo. javax.swing.JLabel jLabelMapa = mapa; //Indica aqu el tamao que desees para el mapa int tamHorizontal = 256; int tamVertical = 256; //Puedes encontrar ms informacin sobre el uso de mapas estticos de Google Maps aqu: // http://code.google.com/intl/es-ES/apis/maps/documentation/staticmaps/ //A partir de aqu no hace falta cambiar nada try { String txtDireccionImagenMapa = "http://maps.google.com/maps/api/staticmap?path=" + origen + "|" + destino + "&size=" + tamHorizontal + "x" + tamVertical + "&language=ES&sensor=false"; System.out.println(txtDireccionImagenMapa); java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); java.awt.Image imagenMapa = toolkit.getImage(new java.net.URL(txtDireccionImagenMapa)); jLabelMapa.setIcon(new javax.swing.ImageIcon(imagenMapa)); } catch (java.net.MalformedURLException e) { javax.swing.JOptionPane.showMessageDialog(this, "La direccin de imagen indicada no es correcta"); } }//GEN-LAST:event_direccionFocusLost /** * @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(Taximetro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Taximetro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Taximetro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Taximetro.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 Taximetro().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Comprobar; private javax.swing.JTextArea area; private javax.swing.JButton botonFinal; private javax.swing.JButton botonInicio; private javax.swing.JTextField direccion; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel label1; private javax.swing.JLabel label2; private javax.swing.JLabel label3; private javax.swing.JLabel label4; private javax.swing.JTextField latitud; private javax.swing.JLabel latitudFormato; private javax.swing.JTextField longitud; private javax.swing.JLabel longitudFormato; private javax.swing.JLabel mapa; private javax.swing.JTextField numTarjeta; // End of variables declaration//GEN-END:variables }