Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package drawingpanel;
- /**
- *
- * @author thieumao
- */
- import java.awt.*;
- import java.util.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class DrawingPanel extends JPanel implements MouseListener {
- int x1, x2, y1, y2;
- boolean draw = false;
- public static void main(String[] args) {
- JFrame frame = new JFrame();
- frame.add(new DrawingPanel());
- frame.setSize(400, 400);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- public DrawingPanel() {
- super();
- addMouseListener(this);
- }
- public void mouseClicked(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- if (draw) {
- x2 = e.getX();
- y2 = e.getY();
- getGraphics().drawLine(x1, y1, x2, y2);
- } else {
- x1 = e.getX();
- y1 = e.getY();
- }
- draw = !draw;
- }
- public void mousePressed(MouseEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment