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 reportesedd; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.RequestBody; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.Timer; import static reportesedd.ReportesEDD.rb; /** * * @author KMMG */ public class Reportes extends javax.swing.JFrame { /** * Creates new form Reportes */ String aUsuarios; String aEmpresas; String aDeptos; static RequestBody rb; public Reportes(String usuarios, String empresas, String deptos) { initComponents(); this.setLocationRelativeTo(null); //AQUI HAY QUE ESPLITEAR LOS STRINGS Y CARGARLOS A LOS COMBOBOX cargarCombosUsuarios(usuarios); cargarCombosEmpresas(empresas); cargarCombosDeptos(deptos); aUsuarios = usuarios; aEmpresas = empresas; aDeptos = deptos; Timer t = new Timer(3000, new ActionListener() { public void actionPerformed(ActionEvent e) { rb = new FormEncodingBuilder().add("p", "p").build();//agregar parametros String usuarios = Conexion.consultarConString("listarUsuarios", rb); rb = new FormEncodingBuilder().add("p", "p").build();//agregar parametros String empresas = Conexion.consultarConString("empresas", rb); rb = new FormEncodingBuilder().add("p", "p").build();//agregar parametros String deptos = Conexion.consultarConString("departamentos", rb); if (!usuarios.equals(aUsuarios)) { jComboBox1.removeAllItems(); cargarCombosUsuarios(usuarios); aUsuarios = usuarios; } if (!empresas.equals(aEmpresas)) { jComboBox2.removeAllItems(); aEmpresas = empresas; cargarCombosEmpresas(empresas); } if (!deptos.equals(aDeptos)) { jComboBox3.removeAllItems(); aDeptos = deptos; cargarCombosDeptos(deptos); } } }); t.start(); //String envio="1"; //Conexion.Conection_Llenar("cadena=135465X,ASDASD,ASDASD,ASDSD,SFSDF,SDFDFS,TYRT"); } private void cargarCombosUsuarios(String usuarios) { String user[]; try { System.out.println(usuarios); user = usuarios.split(","); for (int i = 0; i <= user.length - 1; i++) { jComboBox1.addItem(user[i]); } } catch (Exception ex) { jComboBox1.addItem(usuarios); } } private void cargarCombosEmpresas(String empresas) { try { String empresa[] = empresas.split(","); for (int i = 0; i < empresa.length; i++) { jComboBox2.addItem(empresa[i]); } } catch (Exception ex) { jComboBox2.addItem(empresas); } } private void cargarCombosDeptos(String deptos) { try { String depto[] = deptos.split(","); for (int i = 0; i < depto.length; i++) { jComboBox3.addItem(depto[i]); } } catch (Exception ex) { jComboBox3.addItem(deptos); } } Visor v; /** * 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(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton4 = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox<>(); jComboBox2 = new javax.swing.JComboBox<>(); jComboBox3 = new javax.swing.JComboBox<>(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jButton5 = new javax.swing.JButton(); jButton6 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jButton1.setText("Matriz Dispersa"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Activos de una Empresa"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Activos de un Departamento"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jLabel1.setText("Nombre de una Empresa:"); jLabel2.setText("Nombre de un Departamento:"); jButton4.setText("Arbol B"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(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().addGap(34, 34, 34).addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jLabel2) .addComponent(jLabel1) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextField1).addComponent(jTextField2).addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(43, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(25, 25, 25).addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton4).addGap(25, 25, 25).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE) .addComponent(jLabel2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3).addGap(53, 53, 53))); jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel3.setText("Usuarios:"); jLabel4.setText("Empresas:"); jLabel5.setText("Departamento"); jButton5.setText("AVL de Usuario"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); jButton6.setText("Activos Rentados por Usuario"); jButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton6ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(56, 56, 56).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)).addGap(30, 30, 30) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addGap(27, 27, 27) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5).addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addGap(95, 95, 95) .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(87, 87, 87)))) .addGroup(jPanel2Layout.createSequentialGroup().addGap(142, 142, 142).addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(66, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(115, 115, 115) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(jLabel4).addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18).addComponent(jButton5).addGap(18, 18, 18).addComponent(jButton6) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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( javax.swing.GroupLayout.Alignment.TRAILING, 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.UNRELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed v = new Visor(); RequestBody rb = new FormEncodingBuilder().add("p", "algo").build();//agregar parametros InputStream is = Conexion.consultar("graficarMatriz", rb); try { FileOutputStream fos = new FileOutputStream("matriz.jpg"); byte[] array = new byte[1000]; int leido = is.read(array); while (leido > 0) { fos.write(array, 0, leido); leido = is.read(array); } is.close(); fos.close(); String ruta = new File("").getAbsolutePath(); ruta = ruta + "\\matriz.jpg"; ImageIcon image = new ImageIcon(ruta); Icon icono = new ImageIcon(image.getImage().getScaledInstance(image.getIconWidth(), image.getIconHeight(), Image.SCALE_DEFAULT)); v.jLabel1.setText(null); v.jLabel1.setIcon(icono); v.ima = icono; } catch (IOException ex) { } v.setTitle("Matriz Dispersa"); v.setVisible(true); }//GEN-LAST:event_jButton1ActionPerformed public BufferedImage ConvertirTxtPng(String txt) { BufferedImage bufferedImage = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.yellow); g2.fillOval(0, 0, this.getWidth(), this.getHeight()); g2.setColor(Color.black); g2.drawString(txt, this.getWidth() / 2, this.getHeight() / 2); g2.dispose(); return bufferedImage; } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed v = new Visor(); String empresa = jTextField1.getText(); RequestBody rb = new FormEncodingBuilder().add("empresa", empresa).build();//agregar parametros InputStream is = Conexion.consultar("reportePorEmpresa", rb); //System.out.println(res); try { FileOutputStream fos = new FileOutputStream("activosEMP.jpg"); byte[] array = new byte[1000]; int leido = is.read(array); while (leido > 0) { fos.write(array, 0, leido); leido = is.read(array); } is.close(); fos.close(); String ruta = new File("").getAbsolutePath(); ruta = ruta + "\\activosEMP.jpg"; ImageIcon image = new ImageIcon(ruta); Icon icono = new ImageIcon(image.getImage().getScaledInstance(image.getIconWidth(), image.getIconHeight(), Image.SCALE_DEFAULT)); v.jLabel1.setText(null); v.jLabel1.setIcon(icono); } catch (IOException ex) { } v.setTitle("Activos de Empresa " + empresa); v.setVisible(true); }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed v = new Visor(); String depto = jTextField2.getText(); RequestBody rb = new FormEncodingBuilder().add("departamento", depto).build();//agregar parametros InputStream is = Conexion.consultar("reportePorDepartamento", rb); //System.out.println(res); try { FileOutputStream fos = new FileOutputStream("activosDEP.jpg"); byte[] array = new byte[1000]; int leido = is.read(array); while (leido > 0) { fos.write(array, 0, leido); leido = is.read(array); } is.close(); fos.close(); String ruta = new File("").getAbsolutePath(); ruta = ruta + "\\activosDEP.jpg"; ImageIcon image = new ImageIcon(ruta); Icon icono = new ImageIcon(image.getImage().getScaledInstance(image.getIconWidth(), image.getIconHeight(), Image.SCALE_DEFAULT)); v.jLabel1.setText(null); v.jLabel1.setIcon(icono); } catch (IOException ex) { } v.setTitle("Activos de Departamento " + depto); v.setVisible(true); }//GEN-LAST:event_jButton3ActionPerformed private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed try { // TODO add your handling code here: Conexion.Conection_API("imagen=1"); v = new Visor(); URL url = new URL("http://192.168.43.122:58402/Arbol_B.png"); InputStream in = new BufferedInputStream(url.openStream()); OutputStream out = new BufferedOutputStream(new FileOutputStream("arbol.png")); for (int i; (i = in.read()) != -1;) { out.write(i); } in.close(); out.close(); String ruta = new File("").getAbsolutePath(); ruta = ruta + "\\arbol.png"; ImageIcon image = new ImageIcon(ruta); Icon icono = new ImageIcon(image.getImage().getScaledInstance(image.getIconWidth(), image.getIconHeight(), Image.SCALE_DEFAULT)); v.jLabel1.setText(null); v.jLabel1.setIcon(icono); v.setVisible(true); v.setTitle("Arbol B"); } catch (MalformedURLException ex) { Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Reportes.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jButton4ActionPerformed private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed v = new Visor(); String usuario = this.jComboBox1.getSelectedItem().toString(); String empresa = this.jComboBox2.getSelectedItem().toString(); String depto = this.jComboBox3.getSelectedItem().toString(); RequestBody rb = new FormEncodingBuilder().add("usuario", usuario).add("empresa", empresa) .add("departamento", depto).build();//agregar parametros InputStream is = Conexion.consultar("graficarAVL", rb); try { FileOutputStream fos = new FileOutputStream("avl.jpg"); byte[] array = new byte[1000]; int leido = is.read(array); while (leido > 0) { fos.write(array, 0, leido); leido = is.read(array); } is.close(); fos.close(); String ruta = new File("").getAbsolutePath(); ruta = ruta + "\\avl.jpg"; ImageIcon image = new ImageIcon(ruta); Icon icono = new ImageIcon(image.getImage().getScaledInstance(image.getIconWidth(), image.getIconHeight(), Image.SCALE_DEFAULT)); v.jLabel1.setText(null); v.jLabel1.setIcon(icono); } catch (IOException ex) { } v.setTitle("AVL"); v.setVisible(true); }//GEN-LAST:event_jButton5ActionPerformed private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed String usuario = this.jComboBox1.getSelectedItem().toString(); String empresa = this.jComboBox2.getSelectedItem().toString(); String depto = this.jComboBox3.getSelectedItem().toString(); //String res = Conexion.Conection_API("7"); }//GEN-LAST:event_jButton6ActionPerformed /** * @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(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Reportes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Reportes.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 Reportes().setVisible(true); } }); }*/ // Variables declaration - do not modify//GEN-BEGIN:variables 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.JButton jButton6; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JComboBox<String> jComboBox2; private javax.swing.JComboBox<String> jComboBox3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration//GEN-END:variables }