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 taximetrotema2; import es.javiergarbedo.coordinateslib.CoordinatesConverter; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Calendar; import javax.swing.JOptionPane; import org.apache.commons.lang3.time.DurationFormatUtils; import org.apache.commons.validator.routines.CreditCardValidator; /** * * @author FP Informtica */ public class Main extends javax.swing.JFrame { final double TARIFAXMINUTO = 0.50; final double IVA = 0.21; int contador = 0; Calendar fechaHoraSalida; Calendar fechaHoraFin; DateFormat formatoHora = DateFormat.getTimeInstance(DateFormat.MEDIUM); DateFormat formatoFecha = DateFormat.getTimeInstance(DateFormat.MEDIUM); /** * Creates new form Main */ public Main() { initComponents(); jToggleButtonStop.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(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jTextFieldLatitud = new javax.swing.JTextField(); jTextFieldLongitud = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); jLabel10 = new javax.swing.JLabel(); jTextFieldDestino = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); jToggleButtonStop = new javax.swing.JToggleButton(); jToggleButtonBanderaVerde = new javax.swing.JToggleButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jPanel3 = new javax.swing.JPanel(); jLabelMapaRuta = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jTextFieldTarjeta = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Taxmetro"); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("")); jLabel2.setText("Latitud:"); jLabel3.setText("Longitud:"); jTextFieldLatitud.setText("36.679619"); jTextFieldLatitud.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextFieldLatitudActionPerformed(evt); } }); jTextFieldLongitud.setText("-5.444740"); jTextFieldLongitud.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextFieldLongitudActionPerformed(evt); } }); jLabel4.setText("30 40' 46\""); jLabel5.setText("-5 26' 41\""); jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Highway.png"))); // NOI18N 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, false) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel2) .addGap(18, 18, 18).addComponent(jTextFieldLatitud)) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldLongitud, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(18, 18, 18) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel5)) .addGap(48, 48, 48).addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2) .addComponent(jTextFieldLatitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)).addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextFieldLongitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel1.setText("Posicin GPS Inicio"); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("")); jLabel10.setText("Destino:"); 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(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldDestino).addContainerGap())); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(24, 24, 24).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel10) .addComponent(jTextFieldDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(29, Short.MAX_VALUE))); jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel6.setText("Datos del Trayecto"); jToggleButtonStop .setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/StopSign-nofont.png"))); // NOI18N jToggleButtonStop.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButtonStopActionPerformed(evt); } }); jToggleButtonBanderaVerde .setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/green_flag.png"))); // NOI18N jToggleButtonBanderaVerde.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jToggleButtonBanderaVerdeActionPerformed(evt); } }); jTextArea1.setEditable(false); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("")); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelMapaRuta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelMapaRuta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jLabel7.setText("Mapa:"); jLabel8.setText("Tarjeta de Crdito:"); jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N jButton1.setText("Comprobar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7) .addGroup(layout.createSequentialGroup() .addComponent(jToggleButtonBanderaVerde).addGap(38, 38, 38) .addComponent(jToggleButtonStop, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8).addComponent(jButton1) .addComponent(jTextFieldTarjeta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel6)) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(10, 10, 10) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .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.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldTarjeta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1).addGap(0, 52, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jToggleButtonStop, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jToggleButtonBanderaVerde, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jToggleButtonBanderaVerdeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonBanderaVerdeActionPerformed jToggleButtonStop.setEnabled(true); fechaHoraSalida = Calendar.getInstance(); formatoHora.format(fechaHoraSalida.getTime()); double latitud = Double.valueOf(jTextFieldLatitud.getText()); double longitud = Double.valueOf(jTextFieldLongitud.getText()); CoordinatesConverter conversorCoordenadas = new CoordinatesConverter(); jLabel4.setText(conversorCoordenadas.decimalToDMS(latitud)); jLabel5.setText(conversorCoordenadas.decimalToDMS(longitud)); //Cambia los siguientes valores por las direciones de origen y destino deseadas String origen = jTextFieldLatitud.getText() + "," + jTextFieldLongitud.getText(); String destino = jTextFieldDestino.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 = jLabelMapaRuta; //Indica aqu el tamao que desees para el mapa int tamHorizontal = 300; int tamVertical = 270; //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"); } jToggleButtonBanderaVerde.setEnabled(false); }//GEN-LAST:event_jToggleButtonBanderaVerdeActionPerformed private void jTextFieldLatitudActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldLatitudActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextFieldLatitudActionPerformed private void jTextFieldLongitudActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldLongitudActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextFieldLongitudActionPerformed private void jToggleButtonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonStopActionPerformed jToggleButtonBanderaVerde.setEnabled(true); DecimalFormat formatoTicket = new DecimalFormat("00000"); contador++; String formatoContador = formatoTicket.format(contador); fechaHoraFin = Calendar.getInstance(); long milisegundosSalida = fechaHoraSalida.getTimeInMillis(); long milisegundosFin = fechaHoraFin.getTimeInMillis(); long duracionTrayectoMilis = milisegundosFin - milisegundosSalida; String formatoMilis = DurationFormatUtils.formatDuration(duracionTrayectoMilis, "HH:mm:ss"); NumberFormat formatoMoneda = NumberFormat.getCurrencyInstance(); double duracionTrayectoEnSeg = (duracionTrayectoMilis / 1000); double importe = (duracionTrayectoEnSeg / 60) * TARIFAXMINUTO; double importeIva = importe * IVA; double importeTotal = importe + importeIva; jTextArea1.setText("TICKET\n"); jTextArea1.append("======\n"); jTextArea1.append("Nticket: " + formatoContador); jTextArea1.append("\nFecha:" + formatoFecha.format(fechaHoraSalida.getTime())); jTextArea1.append("\n\n"); jTextArea1.append("Hora bajada de bandera: " + formatoHora.format(fechaHoraSalida.getTime())); jTextArea1.append("\nHora fin de trayecto: " + formatoHora.format(fechaHoraFin.getTime())); jTextArea1.append("\nDuracin de trayecto: " + formatoMilis); jTextArea1.append("\nTarifa por minuto: " + formatoMoneda.format(TARIFAXMINUTO)); jTextArea1.append("\n\n"); jTextArea1.append("Importe: " + formatoMoneda.format(importe)); jTextArea1.append("\nIVA: " + formatoMoneda.format(importeIva)); jTextArea1.append("\nImporte total: " + formatoMoneda.format(importeTotal)); jToggleButtonStop.setEnabled(false); }//GEN-LAST:event_jToggleButtonStopActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String numTarjeta = jTextFieldTarjeta.getText(); CreditCardValidator validadorTarjeta = new CreditCardValidator(); boolean tarjetaValida = validadorTarjeta.isValid(numTarjeta); String mensaje = (tarjetaValida == true) ? "Tarjeta vlida" : "Tarjeta no vlida"; JOptionPane.showMessageDialog(null, mensaje); }//GEN-LAST:event_jButton1ActionPerformed /** * @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JLabel jLabelMapaRuta; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextFieldDestino; private javax.swing.JTextField jTextFieldLatitud; private javax.swing.JTextField jTextFieldLongitud; private javax.swing.JTextField jTextFieldTarjeta; private javax.swing.JToggleButton jToggleButtonBanderaVerde; private javax.swing.JToggleButton jToggleButtonStop; // End of variables declaration//GEN-END:variables }