Save first mouseposition while 'mouseDragged' - java

I've created a Java-Application to add/remove and show a random number of points on a JPanel. It is possible to move the points per drag&drop. To undo the last actions I buffer the points.
My question: if a point will moved via 'mouseDragged', is it possible to save the first mouseposition while the drag begins? I've tried the following:
public void mouseDragged(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (!points.isEmpty() && curPointIndex > -1) {
if (move == false) {
pointmove = new Point(x,y);
move = true;
}
}
}
The method mouseReleased change the variable move back to false. The variable pointmove should save the first point, but it changes all the time while the mouse is dragged.
Could anyone discribe why or what I can to do differently?

As Arnaud mentioned, you can use mousePressed to store the initial point:
#Override
public void mousePressed(MouseEvent e)
{
Point initialPoint = e.getPoint();
}

Related

keyPressed() event carries on firing even after i stop pressing the key

So the object that I've drawn starts moving either up or down depending on what arrow key I've pressed but once i let go it carries on moving in the same direction not stopping unless i press a random key. If i press up it starts moving up and carries on moving up even after letting go of up. If i then press down it starts moving down and carries on moving down even after i am no longer pressing it. If any key other than up arrow key or down arrow key is pressed whilst it is moving it stops moving.
MAIN SKETCH:
Defender defender1;
PImage background;
int x=0; //global variable background location
void setup() {
size(800,400);
background = loadImage("spaceBackground.jpg");
background.resize(width,height);
defender1 = new Defender(200,200);
}
void draw () {
image(background, x, 0); //draw background twice adjacent
image(background, x+background.width, 0);
x -=4;
if(x == -background.width)
x=0; //wrap background
defender1.render();
defender1.keyPressed();
}
Defender Class:
class Defender {
int x;
int y;
Defender(int x, int y) {
this.x = x;
this.y = y;
}
void render() {
fill(255,0,0);
rect(x,y,50,20);
triangle(x+50,y,x+50,y+20,x+60,y+10);
fill(0,0,100);
rect(x,y-10,20,10);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
defender1.y = y-1;
} else if (keyCode == DOWN) {
defender1.y = y+1;
}
}
}
}
You're calling your defender1.keyPressed() function every time draw() is called.
The key and keyCode variables hold the most recently pressed key, regardless of whether that key is currently being held down.
To check whether any key is currently being pressed, you could use the keyPressed variable.
Or you could change your code to only call defender1.keyPressed() from the sketch-level keyPressed() function.
You can find more info in the reference.
Shameless self-promotion: here is a tutorial on user input in Processing.

Mouse Drag To Zoom implementation Java - not on click

