-1

I am remaking a Boardgame in java. I wanted to be able to click on a token, then on a spot, and have that token move to that spot. But, the token moves to a complete different location. Location of click On the image, I clicked in the red square, but the token ended up to the right and down from the actual spot I clicked. (The token is the round thing with the stark logo on it)

I printed the coördinates of the clicks and the location to see if they would differ, but this was not the case.

The code of the clicking part:

map.setOnMousePressed((e -> {
        System.out.println(e.getX());
        System.out.println(e.getY());
        currentOrder.setX(e.getX());
        currentOrder.setY(e.getY());
        System.out.println(currentOrder.getX());
        System.out.println(currentOrder.getY());
        currentOrder = basics;
    }));

The output of coördinates:

564.0
365.3333333333333
564.0
365.3333333333333
610.0
267.3333333333333
610.0
267.3333333333333
569.3333333333334
319.3333333333333
569.3333333333334
319.3333333333333

As you can see the coördinates of the clicked spot and the coördinates of the token are the same. But, on the screen, they are still not in the same spot.

Why is this the case? And how could I fix this?

I tried subtracting the translateX from the one I got, I tried subtracting the width of the token from the coördinates, I tried getSceneX and getScreenX, I tried setLayoutX. And all of this in every possible combination. Neither of it seemed to work. Sometimes it even gave the token a fixed X position and only the Y changed. So, not anything that worked.

Questions I read but didn't help me:

And probably some others I can't find anymore.

I used this as a reference to make that part of my code.

I tried to add only the relevant code. If another part of my code is needed for clarification, please let me know.

Noralie
  • 139
  • 9
  • What is `currentOrder`? – James_D Jun 28 '17 at 15:01
  • currentOrder is the order I clicked. Order refers to ordertoken. When I click on the token, it sets currentOrder to that token. Then, when I click somewhere, it uses the token I clicked from currentOrder, and then changes currentOrder back to basics, so that when I click the screen again it doesn't move again. I used a very basic version of this in another code, and it works there. – Noralie Jun 28 '17 at 15:03
  • I meant, what type of thing is it? Which class defines its `setX` and `setY` methods? – James_D Jun 28 '17 at 15:04
  • Oh sorry. It is an ImageView. – Noralie Jun 28 '17 at 15:05
  • `ImageView.setX()` and `setY()` don't do what you seem to be assuming they do. They set the `x` and `y` coordinates of the image within the image view (i.e. they define the top left of the `ImageView`'s viewport), not the location of the `ImageView` in its container. – James_D Jun 28 '17 at 15:07
  • I know they set it to the top left. I used the same code in a really simple class having just a scene, and it works perfectly there. But for some reason not here, while I almost literally copied the same code. – Noralie Jun 28 '17 at 15:09
  • But... huh?? What are you expecting to happen then? I thought you wanted the image view to appear where you clicked the mouse. – James_D Jun 28 '17 at 15:10
  • Yes, maybe I am explaining myself incorrectly. I want it to appear where I click, and in that case that would mean the top left part would be where I clicked. (Like it is in the test code I made) but here, the image is not even close. – Noralie Jun 28 '17 at 15:12
  • But as I explained, `setX()` and `setY()` do not change the coordinates of the `ImageView` in its container. – James_D Jun 28 '17 at 15:13
  • I don't specifically want to change the coördinates of the ImageView itself, just the image. – Noralie Jun 28 '17 at 15:14
  • The image isn't a visual component (in the sense that it's not a `Node` subclass). It doesn't have coordinates in the coordinate space of whatever container contains the image view. I think you have some pretty fundamental misunderstandings about how all these pieces fit together, and it's not really possible to answer this question given the information you've provided. You probably need to create a [MCVE] that shows the issue, instead of posting just a snippet of some large project without suitable context. – James_D Jun 28 '17 at 15:17
  • The problem is, I made a simple program with just one class and the exact same code (save the names) and there it worked exactly as I expected. Only when using it in this bigger project, it doesn't give the same results. – Noralie Jun 28 '17 at 15:22
  • I guess, let me try to explain this again. An `Image` is not a `Node`: it is not part of the scene graph, so it doesn't have coordinates and doesn't belong to a container of any kind. An `ImageView` is a `Node`: it belongs to a parent, has coordinates in that parent's coordinate system, and is part of the scene graph. It's what the user sees. An `ImageView` has an `Image`, which is essentially data used by the `ImageView` to display itself. The `setX()` and `setY()` methods of `ImageView` simply specify the top left pixel of the image that the `ImageView` displays.... – James_D Jun 28 '17 at 15:59
  • If you want to move the "token", `setX()` and `setY()` *do not do that* (at least, not directly and not in the way you seem to expect). In order to move the "token" you need to change the location of the `ImageView` in its parent container. How you do that will depend on what parent it is using and how you are managing layout in that parent. E.g. if it were a `GridPane` you would change its row and column index. If you are using a plain `Pane` with no layout, you would likely call `setLayoutX()` and `setLayoutY()`. You haven't provided this information, so your question is not answerable. – James_D Jun 28 '17 at 16:03

1 Answers1

2

Looks to me that you add a mouse listener to a component named map (and then you will receive coordinates relative to map) and add the logo to currentOrder's parent component, which has its own coordinate origin.

Depending on your component order you have to convert the coordinates. There are several possibilities to do this. One option would be using convertPointFromScreen and convertPointToScreen from ScreenUtilities.

PS: To give you some code as you commented on screen coordinates -- have you tried something like

Point screen_pt = SwingUtilities.convertPointToScreen(e.getPoint(), map);
Point order_pt = SwingUtilities.convertPointFromScreen(screen_pt, parent);
currentOrder.setX(order_pt.x);
currentOrder.setY(order_pt.y);

with parent here as the Component currentOrder is added to?

Lars
  • 325
  • 4
  • 5
  • I also tried it with the coördinates of the whole scene (scene.setOnMousePressed) but that didn't change the problem. And like I said before, I also used getSceneX and getScreenX, to get the coördinates on the screen, but that didn't change anything. – Noralie Jun 28 '17 at 14:41
  • I get this error when trying this: The method getPoint() is undefined for the type MouseEvent. – Noralie Jun 28 '17 at 14:58
  • Huh? Surprises me, as `getPoint()` in `MouseEvent` https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html#getPoint()) should exists. Anyway, you can also use `new java.awt.Point(e.getX(), e.getY())` instead. BTW, looking at the API doc, there are also `getXOnScreen()` and `getYOnScreen()` in `MouseEvent`. – Lars Jun 28 '17 at 15:37
  • I am using Javafx, not awt. – Noralie Jun 28 '17 at 15:39
  • OK, sorry. Wrong assumption by me. I think it also has a Point2D. However, I can't help concerning the coordinate system, as I don't know how it is organized in JavaFX. Never used it. – Lars Jun 28 '17 at 15:42