event

Move and resize objects using Mouse Events

With this tutorial we are going to see how to handle moving and re sizing events in a Java Desktop application. You might find this useful when you want to add rich mouse functionality to the application. For example it’s very cool to let the users resize a window using the mouse wheel.

In short in order to move and resize objects using mouse events, one has to perform the following steps:

  • Create a class that extends MouseAdapter
  • Override the mousePressed method to customize the handling of that specific event. Now every time you press a button this method will be launched.
  • Use MouseEvent.getX, MouseEvent.getY methods to get the coordinates of the position the mouse pointer had when the mouse event occurred.
  • Override the mouseDragged method to customize the handling of that specific event. Now every time you drag an object, this method will be launched.
  • Use MouseEvent.getX, MouseEvent.getY methods to get the new coordinates of the dragged object. Then call repaint() to repaint the object to the new position.
  • Create a class that implements MouseWheelListener.
  • Override mouseWheelMoved method to customize the handling of that specific event. Now every time the user uses the mouse wheel this method will fire up. You can then use MouseWheelEvent.WHEEL_UNIT_SCROLL to get the scroll units amount and resize the window accordingly.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

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 Resize extends JPanel {

    private Rectangle2D.Float myRect = new Rectangle2D.Float(90, 90, 90, 90);
    BindMouseMove movingAdapt = new BindMouseMove();

    public Resize() {

  addMouseMotionListener(movingAdapt);

  addMouseListener(movingAdapt);

  addMouseWheelListener(new ResizeHandler());
    }

    @Override
    public void paint(Graphics graphics) {

  super.paint(graphics);

  Graphics2D graphics2d = (Graphics2D) graphics;

  graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

  graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

  graphics2d.setColor(new Color(0, 0, 200));

  graphics2d.fill(myRect);
    }

    class BindMouseMove extends MouseAdapter {

  private int x;

  private int y;

  @Override

  public void mousePressed(MouseEvent event) {

x = event.getX();

y = event.getY();

  }

  @Override

  public void mouseDragged(MouseEvent event) {

int dx = event.getX() - x;

int dy = event.getY() - y;

if (myRect.getBounds2D().contains(x, y)) {

    myRect.x += dx;

    myRect.y += dy;

    repaint();

}

x += dx;

y += dy;

  }
    }

    class ResizeHandler implements MouseWheelListener {

  @Override

  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 jFrame = new JFrame("Moving and Scaling");

  Resize resi = new Resize();

  resi.setDoubleBuffered(true);

  jFrame.add(resi);

  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  jFrame.setSize(300, 300);

  jFrame.setLocationRelativeTo(null);

  jFrame.setVisible(true);
    }
}

 
This was an example on how to move and resize objects using Mouse Events

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button