Move and scale graphical objects with a mouse on the panel : Mouse « Event « Java






Move and scale graphical objects with a mouse on the panel

   

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Rectangle2D;

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

public class MouseMoveScale extends JPanel {
  private Rectangle2D.Float myRect = new Rectangle2D.Float(50, 50, 50, 50);

  MovingAdapter ma = new MovingAdapter();

  public MouseMoveScale() {
    addMouseMotionListener(ma);
    addMouseListener(ma);
    addMouseWheelListener(new ScaleHandler());
  }

  public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    g2d.setColor(new Color(0, 0, 200));
    g2d.fill(myRect);
  }

  class MovingAdapter extends MouseAdapter {

    private int x;

    private int y;

    public void mousePressed(MouseEvent e) {
      x = e.getX();
      y = e.getY();
    }

    public void mouseDragged(MouseEvent e) {

      int dx = e.getX() - x;
      int dy = e.getY() - y;

      if (myRect.getBounds2D().contains(x, y)) {
        myRect.x += dx;
        myRect.y += dy;
        repaint();
      }
      x += dx;
      y += dy;
    }
  }

  class ScaleHandler implements MouseWheelListener {
    public void mouseWheelMoved(MouseWheelEvent e) {

      int x = e.getX();
      int y = e.getY();

      if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {

        if (myRect.getBounds2D().contains(x, y)) {
          float amount = e.getWheelRotation() * 5f;
          myRect.width += amount;
          myRect.height += amount;
          repaint();
        }
      }
    }
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Moving and Scaling");
    MouseMoveScale m = new MouseMoveScale();
    m.setDoubleBuffered(true);
    frame.add(m);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

   
    
    
  








Related examples in the same category

1.Drags within the imageDrags within the image
2.Mouse Wheel Event DemoMouse Wheel Event Demo
3.Mouse drag and drawMouse drag and draw
4.Move Shape with mouseMove Shape with mouse
5.MouseMotion Event: mouse move and dragMouseMotion Event: mouse move and drag
6.MouseDrag -- implement simple mouse drag in a windowMouseDrag -- implement simple mouse drag in a window
7.MouseDragClip -- implement simple mouse drag in a window. Speed up by using clipping regions
8.Swing Mouse Motion Event DemoSwing Mouse Motion Event Demo
9.Mouse Event DemoMouse Event Demo
10.Handle mouse motion event
11.Handle mouse button click event?
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