I am trying to make a very simple platformer game, but once I added the ability for platforms to move I started to get a really weird "glitch". For some reason, if I move a pane through the scene by an integer value (e.g. block.setLayoutX(block.getLayoutX() + 1)) there is no problem with the program, but once I use a double with decimal points (e.g. ```block.setLayoutX(block.getLayoutX() + 1.5) then random lines began showing up as the block moves throughout the scene.
The code below is a replica of what the issue is in my main program.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.animation.AnimationTimer;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.Group;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
public class GroupingIssue extends Application{
public static Pane block = new Pane(PlatformMaker.createPlatform(50, 50, Color.WHITE));
public static Group ObjectGroup = new Group(block);
public static Group MainGroup = new Group(ObjectGroup);
public static Scene MainScene;
public static AnimationTimer rightDown;
public static AnimationTimer leftUp;
@Override public void start(Stage stage){
MainScene = new Scene(MainGroup);
rightDown = new AnimationTimer(){
@Override public void handle(long e){
block.setLayoutX(block.getLayoutX() + 1.25);
block.setLayoutY(block.getLayoutY() + 1.25);
if(block.getLayoutY() > 275){
block.setLayoutY(275);
block.setLayoutX(275);
leftUp.start();
stop();
}
}
};
leftUp = new AnimationTimer(){
@Override public void handle(long e){
block.setLayoutX(block.getLayoutX() - 1.25);
block.setLayoutY(block.getLayoutY() - 1.25);
if(block.getLayoutY() < 25){
block.setLayoutY(25);
block.setLayoutX(25);
rightDown.start();
stop();
}
}
};
block.setLayoutX(5);
block.setLayoutY(5);
stage.setScene(MainScene);
stage.setWidth(300);
stage.setHeight(300);
stage.show();
rightDown.start();
}
}
class PlatformMaker{
public static Pane createPlatform(double width, double height, Color c){
Polygon p = new Polygon(new double[]{-width / 2, -height / 2, -width / 2, height / 2, width / 2, height / 2, width / 2 + 10, height / 2 - 10, width / 2 + 10, -height / 2 - 10, -width / 2 + 10, -height / 2 - 10});
Polygon p1 = new Polygon(new double[]{width / 2 + 1, height / 2 - 2, width / 2 + 9, height / 2 - 11, width / 2 + 9, -height / 2 - 8, width / 2 + 1, -height / 2 + 1});
p1.setFill(c);
Polygon p2 = new Polygon(new double[]{-width / 2 + 2, -height / 2 - 1, width / 2 - 1, -height / 2 - 1, width / 2 + 8, -height / 2 - 9, -width / 2 + 11, -height / 2 - 9});
p2.setFill(c);
return new Pane(p, p1, p2);
}
}
Some of the things I have tried to solve the issue is using a group or using a pane and I have also tried to see what happens if I use a regular rectangle instead of a group and there is no issue when I use a rectangle;
Replicates (sometimes) on:
The issue seems independent of the graphics card used, as I switched graphics and behavior did not change.
I have two monitors, one is the HiDPI screen on the laptop. When displayed there, there is no visual glitch. If I drag the window to my external monitor, which is a widescreen non-HiDPI screen, I see visual glitches (sometimes).

The glitches will appear more reliably if I add the following line:
block.setCacheHint(CacheHint.QUALITY);
The glitches do not appear if I add the following line:
block.setCacheHint(CacheHint.SPEED);
I advise submitting a bug report. In the bug report you could link back to this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With