Advertisement
Guest User

Untitled

a guest
May 21st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package vk56;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10.  
  11. /**
  12.  *
  13.  * @author PajNtxhee
  14.  */
  15. public class NewJFrame extends javax.swing.JFrame {
  16.  int x1, x2, y2, y1;
  17.  Color c;
  18.  /**
  19.   * Creates new form NewJFrame
  20.   */
  21.  public NewJFrame() {
  22.   initComponents();
  23.  
  24.  }
  25.  
  26.  /**
  27.   * This method is called from within the constructor to initialize the form.
  28.   * WARNING: Do NOT modify this code. The content of this method is always
  29.   * regenerated by the Form Editor.
  30.   */
  31.  @SuppressWarnings("unchecked")
  32.   // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  33.  private void initComponents() {
  34.  
  35.    buttonGroup1 = new javax.swing.ButtonGroup();
  36.    jPanel1 = new javax.swing.JPanel();
  37.    jRadioButton1 = new javax.swing.JRadioButton();
  38.    jRadioButton2 = new javax.swing.JRadioButton();
  39.    jButton1 = new javax.swing.JButton();
  40.  
  41.    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  42.    setTitle("Vẽ hình bằng chuột");
  43.  
  44.    jPanel1.setBackground(java.awt.Color.white);
  45.    jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
  46.     public void mousePressed(java.awt.event.MouseEvent evt) {
  47.      jPanel1MousePressed(evt);
  48.     }
  49.     public void mouseReleased(java.awt.event.MouseEvent evt) {
  50.      jPanel1MouseReleased(evt);
  51.     }
  52.    });
  53.  
  54.    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  55.    jPanel1.setLayout(jPanel1Layout);
  56.    jPanel1Layout.setHorizontalGroup(
  57.     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  58.     .addGap(0, 425, Short.MAX_VALUE)
  59.    );
  60.    jPanel1Layout.setVerticalGroup(
  61.     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  62.     .addGap(0, 0, Short.MAX_VALUE)
  63.    );
  64.  
  65.    buttonGroup1.add(jRadioButton1);
  66.    jRadioButton1.setText("Đường thẳng");
  67.    jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
  68.     public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.      jRadioButton1ActionPerformed(evt);
  70.     }
  71.    });
  72.  
  73.    buttonGroup1.add(jRadioButton2);
  74.    jRadioButton2.setText("Hình chữ nhật");
  75.    jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
  76.     public void actionPerformed(java.awt.event.ActionEvent evt) {
  77.      jRadioButton2ActionPerformed(evt);
  78.     }
  79.    });
  80.  
  81.    jButton1.setText("Màu viền");
  82.    jButton1.addActionListener(new java.awt.event.ActionListener() {
  83.     public void actionPerformed(java.awt.event.ActionEvent evt) {
  84.      jButton1ActionPerformed(evt);
  85.     }
  86.    });
  87.  
  88.    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  89.    getContentPane().setLayout(layout);
  90.    layout.setHorizontalGroup(
  91.     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92.     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  93.      .addGap(0, 19, Short.MAX_VALUE)
  94.      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.       .addComponent(jButton1)
  96.       .addGroup(layout.createSequentialGroup()
  97.        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  98.         .addComponent(jRadioButton1, javax.swing.GroupLayout.Alignment.TRAILING)
  99.         .addComponent(jRadioButton2, javax.swing.GroupLayout.Alignment.TRAILING))
  100.        .addGap(18, 18, 18)
  101.        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
  102.    );
  103.    layout.setVerticalGroup(
  104.     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  105.     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  106.     .addGroup(layout.createSequentialGroup()
  107.      .addGap(51, 51, 51)
  108.      .addComponent(jRadioButton1)
  109.      .addGap(70, 70, 70)
  110.      .addComponent(jRadioButton2)
  111.      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
  112.      .addComponent(jButton1)
  113.      .addGap(22, 22, 22))
  114.    );
  115.  
  116.    pack();
  117.   } // </editor-fold>                        
  118.  public void dt(Graphics g) {
  119.   g.setColor(c);
  120.   g.drawLine(x1, y1, x2, y2);
  121.  }
  122.  
  123.  public void hcn(Graphics g) {
  124.   g.setColor(c);
  125.   g.drawRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
  126.   g.setColor(Color.pink);
  127.   g.fillRect(x1, y1, Math.abs(x2 - x1), Math.abs(y2 - y1));
  128.  }
  129.  private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  130.   // TODO add your handling code here:
  131.   dt(jPanel1.getGraphics());
  132.  }
  133.  
  134.  private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  135.   // TODO add your handling code here:
  136.   hcn(jPanel1.getGraphics());
  137.  }
  138.  
  139.  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  140.   // TODO add your handling code here:
  141.   c = Color.red;
  142.  }
  143.  
  144.  private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {
  145.   // TODO add your handling code here:
  146.   x1 = evt.getX();
  147.   y1 = evt.getY();
  148.  }
  149.  
  150.  private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) {
  151.   // TODO add your handling code here:
  152.   x2 = evt.getX();
  153.   y2 = evt.getY();
  154.  }
  155.  
  156.  /**
  157.   * @param args the command line arguments
  158.   */
  159.  public static void main(String args[]) {
  160.   /* Set the Nimbus look and feel */
  161.   //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  162.   /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  163.    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  164.    */
  165.   try {
  166.    for (javax.swing.UIManager.LookAndFeelInfo info: javax.swing.UIManager.getInstalledLookAndFeels()) {
  167.     if ("Nimbus".equals(info.getName())) {
  168.      javax.swing.UIManager.setLookAndFeel(info.getClassName());
  169.      break;
  170.     }
  171.    }
  172.   } catch (ClassNotFoundException ex) {
  173.    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  174.   } catch (InstantiationException ex) {
  175.    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  176.   } catch (IllegalAccessException ex) {
  177.    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  178.   } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  179.    java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  180.   }
  181.   //</editor-fold>
  182.  
  183.   /* Create and display the form */
  184.   java.awt.EventQueue.invokeLater(new Runnable() {
  185.    public void run() {
  186.     new NewJFrame().setVisible(true);
  187.    }
  188.   });
  189.  }
  190.  
  191.  // Variables declaration - do not modify                    
  192.  private javax.swing.ButtonGroup buttonGroup1;
  193.  private javax.swing.JButton jButton1;
  194.  private javax.swing.JPanel jPanel1;
  195.  private javax.swing.JRadioButton jRadioButton1;
  196.  private javax.swing.JRadioButton jRadioButton2;
  197.  // End of variables declaration                  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement