Al G Johnston
Al G Johnston

Reputation: 139

JavaFx Canvas not showing up in GridPane despite being in children/list/backingList

Basically what the title says. I did notice that it didn't show up in childSet/map/table like the MenuBar and Text did. What could I be doing wrong? I have to add more details for some reason?

All I'm trying to do is get the blue rectangle to show up in the gridPane as a test, so I can start drawing on the canvas.

private GridPane hackingPane;
private int[] redData;
private int[] tilesetHeaderAddresses;
private TilesetHeader[] tilesetHeaders;
private int[] mapHeaderPointers;
private int[] mapHeaderBanks;
private int[] mapHeaderAddresses;
private MapHeader[] mapHeaders;

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

@Override
public void start(Stage hackingStage) {
    initStage(hackingStage);
    hackingStage.show();
}

private void initStage(Stage hackingStage) {
    hackingPane = new GridPane();
    Scene hackingScene = new Scene(hackingPane);

    hackingStage.initStyle(StageStyle.DECORATED);
    hackingStage.setScene(hackingScene);
    hackingStage.setTitle("Red Hacker :)");
    initGridPane(hackingStage);
}

private void initGridPane(Stage hackingStage) {
    MenuBar hackingMB = new MenuBar();
    final Menu fileMenu = new Menu("File");
    MenuItem openMI = new MenuItem("Open");
    Text hex = new Text("Tilesets");
    Canvas tilesetCanvas = new Canvas(80, 80);
    GraphicsContext gc = tilesetCanvas.getGraphicsContext2D();

    fileMenu.getItems().add(openMI);
    hackingMB.getMenus().add(fileMenu);

    hackingPane.add(hackingMB, 0, 0, 1, 1);
    GridPane.setHgrow(hackingMB, Priority.ALWAYS);

    hackingPane.add(hex, 0, 1, 1, 1);
    hex.setFont(new Font(25));
    hex.setTextAlignment(TextAlignment.CENTER);
    GridPane.setHalignment(hex, HPos.CENTER);
    GridPane.setHgrow(hex, Priority.ALWAYS);

    gc.setFill(Color.BLUE);
    gc.fillRect(80,80,80,80);
    hackingPane.add(tilesetCanvas, 0, 2, 1, 1);
    GridPane.setMargin(tilesetCanvas, new Insets(0, 25, 25, 25));


    openMI.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent e) {
            openMIEvent(hackingStage);
        }
    });
}

private void openMIEvent(Stage hackingStage) {
    FileChooser fc = new FileChooser();
    fc.setTitle("Open File:");
    File sf = fc.showOpenDialog(hackingStage);

    if(sf != null){
        try {
            parseFile(sf);
        } catch (IOException e) {
            System.out.println("Error opening file");
        }
    }
}

private void parseFile(File sf) throws IOException {
    FileInputStream in = null;
    int i = 0;
    int size;

    redData = new int[(Math.toIntExact(sf.length()))];
    size = redData.length;
    try{
        in = new FileInputStream(sf);
        while(i < size){
            redData[i] = in.read();
            i++;
        }
    } finally{
        if(in != null){
            in.close();
        }
    }

    getTilesetHeaderAddresses();
    generateTilesetHeaders();

    getMapHeaderPointers();
    getMapHeaderBanks();
    getMapHeaderAddresses();
    generateMapHeaders();

    System.out.println(":)");
}

private void getTilesetHeaderAddresses() {
    int i = 0;
    tilesetHeaderAddresses = new int[23];
    while(i < 23){
        tilesetHeaderAddresses[i] = (51134 + (i * 12));
        i++;
    }
}

private void generateTilesetHeaders() {
    int i = 0;
    tilesetHeaders = new TilesetHeader[23];
    while(i < 23){
        tilesetHeaders[i] = new TilesetHeader(redData, tilesetHeaderAddresses[i]);
        i++;
    }   
}

private void getMapHeaderPointers() {
    int i = 0;
    mapHeaderPointers = new int[248];
    while(i < 248){
        short test1 = (short) (redData[(2*i)+431] << 8);
        short test2 = (short) (redData[(2*i)+430] & 0xFF);
        mapHeaderPointers[i] = (short) ((test1) | (test2));
        i++;
    }
}

private void getMapHeaderBanks() {
    int i = 0;
    mapHeaderBanks = new int[248];
    while(i < 248){
        mapHeaderBanks[i] = redData[49725+i];
        i++;
    }
}

private void getMapHeaderAddresses() {
    int i = 0;
    mapHeaderAddresses = new int[248];
    while(i < 248){
        mapHeaderAddresses[i] = (mapHeaderBanks[i]*0x4000) + (mapHeaderPointers[i]%0x4000);
        i++;
    }
}

private void generateMapHeaders() {
    int i = 0;
    mapHeaders = new MapHeader[248];
    while((i < 248)){
        if((i != 11) & (i != 105) & (i != 106) & (i != 107) & (i != 109) & (i != 110) & (i != 111) & (i != 112) & (i != 114) & (i != 115) & (i != 116) & (i != 117) & (i != 204) & (i != 205) & (i != 206) & (i != 231) & (i != 237) & (i != 238) & (i != 241) & (i != 242) & (i != 243) & (i != 244)){
        mapHeaders[i] = new MapHeader(redData, mapHeaderAddresses[i], mapHeaderBanks);
        }
        i++;
    }       
}

}

Upvotes: 1

Views: 2809

Answers (1)

fabian
fabian

Reputation: 82461

Since you specify the size of the Canvas as 80 x 80. Drawing a rectangle with a top left at coordinates (80, 80) draws a rectangle that is outside the Canvas bounds and therefore does not show up...
You need to draw something that is inside the canvas coordinates, e.g.

gc.fillRect(10, 10, 60, 60);

for a rectangle with a distance of 10 px to the bounds of the Canvas.

Upvotes: 2

Related Questions