JavaFx Canvas尽管位于children / list / backingList中但未显示在GridPane中

时间:2016-09-22 15:42:06

标签: canvas javafx java-8 gridpane

基本上标题是什么。我注意到它没有像MenuBar和Text那样出现在childSet / map / table中。我能做错什么?出于某种原因,我必须添加更多细节?

我试图做的就是让蓝色矩形显示在gridPane中作为测试,这样我就可以开始在画布上绘图了。

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++;
    }       
}

}

1 个答案:

答案 0 :(得分:0)

由于您将T1 ( DateOfLatestChange, EmployeeID ) T2 ( EmployeeID, DateOfChange, CommentsOfChange ) 的大小指定为Canvas。在坐标80 x 80处绘制一个左上角的矩形会绘制一个超出(80, 80)边界的矩形,因此不显示...
你需要绘制画布坐标内的东西,例如

Canvas

表示与gc.fillRect(10, 10, 60, 60); 的边界距离为10 px的矩形。