Crazy with these mouseEvent methods in Java

A week at this time, and I'm just banging my head about this issue. It should be a simple exterior:

I have a rectangle. I click and drag the rectangle along the x axis. The rectangle should only move inside the specified boundary region (canvas), therefore, if the canvas has a width of 200 pixels, the x coordinate should go only from 0 to getWidth()-RECTANGLE_WIDTH.

Simple enough, but I just can't get the darn to work.

Below is my bulky code.

I attached two GLabels to view the coordinates. I noticed that the mouseMoved label will only display the coordinates inside the canvas (what I want), but the mouseDragged label will be negative, and the coordinates outside the canvas will also be displayed - this also controls the movement of the rectangle object. I am not sure why these two behaviors are different from each other.

Right now, my expression is below:

if ((gobj.getX()) > 0 && (gobj.getX()) < (APPLICATION_WIDTH - PADDLE_WIDTH)){
     gobj.move(e.getX() - lastX, 0);                
} 

what this operator is doing so far, gets a rectangle to the edge, but then it just inserts there and does not return. I pull my hair on this thing ...

import java.awt.*;
import java.awt.event.*;
import acm.graphics.*;
import acm.program.*;

/** This class displays a mouse-draggable rectangle and oval */

public class DragObject extends GraphicsProgram {

private static final int PADDLE_WIDTH = 150;
public static final int APPLICATION_WIDTH = 700;

    public void run() {

        GRect rect = new GRect(100, 100, 150, 100);
        rect.setFilled(true);
        rect.setColor(Color.RED);   
        add(rect);

        label2 = new GLabel ("");
        add(label2, 300, 400);


        label = new GLabel ("");
        add(label, 300, 300);
        addMouseListeners();


    }

    /* these coordinates are never going beyond the canvas-even if the 
     * mouse does...this is good...i want this   */

    public void mouseMoved(MouseEvent e){

        label2.setLabel("Coordinates:" + e.getX() + ", " + e.getY());

    }

/** Called on mouse press to record the coordinates of the click */
    public void mousePressed(MouseEvent e) {
        lastX = e.getX();
        lastY = e.getY();
        gobj = getElementAt(lastX, lastY);

    }

/** Called on mouse drag to reposition the object */
    public void mouseDragged(MouseEvent e) { 

          if ((gobj.getX()) > 0 && (gobj.getX()) < (APPLICATION_WIDTH - PADDLE_WIDTH)){
            gobj.move(e.getX() - lastX, 0);

           } 

            lastX = e.getX();


            /* This label is active when i click the object, and does go into negative 
             * numbers and still counts even when off the canvas...*/
            label.setLabel("Coordinates:" + e.getX() + ", " + e.getY());
    }

/* Instance variables */

private GLabel label2;
private GLabel label;
private GObject gobj;   /* The object being dragged */
private double lastX;   /* The last mouse X position */
private double lastY;   /* The last mouse Y position */
}
+3
source share
1 answer

You can drag out a component, even from a window, perfectly designed to support drag and drop.

For drag and drop events, you just need to limit the location of the event inside the container:

static public final int MAX_X=(CANVAS_WIDTH - PADDLE_WIDTH);

...

public void mouseDragged(MouseEvent e) { 
    int px=Math.max(0,Math.min(e.getX(),MAX_X));
    int dx=(px-lastX);

    if(dx!=0) { 
        gobj.move(dx); 
        lastX=px;
        }
    }

, - , GObject, , . .

, (, /) . , - . , x = 350, 700-150 = 550, 350-550 = -200, 200 .

, 0 <= px <= UPPER... - :

    ...
    if(dx!=0) { 
        gobj.moveX(dx,MAX_X); 
        lastX=px;
        }
    ...

GObject:

public void moveX(int dlt, int max) {
    positionX=Util.limitInt((positionX+dlt),0,max);
    }

Util:

/**
 * Limit the range of a number to the specified values.
 */
static public int limitInt(int val, int low, int hgh) {
    return ((val<low) ? low : ((val<hgh) ? val : hgh));
    }

Y . :

public void move(int dltX, int maxX, int dltY, int maxY) {
    positionX=Util.limitInt((positionX+dltX),0,maxX);
    positionY=Util.limitInt((positionX+dltY),0,maxY);
    }
+3

Source: https://habr.com/ru/post/1734993/


All Articles