Mouse drag and draw : Mouse « Event « Java






Mouse drag and draw

Mouse drag and draw
   
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MouseDragActionPanel extends JPanel implements MouseMotionListener {
  private static final int SquareWidth = 10;

  private static final int Max = 100;

  private Rectangle[] squares = new Rectangle[Max];

  private int squareCount = 0;

  private int currentSquareIndex = -1;

  public MouseDragActionPanel() {
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent evt) {
        int x = evt.getX();
        int y = evt.getY();
        currentSquareIndex = getSquare(x, y);
        if (currentSquareIndex < 0) // not inside a square
          add(x, y);
      }

      public void mouseClicked(MouseEvent evt) {
        int x = evt.getX();
        int y = evt.getY();

        if (evt.getClickCount() >= 2) {
          remove(currentSquareIndex);
        }
      }
    });
    addMouseMotionListener(this);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (int i = 0; i < squareCount; i++)
      ((Graphics2D)g).draw(squares[i]);
  }

  public int getSquare(int x, int y) {
    for (int i = 0; i < squareCount; i++)
      if(squares[i].contains(x,y))
        return i;
    return -1;
  }

  public void add(int x, int y) {
    if (squareCount < Max) {
      squares[squareCount] = new Rectangle(x, y,SquareWidth,SquareWidth);
      currentSquareIndex = squareCount;
      squareCount++;
      repaint();
    }
  }

  public void remove(int n) {
    if (n < 0 || n >= squareCount)
      return;
    squareCount--;
    squares[n] = squares[squareCount];
    if (currentSquareIndex == n)
      currentSquareIndex = -1;
    repaint();
  }

  public void mouseMoved(MouseEvent evt) {
    int x = evt.getX();
    int y = evt.getY();

    if (getSquare(x, y) >= 0)
      setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    else
      setCursor(Cursor.getDefaultCursor());
  }

  public void mouseDragged(MouseEvent evt) {
    int x = evt.getX();
    int y = evt.getY();

    if (currentSquareIndex >= 0) {
      Graphics g = getGraphics();
      g.setXORMode(getBackground());
      ((Graphics2D)g).draw(squares[currentSquareIndex]);
      squares[currentSquareIndex].x = x;
      squares[currentSquareIndex].y = y;
      ((Graphics2D)g).draw(squares[currentSquareIndex]);
      g.dispose();
    }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("MouseTest");
    frame.setSize(300, 200);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new MouseDragActionPanel());

    frame.show();
  }
}
           
         
    
    
  








Related examples in the same category

1.Drags within the imageDrags within the image
2.Mouse Wheel Event DemoMouse Wheel Event Demo
3.Move Shape with mouseMove Shape with mouse
4.MouseMotion Event: mouse move and dragMouseMotion Event: mouse move and drag
5.MouseDrag -- implement simple mouse drag in a windowMouseDrag -- implement simple mouse drag in a window
6.MouseDragClip -- implement simple mouse drag in a window. Speed up by using clipping regions
7.Swing Mouse Motion Event DemoSwing Mouse Motion Event Demo
8.Mouse Event DemoMouse Event Demo
9.Handle mouse motion event
10.Handle mouse button click event?
11.Move and scale graphical objects with a mouse on the panel
12.Detecting Double and Triple Clicks
13.InputEvent.BUTTON1_MASK (for left mouse button)
14.InputEvent.BUTTON2_MASK (for middle mouse button)
15.InputEvent.BUTTON3_MASK (for right mouse button)
16.Handling Mouse Clicks
17.Handling Mouse Motion
18.extends MouseAdapter and implements MouseMotionListener to create a mouse drawing programmextends MouseAdapter and implements MouseMotionListener to create a mouse drawing programm