cdiego

Dibujar Puntos y Líneas Java

Mar 24th, 2012
4,534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. package javaapplication2;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.util.ArrayList;
  6.  
  7. /**
  8.  *
  9.  * AUTOR = http://www.youtube.com/user/0mercenario0
  10.  */
  11. public class PuntosLineas2 extends javax.swing.JFrame {
  12.  
  13.     /** Creates new form PuntosLineas2 */
  14.     public PuntosLineas2() {
  15.         initComponents();
  16.     }
  17.  
  18.     @SuppressWarnings("unchecked")
  19.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  20.     private void initComponents() {
  21.  
  22.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  23.         addMouseListener(new java.awt.event.MouseAdapter() {
  24.             public void mousePressed(java.awt.event.MouseEvent evt) {
  25.                 formMousePressed(evt);
  26.             }
  27.         });
  28.  
  29.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  30.         getContentPane().setLayout(layout);
  31.         layout.setHorizontalGroup(
  32.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  33.             .addGap(0, 400, Short.MAX_VALUE)
  34.         );
  35.         layout.setVerticalGroup(
  36.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  37.             .addGap(0, 300, Short.MAX_VALUE)
  38.         );
  39.  
  40.         pack();
  41.     }// </editor-fold>
  42.     //VARIABLE PARA ALMACENAR PUNTOS
  43.     ArrayList<Point> lista = new ArrayList<Point>();
  44. private void formMousePressed(java.awt.event.MouseEvent evt) {
  45.     //*************DIBUJAR PUNTOS
  46.     //CREAR LIENZO
  47.     Graphics g = this.getGraphics();
  48.     //DIBUJAR PUNTO
  49.     g.drawRect(evt.getX(), evt.getY(), 5, 5);
  50.     //*************DIBUJAR LINEA
  51.    
  52.     //GUARDAR PUNTOS
  53.     lista.add(new Point(evt.getX(), evt.getY()));
  54.     //OBTENER PUNTOS
  55.     if(lista.size()>1)
  56.     {
  57.     //DIBUJAR LINEA
  58.         g.drawLine(lista.get(lista.size()-1).x, lista.get(lista.size()-1).y, lista.get(lista.size()-2).x, lista.get(lista.size()-2).y);
  59.     }
  60.    
  61. }
  62.  
  63.     /**
  64.      * @param args the command line arguments
  65.      */
  66.     public static void main(String args[]) {
  67.         /* Set the Nimbus look and feel */
  68.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  69.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  70.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  71.          */
  72.         try {
  73.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  74.                 if ("Nimbus".equals(info.getName())) {
  75.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  76.                     break;
  77.                 }
  78.             }
  79.         } catch (ClassNotFoundException ex) {
  80.             java.util.logging.Logger.getLogger(PuntosLineas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  81.         } catch (InstantiationException ex) {
  82.             java.util.logging.Logger.getLogger(PuntosLineas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  83.         } catch (IllegalAccessException ex) {
  84.             java.util.logging.Logger.getLogger(PuntosLineas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  85.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  86.             java.util.logging.Logger.getLogger(PuntosLineas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  87.         }
  88.         //</editor-fold>
  89.  
  90.         /* Create and display the form */
  91.         java.awt.EventQueue.invokeLater(new Runnable() {
  92.  
  93.             public void run() {
  94.                 new PuntosLineas2().setVisible(true);
  95.             }
  96.         });
  97.     }
  98.     // Variables declaration - do not modify
  99.     // End of variables declaration
  100. }
Advertisement
Add Comment
Please, Sign In to add comment