ganjan
ganjan

Reputation: 7596

Moving Rectangle Shapes in JavaFX with a thread

I have a strange problem that I need help with. This is a very simple application. I have a Rectangle Shape saved in a Car object.

public class Car {

    private Rectangle rect;
    private TheFourCarDeadlock.direction direction;

    public Car(Rectangle r, TheFourCarDeadlock.direction d) {
        this.rect = r;
        this.direction = d;
    }

    public void move() {

        double xPos;
        double yPos;
        switch (direction) {
            case left:
                // make sure x is a positive nr between 0 and 600
                xPos = (rect.getX() - 1) % 600;
                if (xPos < 0) {
                    xPos = 600;
                }
                rect.setX(xPos);
                System.out.println(String.format("move left, x: %1.0f", xPos));
                break;
            case right:
                xPos = (rect.getX() + 1) % 600;
                rect.setX(xPos);
                System.out.println(String.format("move right, x: %1.0f", xPos));
                break;
            case up:
                yPos = (rect.getY() - 1) % 600;
                rect.setY(yPos);
                break;
            case down:
                yPos = (rect.getY() + 1) % 600;
                rect.setY(yPos);
                break;
            default:
                throw new AssertionError(direction.name());

        }
    }
}

The class Car move a car object to the left or right depending on the direction that is set.

public class TheFourCarDeadlock extends Application {

    int maxWidth = 600;
    int maxHeight = 600;

    Car carFromLeft;
    Car carFromRight;

    public static enum direction {

        left, right, up, down
    };

    @Override
    public void start(Stage primaryStage) {

        // setup
        Group root = new Group();
        Scene scene = new Scene(root, maxWidth, maxHeight);

        // cars
        // cars on left side going right
        Rectangle leftSide = new Rectangle(0, 350 - 10, 20, 20);
        leftSide.setFill(Color.BLUE);
        root.getChildren().add(leftSide);
        carFromLeft = new Car(leftSide, direction.right);


        // cars on right side going left
        Rectangle rightSide = new Rectangle(600, 250 + 10, -20, -20);
        rightSide.setFill(Color.BLUE);
        root.getChildren().add(rightSide);
        carFromRight = new Car(rightSide, direction.left);

        primaryStage.setTitle("The Four Car Deadlock");
        primaryStage.setScene(scene);
        primaryStage.show();

        Thread t = new Thread(new DrawRunnable());
        t.start();
    }

    public void moveCars() {

        carFromLeft.move();
        carFromRight.move();

    }

    private class DrawRunnable implements Runnable {

        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(30);
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            moveCars();
                        }
                    });
                }
            } catch (InterruptedException ex) {
                System.out.println("Interrupted");
            }
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

In the main application I create two cars, one going right and one going left. I move each car every 30 ms with a Thread t.

Now, the weird thing that I don't understand is that the carFromLeft works fine and is displayed in the form and then moved to the right as I planned. The carFromRight, that have exactly the same code and made the same way. Just stands still... Even though in the output it prints that the position x of the rectangle is changing like it's supposed to.

In the output I get this:

move left, x: 436
move right, x: 165
move left, x: 435
move right, x: 166
move left, x: 434
move right, x: 167
move left, x: 433
move right, x: 168
move left, x: 432
move right, x: 169
move left, x: 431
move right, x: 170
move left, x: 430

So the rectangles change their x position. One going left and one going right. But only the one moving to the right is updated and drawn in the form... What is going on?

Upvotes: 0

Views: 2132

Answers (1)

tomsontom
tomsontom

Reputation: 5897

You gave the rect an negative height and width - if you give it a postive one it shows up and moves for me from left to right.

BTW - In JavaFX you should not use threads to implement this feature because you are spamming the system with Platform.runLater calls. You should use an Animation e.g. a TranslateTransition

Upvotes: 1

Related Questions