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 eddpro1graficas; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; import java.io.FileWriter; import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; /** * * @author lMMl */ public class VentanaMatriz extends javax.swing.JFrame { public static OkHttpClient webClient = new OkHttpClient(); /** * Creates new form Principal */ public VentanaMatriz() { 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(); BotonVer = new javax.swing.JButton(); setAutoRequestFocus(false); setBackground(new java.awt.Color(255, 255, 255)); setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR)); jLabel1.setBackground(new java.awt.Color(153, 153, 153)); jLabel1.setFont(new java.awt.Font("Courier New", 1, 14)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 102, 0)); jLabel1.setText("Matriz Dispersa"); jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR)); jLabel1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jLabel1MouseClicked(evt); } }); BotonVer.setText("Ver"); BotonVer.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BotonVerActionPerformed(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() .addComponent(BotonVer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) .addGroup(layout.createSequentialGroup().addGap(87, 87, 87).addComponent(jLabel1) .addContainerGap(91, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(89, 89, 89).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(BotonVer) .addContainerGap(347, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked // TODO add your handling code here: this.Graficarr(this.Vermatriz()); }//GEN-LAST:event_jLabel1MouseClicked private void BotonVerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonVerActionPerformed this.Graficarr(this.Vermatriz()); }//GEN-LAST:event_BotonVerActionPerformed public String getString(String metodo, RequestBody formBody) { try { //URL url = new URL("http://0.0.0.0:5000/" + metodo); //URL url = new URL("http://192.168.0.21:5000/" + metodo); URL url = new URL("http://192.168.43.165:5000/" + metodo); Request request = new Request.Builder().url(url).post(formBody).build(); Response response = webClient.newCall(request).execute();//Aqui obtiene la respuesta en dado caso si hayas pues un return en python String response_string = response.body().string();//y este seria el string de las respuesta return response_string; } catch (MalformedURLException ex) { //java.util.logging.Logger.getLogger(testwebserver.TestWebServer.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { //java.util.logging.Logger.getLogger(testwebserver.TestWebServer.class.getName()).log(Level.SEVERE, null, ex); } return null; } private String Vermatriz() { String nombre = ""; RequestBody formBody = new FormEncodingBuilder().add("dato", nombre).build(); String r = getString("Vermatriz", formBody); return r; } private String Verletra() { String letra = ""; RequestBody formBody = new FormEncodingBuilder().add("dato", letra).build(); String r = getString("Verporletra", formBody); return r; } private String Verdomio() { String letra = ""; RequestBody formBody = new FormEncodingBuilder().add("dato", letra).build(); String r = getString("Verpordominio", formBody); return r; } private void AgregaraMatriz() { String n = ""; String b = n.substring(0, 1); String correo = ""; String letra = b; String dato = ""; RequestBody formBody = new FormEncodingBuilder().add("cor", correo).add("let", letra).add("dat", dato) .build(); String r = getString("agregaramatriz", formBody); System.out.println(r); } private void EliminarDeMatriz() { String n = ""; String b = n.substring(0, 1); String correo = ""; String letra = b; String dato = ""; RequestBody formBody = new FormEncodingBuilder().add("cor", correo).add("let", letra).add("dat", dato) .build(); String r = getString("Eliminardematriz", formBody); System.out.println(r); } public void Graficarr(String datosi) { // TODO add your handling code here: FileWriter archivo = null; PrintWriter writer; try { archivo = new FileWriter("matriz.dot"); writer = new PrintWriter(archivo); writer.print("digraph ejemplo_1 { \nnode [shape=record,width=.1,height=.1];\n" + datosi + "}"); } catch (Exception e) { System.err.println("Error escribiendo"); } finally { try { if (null != archivo) archivo.close(); } catch (Exception e2) { System.err.println("Error cerrando"); } } try { Runtime rt = Runtime.getRuntime(); Runtime rts = Runtime.getRuntime(); String[] args = { "/usr/local/bin/dot", "-Tjpg", "-o", "matriz.jpg", "matriz.dot" }; rt.exec(args); String[] demos = { "/usr/bin/open", "matriz.dot", "-a", "Graphviz" }; //rt.exec( "dot -Tjpg -o matriz.jpg matriz.dot"); rt.exec(args); rts.exec(demos); //Dormir el hilo 2 segundos } catch (Exception ex) { System.err.println("Error creando matriz.jpg"); } } public void Graficaparte(String datosi) { // TODO add your handling code here: FileWriter archivo = null; PrintWriter writer; try { archivo = new FileWriter("matriz2.dot"); writer = new PrintWriter(archivo); writer.print("digraph ejemplo_1 { \nnode [shape=record,width=.1,height=.1];\n" + datosi + "}"); } catch (Exception e) { System.err.println("Error escribiendo"); } finally { try { if (null != archivo) archivo.close(); } catch (Exception e2) { System.err.println("Error cerrando"); } } try { Runtime rt = Runtime.getRuntime(); Runtime rts = Runtime.getRuntime(); String[] args = { "/usr/local/bin/dot", "-Tjpg", "-o", "matriz.jpg", "matriz.dot" }; rt.exec(args); String[] demos = { "/usr/bin/open", "matriz2.dot", "-a", "Graphviz" }; //rt.exec( "dot -Tjpg -o matriz.jpg matriz.dot"); rt.exec(args); rts.exec(demos); //Dormir el hilo 2 segundos } catch (Exception ex) { System.err.println("Error creando matriz.jpg"); } } /** * @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(VentanaMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(VentanaMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(VentanaMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(VentanaMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new VentanaMatriz().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton BotonVer; private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables }