I have a simple method which writes pixels to a canvas' GrapicConte2D. A int[]-pixelarray is getting randomized every frame and updated with the PixelWriters setPixels()-Method.
I have a Dual-Monitor-setup, a "regular" screen and a macbook pro retina. When dragging my application frame into my "regular" screen everything works very fine for me. But placing it on my MacBooks Retina Displays it gets really laggy.
I have really no idea what went wrong. I was checking my code a few times, but it seems that I cannot help myself.
I would be very grateful for every advise that could help me.
Thanks
Code:
public class CanvasTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Random random = new Random();
BorderPane root = new BorderPane();
ScrollPane scrollPane = new ScrollPane();
Canvas canvas = new Canvas(1280, 800);
GraphicsContext gc = canvas.getGraphicsContext2D();
PixelWriter writer = gc.getPixelWriter();
PixelFormat format = PixelFormat.getIntArgbInstance();
int[] pixels = new int[(int) (canvas.getWidth() * canvas.getHeight())];
scrollPane.setContent(canvas);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
for (int i = 0; i < pixels.length; i++) {
pixels[i] = (255 << 24) | random.nextInt(0xffffff);
}
writer.setPixels(0, 0, (int) canvas.getWidth(), (int) canvas.getHeight(),
format, pixels, 0, (int) canvas.getWidth());
}
};
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root,1280,800));
primaryStage.show();
timer.start();
}
public static void main(String[] args) {
launch(args);
}
}