Torn
Torn

Reputation: 346

How to use a Stage or anything inside the stage from outside the start method?

I am completely new to javafx.
It would probably be helpful enough to know why the code below is not working. (line 31: pw cannot be resolved)
But it would be nice to also know what else I might be doing wrong or inefficiently.
The end goal at some point is to have it draw a picture per-pixel on the screen.
What that picture is is however not so certain, and I'd like to be able to add pixels to that image from multiple different classes.
Also a possibility of adding some sort of more traditional UI on top, but that's not priority.

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.image.PixelWriter;
import javafx.scene.canvas.*;

public class Render extends Application {

    public static void render(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        stage.setTitle("Placeholder Title");
        Canvas canvas = new Canvas(1280, 720);
        Group root = new Group(canvas);
        Scene scene = new Scene(root, 1280, 720);
        stage.setScene(scene);
        PixelWriter pw = canvas.getGraphicsContext2D().getPixelWriter();
        stage.show();
    }

    private static void testRender() {
        int c = -1;
        for (int x = 0; x < 1280; x++) {
            for (int y = 0; y < 720; y++, c--) {
                pw.setArgb(x, y, c);
            }
        }
    }

}

Upvotes: 1

Views: 1417

Answers (2)

UnholySheep
UnholySheep

Reputation: 4096

As my explanations in comments seem to be unclear I'll try to explain by modifying the code (not tested, I'll try to help with any errors that it might contain).

Option 1: Storing the accessed variables as class members:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.image.PixelWriter;
import javafx.scene.canvas.*;

public class Render extends Application {

    private PixelWriter pw;

    public static void render(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        stage.setTitle("Placeholder Title");
        Canvas canvas = new Canvas(1280, 720);
        Group root = new Group(canvas);
        Scene scene = new Scene(root, 1280, 720);
        stage.setScene(scene);
        pw = canvas.getGraphicsContext2D().getPixelWriter();
        testRender(); // we can't call this before everything has been initialized anyway
        stage.show();
    }
    // this doesn't need to be static
    private void testRender() {
        int c = -1;
        for (int x = 0; x < 1280; x++) {
            for (int y = 0; y < 720; y++, c--) {
                pw.setArgb(x, y, c);
            }
        }
    }
}

Option 2: Passing the accessed instances as parameters:

@Override
public void start(Stage stage) {
    stage.setTitle("Placeholder Title");
    Canvas canvas = new Canvas(1280, 720);
    Group root = new Group(canvas);
    Scene scene = new Scene(root, 1280, 720);
    stage.setScene(scene);
    PixelWriter pw = canvas.getGraphicsContext2D().getPixelWriter();
    testRender(pw);
    stage.show();
}

private static void testRender(PixelWriter pw) {
    int c = -1;
    for (int x = 0; x < 1280; x++) {
        for (int y = 0; y < 720; y++, c--) {
            pw.setArgb(x, y, c);
        }
    }
}

I hope this helps you understand your possible solutions

Upvotes: 0

CTN
CTN

Reputation: 334

You can reffer this link to getting started with JavaFX.

https://docs.oracle.com/javase/8/javafx/get-started-tutorial/

Upvotes: 1

Related Questions