Java: Restrict the mouse movement to vertical - java

Is there a way to restrict the mouse movement to vertical? I am currently developing a program when user dragging mouse pointer to the left or right in specific rectangle shape cursor can't move but up and down movements should work.

you can try like this,f is reference to a Jframe but here the X co-ordinate is fixed.
final Robot r=new Robot();
f.addMouseMotionListener(new MouseMotionListener(){
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
r.mouseMove(20, e.getY());
}
});

Related

How to find direction of swipe in jframe

I am working on a swing application which run on a touch screen linux tablet. I want to implement a module in which i find that in which direction user swipe its finger on the screen. I am using MouseMotionListener to find the position of the mouse. But confusing now that how i can find the exact direction of the mouse movement. I only interested to find upward and downward movement on jframe. Can someone give me idea about that
I am using MouseMotionListener to find the position of the mouse.
I would guess you need to keep track of two MouseEvents:
the previous event (or maybe the mousePressed event which started the swipe?)
the current event.
so that you have access to the two points generated by each event.
Then you compare the two points using basic math. If the current y value is greater you are swiping down, otherwise up.
I wanted to add this example code illustrating #camickr's answer since it might help - as it got rejected as an edit, and I don't have enough points to add comments, here it is:
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DragDirectionDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Drag Direction Demo");
frame.setSize(500, 500);
frame.setVisible(true);
frame.addMouseListener(new MouseListener() {
float lastY = 0;
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse released at " + e.getY());
if (e.getY() < lastY) {
System.out.println("Upward swipe");
} else if (e.getY() > lastY) {
System.out.println("Downward swipe");
} else {
System.out.println("No movement");
}
;
}
public void mousePressed(MouseEvent e) {
System.out.println("Mouse clicked at " + e.getY());
lastY = e.getY();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
});
}
});
}
}
It is just a toy example illustrating the approach explained in #camickr's answer: tracking the y coordinate when the motion starts (the mouse button being depressed, in my example); then, comparing it to the y coordinate when the motion finishes (mouse button released, in my example - may need tweaking for touch, I don't know).
Observe the console output indicating what the motion has been picked up as. Good luck!

Java, If a draggable object touches other object

I have a code for dragging a label width mouse.
lbl_banner.addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e) {
//catching the current values for x,y coordinates on screen
x_pressed = e.getX();
y_pressed = e.getY();
}
});
lbl_banner.addMouseMotionListener(new MouseMotionAdapter(){
#Override
public void mouseDragged(MouseEvent e){
//and when the Jlabel is dragged
setLocation(e.getXOnScreen() - x_pressed, e.getYOnScreen() - y_pressed);
}
});
Now, how do I make a function: While I'm dragging this label around the Screen, If the Label by dragging touches other object (label, button,...) to do something.
if(//labelTouchesSomething){//do something}
While this is not technically a dragging but the dynamic move of a component (dragging is the transfer of contents in between components), you can compute the intersection of the current moving component against other components (this may need some navigation inside your hierarchy). May be this can help you: How do I detect the collison of components?. You can also use the methods contains of Component to determine if some coordinates are inside a component or not.

Shape position wont redraw when dragged with mouse

I have a filled circle drawn on a canvas and I'm trying to get it to move based on a click and drag method with the mouse. I've managed to checked whether the mouse pointer is within the bounds of the circle, and when I drag the mouse, the variable storing the position of the circle updates as it should, but the circle itself is not redrawing as I'm dragging (the most it will do is flicker). My problem is at the end where I'm overriding mouseDragged().
getCanvas().addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent event)
{
super.mouseClicked(event);
Point mousePosition = event.getPoint();
if (_circle.getShape1().contains(mousePosition))
Main.debugLabel.setText("Clicked");
}
#Override
public void mouseReleased(MouseEvent event)
{
super.mouseReleased(event);
_circle.isDraggable = false;
Main.debugLabel.setText("Released");
}
#Override
public void mousePressed(MouseEvent event)
{
super.mousePressed(event);
int button = event.getModifiers();
if (button == InputEvent.BUTTON1_MASK)
{
_circle.isDraggable = true;
Main.debugLabel.setText("Pressed");
}
}
});
getCanvas().addMouseMotionListener(new MouseAdapter()
{
#Override
public void mouseDragged(MouseEvent event)
{
super.mouseDragged(event);
Point mousePosition = event.getPoint();
if (_circle.isDraggable)
{
_circle.posX = mousePosition.x;
_circle.posY = mousePosition.y;
Main.debugLabel.setText("Dragging " + _circle.posX);
getCanvas().repaint();
}
}
#Override
public void mouseMoved(MouseEvent event)
{
super.mouseMoved(event);
Point mousePosition = event.getPoint();
if (_circle.getShape1().contains(mousePosition))
Main.debugLabel.setText("Within Bounds");
else if (!_circle.getShape1().contains(mousePosition) && !_circle.isDraggable)
Main.debugLabel.setText("Out of Bounds");
}
});
As shown in this example, one approach is to maintain two Point instances. One holds the last mouse location; the other holds the desired target location; both are in in component-relative coordinates.
In mousePressed(),
Initialize the last mouse location.
Optionally, mark the target as selected.
Invoke repaint() to display the selected appearance.
In mouseDragged(),
Update the target location by the difference between the new and old mouse locations.
Update the last mouse location to the current mouse location.
Invoke repaint().

Capturing mouse movement while moused pressed in Java

I'm trying to get the mouse position while pressing the mouse button but it doesn't work.
I'm extending the MouseAdapter and as stated at the Javadoc the mouseMove() is invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
This is an example class I have created to show you my problem:
public class TestMouse extends MouseAdapter{
int x,y;
boolean pressed;
public void mousePressed(MouseEvent e){
pressed = true;
}
public void mouseReleased(MouseEvent e){
pressed = false;
}
/*
Invoked when the mouse is not pressed only.
*/
public void mouseMoved(MouseEvent e){
x = e.getX();
y = e.getY();
}
/*
I want something like that.
*/
public void mousePressedAndMoved(MouseEvent e){
....
}
}
That's the problem with MouseAdapter, since it's a abstract class and not an interface (MouseMotionListener is the one you need), it gives empty implementations for all the possible events just to avoid you from being forced to override them all, this also implies that you could miss some of these events if you don't read docs.
If you look carefully at documentation though, you will see that you have
public void mouseDragged(MouseEvent e)
that you can override to listen exactly to what you need.

JFrame Paint having click-sensitive areas of the screen

I was making a program and I wanted to know how to make certain areas of a JFrame activate something when clicked, although without a button, like you clicked in the upper-right quarter of a picture to activate something.
Create an List of Shape objects to represent the areas that you want to click on:
List<Shape> shapes = new ArrayList<Shape>();
Then you can add different shapes to the List:
areas.add( new Rectangle(5, 5, 10, 10) );
Then you add a MouseListener to the frame and in the mousePressed event you would do something like:
for (Shape shape: shapes)
{
if (shape.contains(theMousePointFromTheMouseEvent)
// do something
}
create a JLabel object and set its icon to the image you want to display. Then add a mouse listener to the label object and implement all of its abstract classes especially the mouse clicked method to do what you want to do when clicked. Then when you click your JLabel you will see what you want.
The below code is an example to print "hello" when label is clicked:-
java.awt.event.MouseListener ml = new java.awt.event.MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("hello");
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
};
jLabel1.addMouseListener(ml);

Categories

Resources