Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////FORM DIBUJAR////////////////////////////
  2. public final class Dibujar extends javax.swing.JFrame {
  3.  
  4.  
  5.     private Connection con;
  6.     private Statement st;
  7.     private ResultSet rs;
  8.     private boolean pintar = false;
  9.     private int x1, y1, d1, d2;
  10.     private int fig;
  11.     private ArrayList<Figura> array = new ArrayList<>();
  12.     public Dibujar() throws IOException {
  13.  
  14.      
  15.         this.getContentPane().setBackground(Color.BLACK);
  16.         initComponents();
  17.     }
  18.  
  19.     @SuppressWarnings("unchecked")
  20.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  21.     private void initComponents() {
  22.  
  23.         btnGuardar = new javax.swing.JButton();
  24.         btnCargar = new javax.swing.JButton();
  25.         btnLinea = new javax.swing.JButton();
  26.         btnRectangulo = new javax.swing.JButton();
  27.         btnCirculo = new javax.swing.JButton();
  28.  
  29.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  30.         addMouseListener(new java.awt.event.MouseAdapter() {
  31.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  32.                 formMouseClicked(evt);
  33.             }
  34.         });
  35.  
  36.         btnGuardar.setText("Guardar");
  37.         btnGuardar.addActionListener(new java.awt.event.ActionListener() {
  38.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  39.                 btnGuardarActionPerformed(evt);
  40.             }
  41.         });
  42.  
  43.         btnCargar.setText("Cargar");
  44.         btnCargar.addActionListener(new java.awt.event.ActionListener() {
  45.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  46.                 btnCargarActionPerformed(evt);
  47.             }
  48.         });
  49.  
  50.         btnLinea.setBackground(new java.awt.Color(0, 204, 51));
  51.         btnLinea.setFont(new java.awt.Font("AR CHRISTY", 0, 12)); // NOI18N
  52.         btnLinea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proyecto/u3/linea.png"))); // NOI18N
  53.         btnLinea.setText("Lineas");
  54.         btnLinea.addActionListener(new java.awt.event.ActionListener() {
  55.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  56.                 btnLineaActionPerformed(evt);
  57.             }
  58.         });
  59.  
  60.         btnRectangulo.setBackground(new java.awt.Color(255, 102, 51));
  61.         btnRectangulo.setFont(new java.awt.Font("AR CHRISTY", 0, 12)); // NOI18N
  62.         btnRectangulo.setIcon(new javax.swing.ImageIcon("C:\\Users\\Naieli\\Documents\\SEMESTRE 5\\TAP\\U1\\figuras\\figuras\\cuadrado-rojo-icono-8697-96.png")); // NOI18N
  63.         btnRectangulo.addActionListener(new java.awt.event.ActionListener() {
  64.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  65.                 btnRectanguloActionPerformed(evt);
  66.             }
  67.         });
  68.  
  69.         btnCirculo.setBackground(new java.awt.Color(51, 51, 255));
  70.         btnCirculo.setFont(new java.awt.Font("AR CHRISTY", 0, 12)); // NOI18N
  71.         btnCirculo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/proyecto/u3/circulo-verde-icono-4055-128.png"))); // NOI18N
  72.         btnCirculo.setText("Circulos");
  73.         btnCirculo.addActionListener(new java.awt.event.ActionListener() {
  74.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  75.                 btnCirculoActionPerformed(evt);
  76.             }
  77.         });
  78.  
  79.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  80.         getContentPane().setLayout(layout);
  81.         layout.setHorizontalGroup(
  82.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83.             .addGroup(layout.createSequentialGroup()
  84.                 .addGap(30, 30, 30)
  85.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  86.                     .addGroup(layout.createSequentialGroup()
  87.                         .addComponent(btnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
  88.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  89.                         .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
  90.                         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  91.                     .addGroup(layout.createSequentialGroup()
  92.                         .addComponent(btnLinea, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
  93.                         .addGap(28, 28, 28)
  94.                         .addComponent(btnRectangulo, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
  95.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
  96.                         .addComponent(btnCirculo, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
  97.                         .addGap(19, 19, 19))))
  98.         );
  99.         layout.setVerticalGroup(
  100.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  101.             .addGroup(layout.createSequentialGroup()
  102.                 .addContainerGap()
  103.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  104.                     .addComponent(btnLinea, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
  105.                     .addComponent(btnRectangulo, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
  106.                     .addComponent(btnCirculo, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
  107.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 359, Short.MAX_VALUE)
  108.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  109.                     .addComponent(btnCargar, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
  110.                     .addComponent(btnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  111.                 .addGap(29, 29, 29))
  112.         );
  113.  
  114.         pack();
  115.     }// </editor-fold>                        
  116.  
  117.     private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
  118.         // TODO add your handling code here:
  119.         Graphics pen = this.getGraphics();
  120.         pen.setColor(Color.CYAN);
  121.         Figura aux = new Figura();
  122.  
  123.         if (!pintar) {
  124.             pintar = true;
  125.             x1 = evt.getX();
  126.             y1 = evt.getY();
  127.         } else {
  128.             if (fig == 1) {
  129.                 d1 = Math.abs(evt.getX() - x1);
  130.                 d2 = Math.abs(evt.getY() - y1);
  131.                 equis(evt.getX(), evt.getY());
  132.                 aux.setNombre("Rectangulo");
  133.                 aux.setX1(x1);
  134.                 aux.setY1(y1);
  135.                 aux.setD1(d1);
  136.                 aux.setD2(d2);
  137.                 pen.drawRect(x1, y1, d1, d2);
  138.                 array.add(aux);
  139.                 aux=new Figura();
  140.             } else if (fig == 2) {
  141.                 aux.setNombre("Linea");
  142.                 aux.setX1(x1);
  143.                 aux.setY1(y1);
  144.                 aux.setD1(evt.getX());
  145.                 aux.setD2(evt.getY());
  146.                 array.add(aux);
  147.                 pen.drawLine(x1, y1, evt.getX(), evt.getY());
  148.                 aux=new Figura();
  149.             } else if (fig == 3) {
  150.                 d1 = (int) Math.sqrt(Math.pow((x1 - evt.getX()), 2) + Math.pow((y1 - evt.getY()), 2));
  151.                 aux.setNombre("Circulo");
  152.                 aux.setX1(x1 - (int) (d1 / 2));
  153.                 aux.setY1(y1 - (int) (d1 / 2));
  154.                 aux.setD1(d1);
  155.                 aux.setD2(d1);
  156.                 array.add(aux);
  157.                 pen.drawOval((x1 - (int) (d1 / 2)), (y1 - (int) (d1 / 2)), d1, d1);
  158.                 aux=new Figura();
  159.             }
  160.             pintar = false;
  161.            
  162.         }
  163.     }                                
  164.  
  165.     private void btnCirculoActionPerformed(java.awt.event.ActionEvent evt) {                                          
  166.         // TODO add your handling code here:
  167.         this.fig = 3;
  168.     }                                          
  169.  
  170.     private void btnRectanguloActionPerformed(java.awt.event.ActionEvent evt) {                                              
  171.         // TODO add your handling code here:
  172.         this.fig = 1;
  173.     }                                            
  174.    
  175.     private void btnLineaActionPerformed(java.awt.event.ActionEvent evt) {                                        
  176.         // TODO add your handling code here:
  177.         this.fig = 2;
  178.     }                                        
  179.  
  180.     private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  181.  try{
  182.            
  183.    Class.forName("com.mysql.jdbc.Driver");
  184. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
  185. st=con.createStatement();
  186. for(int i=0; i<array.size();i++){
  187. String query =String.format("INSERT INTO dibujos(Nombre,x1,y1,x2,y2) VALUES('%s',%d,%d,%d,%d);",array.get(i).getNombre(),array.get(i).getX1(),array.get(i).getY1(),array.get(i).getD1(),array.get(i).getD2());
  188.     System.out.println(query);
  189.   st.executeUpdate(query);
  190. }
  191.    
  192.  
  193.  
  194. }catch(Exception e){
  195.     System.out.println("error: "+e);
  196. }        
  197.     }                                          
  198.  
  199.     private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  200.         // TODO add your handling code here:g.setColor(Color.red);
  201.         array.clear();
  202.         try{
  203.            
  204.    Class.forName("com.mysql.jdbc.Driver");
  205. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
  206. st=con.createStatement();
  207.  
  208. String query ="SELECT * FROM dibujos";
  209. rs = st.executeQuery(query);
  210.     System.out.println("Contenido");
  211. while(rs.next()){
  212. String Nombre = rs.getString("Nombre");
  213. int xx1 = rs.getInt("x1");
  214. int yy1 = rs.getInt("y1");
  215. int xx2 = rs.getInt("x2");
  216. int yy2 = rs.getInt("y2");
  217. array.add(new Figura(Nombre,xx1,yy1,xx2,yy2));
  218.  
  219. }
  220. }catch(Exception e){
  221.     System.out.println("error");
  222. }
  223.         Graphics g=this.getGraphics();
  224.         g.setColor(Color.PINK);
  225.         for(int c=0;c<array.size();c++){
  226.             System.out.println(array.get(c).getTodo());
  227.             if(array.get(c).getNombre().equalsIgnoreCase("Rectangulo")){
  228.                 g.drawRect(array.get(c).getX1(), array.get(c).getY1(), array.get(c).getD1(), array.get(c).getD2());
  229.             }
  230.             else if(array.get(c).getNombre().equalsIgnoreCase("Linea")){
  231.             g.drawLine(array.get(c).getX1(), array.get(c).getY1(), array.get(c).getD1(), array.get(c).getD2());
  232.             }
  233.             else if(array.get(c).getNombre().equalsIgnoreCase("Circulo")){
  234.             g.drawOval(array.get(c).getX1(), array.get(c).getY1(), array.get(c).getD1(), array.get(c).getD2());
  235.             }
  236.            
  237.         }
  238.     }                                        
  239.     private void equis(int x2, int y2) {
  240.         if (x1 > x2 && y1 > y2) {
  241.             x1 = x2;
  242.             y1 = y2;
  243.         } else if (x1 < x2 && y1 > y2) {
  244.             y1 = y2;
  245.         } else if (x1 > x2 && y1 < y2) {
  246.             x1 = x2;
  247.         }
  248.  
  249.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement