thieumao

Drawing Panel (Vẽ đường thẳng)

Jun 13th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package drawingpanel;
  2.  
  3. /**
  4.  *
  5.  * @author thieumao
  6.  */
  7. import java.awt.*;
  8. import java.util.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11.  
  12. public class DrawingPanel extends JPanel implements MouseListener {
  13.  
  14.     int x1, x2, y1, y2;
  15.     boolean draw = false;
  16.  
  17.     public static void main(String[] args) {
  18.         JFrame frame = new JFrame();
  19.         frame.add(new DrawingPanel());
  20.         frame.setSize(400, 400);
  21.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         frame.setVisible(true);
  23.     }
  24.  
  25.     public DrawingPanel() {
  26.         super();
  27.         addMouseListener(this);
  28.     }
  29.  
  30.     public void mouseClicked(MouseEvent e) {
  31.     }
  32.  
  33.     public void mouseEntered(MouseEvent e) {
  34.     }
  35.  
  36.     public void mouseExited(MouseEvent e) {
  37.     }
  38.  
  39.     public void mouseReleased(MouseEvent e) {
  40.         if (draw) {
  41.             x2 = e.getX();
  42.             y2 = e.getY();
  43.             getGraphics().drawLine(x1, y1, x2, y2);
  44.         } else {
  45.             x1 = e.getX();
  46.             y1 = e.getY();
  47.         }
  48.         draw = !draw;
  49.     }
  50.  
  51.     public void mousePressed(MouseEvent e) {
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment