I have a Pane with a grid of canvases in it. When these canvases are colored and I change the scale of the pane white lines appear between the canvases.
The code currently has a scene with a pane in it. When the mouse is dragged it finds a canvas at that location using a HashMap that stores all the canvases by their x and y indexes, the found canvas is colored black. When the mouse is scrolled the scale of the pane is changed by 0.1.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class Main extends Application {
private static final double SIZE = 10;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) {
Pane pane = new Pane();
Scene scene = new Scene(pane);
pane.setOnMouseDragged(mouseEvent -> {
int x = (int) (mouseEvent.getX() / SIZE);
int y = (int) (mouseEvent.getY() / SIZE);
Plot plot = new Plot(x, y);
Canvas canvas = canvases.computeIfAbsent(plot, plot1 -> {
Canvas newCanvas = new Canvas(SIZE, SIZE);
pane.getChildren().add(newCanvas);
newCanvas.setLayoutX(x * SIZE);
newCanvas.setLayoutY(y * SIZE);
return newCanvas;
});
canvas.getGraphicsContext2D().setFill(Color.BLACK);
canvas.getGraphicsContext2D().fillRect(0, 0, 50, 50);
});
scene.setOnScroll(scrollEvent -> {
double scale = pane.getScaleX() + (scrollEvent.getDeltaY() > 0 ? 0.01 : -0.01);
pane.setScaleX(scale);
pane.setScaleY(scale);
});
stage.setScene(scene);
stage.show();
}
private static class Plot {
private final int x;
private final int y;
private Plot(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Plot plot = (Plot) o;
return x == plot.x &&
y == plot.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
private final Map<Plot, Canvas> canvases = new HashMap<>();
}



