将画布存储在对象中?

时间:2014-12-16 22:16:24

标签: java canvas javafx

我写了一个类,负责持有画布及其graphicsContext2d对象。 但是,当我想将该画布对象添加到场景时,它不起作用。

为什么这段代码不像评论时的代码那样工作? 该对象只是存储在其他地方,或者我缺少什么?

// EDIT2:

问题是tab1.getHeight()返回0.0 ...

//编辑:

这是有效的,但为什么?我只是在CanvasScreen之外而不是在其中创建对象......

@FXML
public void initialize() {
    Canvas canvas = new Canvas(500, 500);
    CanvasScreen screen = new CanvasScreen(canvas, tab1.getHeight(), tab1.getWidth());
    screen.getContext().setStroke(Color.BLUE);
    screen.getContext().setLineWidth(5);
    screen.getContext().strokeLine(40, 10, 10, 40);


    tab1.getChildren().add(screen.getCanvas());
}

// OLD:

@FXML
public void initialize() {
    CanvasScreen screen = new CanvasScreen(tab1.getHeight(), tab1.getWidth());
    screen.getContext().setStroke(Color.BLUE);
    screen.getContext().setLineWidth(5);
    screen.getContext().strokeLine(40, 10, 10, 40);

//  Canvas canvas = new Canvas(500, 500);
//  canvas.getGraphicsContext2D().setStroke(Color.BLUE);
//  canvas.getGraphicsContext2D().setLineWidth(5);
//  canvas.getGraphicsContext2D().strokeLine(40, 10, 10, 40);

    tab1.getChildren().add(screen.getCanvas());
}

0 个答案:

没有答案