I have written some code to implement a drag to zoom (i.e. draw a rectangle with the mouse and then zoom into this area so it fills the container) on a JPanel. However, before I wrote this into the Listener, I had a "mouseClicked" method, which is still there. This method performed a different function, simply returning the coords of the click.
Unfortunately, I mistakenly implemented the zoom function using MousePressed, which thus runs at the same time as the mouseClicked event. This means that when I click the mouse to get the coordinates, it also zooms in on a very small rectangle. Is there anyway to change this?
Thanks in advance for any suggestions, and please let me know if you need anything clarifying!
Here is the listener class:
class panelClickListener extends MouseAdapter implements MouseMotionListener {
JLabel toShowCoords, rangeLabel;
juliaPanel panelJulia;
mandPanel panelMand;
int startX, startY, currentX, currentY;
boolean dragging;
ComplexNumber startRange, endRange;
public panelClickListener(JLabel toShowCoordsIn, juliaPanel panelJuliaIn, mandPanel panelMandIn, JLabel rangeLabelIn) {
toShowCoords = toShowCoordsIn;
panelJulia = panelJuliaIn;
panelMand = panelMandIn;
rangeLabel = rangeLabelIn;
}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
userSelectedPoint = pointsToDrawOn[x][y];
toShowCoords.setText("Number Selected: " + userSelectedPoint.getReal() + " , " + userSelectedPoint.getImaginary());
panelJulia.setFixedNumber(pointsToDrawOn[x][y]);
panelJulia.repaint();
}
public void mousePressed(MouseEvent event) {
Point point = event.getPoint();
startX = point.x;
startY = point.y;
dragging = true;
}
public void mouseReleased(MouseEvent event) {
dragging = false;
startRange = pointsToDrawOn[startX][startY];
endRange = pointsToDrawOn[currentX][currentY];
maxVal = endRange;
minVal = startRange;
rangeLabel.setText("Real axis: " + minVal.getReal() + "," + maxVal.getReal() + " - Imaginary axis: " + minVal.getImaginary() + "," + maxVal.getImaginary());
populatePointArray();
panelMand.repaint();
}
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
currentX = p.x;
currentY = p.y;
}
}
dragging should only be set to true when mouseDragged is called, then when mouseReleased is called, you need to check to see if dragging is true before processing the zoom
There's some theory to go with that, mouseClicked will only be called if the point of the mouse is the same for mousePressed and mouseReleased.
So in a click operation you can expect mousePressed, mouseReleased and mouseClicked to be called (I believe in that order, but you'd better test it).
In a drag operation, you can expect mousePressed, mouseDragged and mouseReleased to be called

Java - Selecting and moving an object

I want to basically select an object on the frame and then drag it and the object moves with the mouse. For some reason I can't seem to get it to work.
Here is my code pressed method :
public void mousePressed(MouseEvent event)
{
//Get x and y
int x = event.getX();
int y = event.getY();
//set selected to null
selected = null;
//if a fruit contains x, y then selected is assigned that fruit
for( Fruit m : fruits)
{
if(m.contains(x,y))
{
selected.setXY(x,y);
}
}
This is my drag method:
public void mouseDragged(MouseEvent event)
{
//if selected not equal null
if(selected != null){
//get x and y
int x = event.getX();
int y = event.getY();
//make select follow the mouse and repaint
selected.setXY(x,y);
repaint();
}
}
If you need more of my code, please let me know and I will edit the question and add it.
Any help is appreciated
Since you set selected to null it cannot work.
What happens if you change mousePressed to (note that it will only work if there is only one fruit at (x,y):
public void mousePressed(MouseEvent event)
{
//Get x and y
int x = event.getX();
int y = event.getY();
//set selected to null
selected = null;
//if a fruit contains x, y then selected is assigned that fruit
for( Fruit m : fruits)
{
if(m.contains(x,y))
{
selected = m;
// position is set in mouseDrag
}
}
In mouseReleased you then set selected back to null (optional).

Gap between mouse on screen and JPanel on Dragndrop

I use JPanel to draw a square on the screen.
When I use MouseDragged it works fine and goes wherever I want, almost. Each time I click on the square, the square automatically moves and the top left corner goes right under the mouse.
How should I do so that the square doesn't replace itself and stays right under the mouse ?
Thanks for any help.
Keep an account of the difference between top-left coordinates of Component which you are
moving and mousePressed location.
And when you get new position, just subtract that difference to it.
Here I have tried to explain it through coding. Let myJPanel be the component you want to
move. Then here is the MouseAdapter that can work for you. New position is stored in
newPosition variable.
new MouseAdapter(){
int diffx = 0, diffy = 0;
public void mousePressed(MouseEvent e) {
Point topLeft = myJPanel.getLocation();
Point mouseDn = e.getPoint();
diffx = mouseDn.x - topLeft.x;
diffy = mouseDn.y - topLeft.y;
}
public void mouseDragged(MouseEvent e) {
Point mouseDr = e.getPoint();
int newX = mouseDr.x - diffx;
int newY = mouseDr.y - diffy;
Point newPosition = new Point(newX, newY);
}
};

Getting coordinates of mouse click on grid easy java StdDraw

okay so I am trying to get the coordinates on a grid of which square a mouse was clicked on (why i casted into int) and this is giving me like the current position of the mouse,however, I want the position after a click and for nothing to happen while it hovers.
what do i need to do?
while (gameOver==false){
mouseX= (int) StdDraw.mouseX();
mouseY=(int) StdDraw.mouseY();
game.update(mouseX, mouseY);
}
now i have
public void mouseReleased(MouseEvent e){
int mouseX = e.getX();
int mouseY = e.getY();
synchronized (mouseLock) {
mousePressed = false;
}
}
public void run(){
print();
boolean gameOver=false;
int mouseX,mouseY;
StdDraw.setCanvasSize(500, 500);
StdDraw.setXscale(0, game.gettheWidth());
StdDraw.setYscale(0, game.gettheHeight());
game.update(-1,-1);
while (gameOver==false){
mouseReleased(????)
game.update(mouseX, mouseY);
}
}
still not working
None of this is making sense still,
Could someone give me an example of it getting x and y coordinates then printing them?
I want the mouseX and mouseY to the be coordinates of the mouseclick. I've looked online I don't understand any of the other questions, I assume it has something to do with mouseevent?
StdDraw implements a MouseListener. Overload the mouseReleased method to set your mouseX and mouseY variables.
To overload, your rewrite the method the way you need it to run:
int mouseX = 0;
int mouseY = 0;
public static void main(String[] args) {
//do stuff
//...
while (gameOver == false) {
//because mouseX and mouseY only change when the mouse button is released
//they will remain the same until the user clicks and releases the mouse button
game.update(mouseX, mouseY);
}
}
//mouseReleased happens whenever the user lets go of the mouse button
#Override
public void mouseReleased(MouseEvent e) {
//when the user lets go of the button, send the mouse coordinates to the variables.
mouseX = e.getX();
mouseY = e.getY();
synchronized (mouseLock) {
mousePressed = false;
}
}
So for example, mouseX and mouseY are both 0. I click the mouse at 5, 6, drag it, and release the mouse at 120, 50. mouseReleased is called and changes mouseX to 120, and mouseY to 50. Meanwhile, game.update(0,0) has been happening. That now changes into game.update(120, 50), and will remain that way until I release the mouse button again.
To print the mouse coordinates:
#Override
public void mouseReleased(MouseEvent e) {
//when the user lets go of the button, send the mouse coordinates to the variables.
System.out.println("mouse x coord = " + e.getX());
System.out.println("mouse y coord = " + e.getY());
synchronized (mouseLock) {
mousePressed = false;
}
}

Categories