Reputation: 25
I stuck at this stage, how add that 3 towers in to the StackPane and how to move the bricks inside that pane (from 1-2-3 for example) using only JavaFX code without Scene Builder? I would be grateful for any answers!
Image:
Upvotes: 0
Views: 1777
Reputation: 18415
My suggestion:
You create a class "Tower" which extends the class Pane and contains a list of disks. The class has a method "accept" which returns boolean depending on whether you can put a disk there or not.
Create 3 instances of the class Tower.
Then you create a class "Disk" which extends the class Rectangle. Create 8 instances with varying width and add them to the first tower, ie the list of the class Tower.
Each Disk will use a mouselistener as described in Drag nodes like in a patience/Klondike card game. The mechanism is very similar to what you need.
In the mousepressed handler of the mouse listener you check whether the disk is on top of the tower or not and hence whether you can drag it or not.
In the mousereleased handler of the mouse listener you simply check whether a disk intersects (see boundsInParent and intersects) with a given tower. If it does and the accept method returns true, you drop it there. If the accept method returns false, you let it fly back to where it came from.
You're finished when a Tower has all of the disks.
That's basically it.
Oh, and don't use a StackPane, use a Pane instead. And you don't need images. The picture you show can be created solely of rectangles.
Upvotes: 4