JavaFX - 当屏幕调整大小时调整画布大小 [英] JavaFX - Resize Canvas when screen is resized

查看:6110
本文介绍了JavaFX - 当屏幕调整大小时调整画布大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我在JavaFX中创建的关卡编辑器的GUI,我想能够将canvas对象调整为新的拆分窗格尺寸。似乎一切iv'e尝试失败。这包括传递窗格对象并使用它的宽度,使用窗口大小侦听器并将width和height属性绑定到拆分窗格的宽度。有任何想法吗。这是调整大小之前的样子:





调整大小后:





任何人都有任何想法。该类的代码非常广泛,但是调整大小的代码将包括在内。

  public Canvas canvas; 
public String tabTitle;
public VBox layout;
public GraphicsContext g;
public核心;

public CanvasTab(Core core,String tabTitle){
this.core = core;
this.canvas = new Canvas(core.scene.getWidth() - 70,core.scene.getHeight() - 70);
layout = VBoxBuilder.create()。spacing(0).padding(new Insets(10,10,10,10))。

this.g = canvas.getGraphicsContext2D();

g.setFill(Color.BLACK);
g.fillRect(0,0,canvas.getWidth(),canvas.getHeight());

HBox.setHgrow(layout,Priority.ALWAYS);

this.setContent(layout);
this.setText(tabTitle);

canvas.widthProperty()。bind(layout.widthProperty()。subtract(20));
canvas.heightProperty()。bind(layout.heightProperty()。subtract(20));
}

public CanvasTab(Canvas canvas){
this.canvas = canvas;
}


解决方案

James_D指出,在调整大小时需要重新绘制画布的内容。这可以通过向你的canvas的width和height属性添加一个监听器来实现,如下:

  InvalidationListener listener = new InvalidationListener {
@Override
public void invalidated(Observable o){
redraw();
}
});
canvas.widthProperty()。addListener(listener);
canvas.heightProperty()。addListener(listener);

或在Java 8中使用功能接口:

  canvas.widthProperty()。addListener(observable  - > redraw()); 
canvas.heightProperty()。addListener(observable - > redraw());

其中 redraw()这将是你的例子(绘制一个黑色矩形:

  private void redraw(){
g。 setFill(Color.BLACK);
g.fillRect(0,0,canvas.getWidth(),canvas.getHeight());
}
pre>

I'm working on the GUI of my level editor that I built in JavaFX, and I want to be able to resize the canvas object to the new split pane dimensions. It seems that everything iv'e tried has failed. This includes passing the pane object in and using it's width directly, using window size listeners and binding the width and height property to that of the split pane. Any ideas. This is what it loos like before a resize:

And after a resize:

Does anybody hasve any ideas. The code for the class is pretty extensive, but the code for the resizing will be included.

public Canvas canvas;
public String tabTitle;
public VBox layout;
public GraphicsContext g;
public Core core;

public CanvasTab(Core core, String tabTitle){
    this.core = core;
    this.canvas = new Canvas(core.scene.getWidth() - 70, core.scene.getHeight() - 70);
    layout = VBoxBuilder.create().spacing(0).padding(new Insets(10, 10, 10, 10)).children(canvas).build();

    this.g = canvas.getGraphicsContext2D();

    g.setFill(Color.BLACK);
    g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

    HBox.setHgrow(layout, Priority.ALWAYS);

    this.setContent(layout);
    this.setText(tabTitle);

    canvas.widthProperty().bind(layout.widthProperty().subtract(20));
    canvas.heightProperty().bind(layout.heightProperty().subtract(20));
}

public CanvasTab(Canvas canvas){
    this.canvas = canvas;
}

解决方案

As James_D pointed out, you need to redraw the content of your canvas when resizing. This can be done by adding a listener to your canvas' width and height property as follows:

InvalidationListener listener = new InvalidationListener(){
    @Override
    public void invalidated(Observable o) {
        redraw();       
    }           
});
canvas.widthProperty().addListener(listener);
canvas.heightProperty().addListener(listener);

or in Java 8 using functional interfaces:

canvas.widthProperty().addListener(observable -> redraw());
canvas.heightProperty().addListener(observable -> redraw());

where redraw() is your own method which would look like this for your example (drawing a black rectangle:

private void redraw() {
    g.setFill(Color.BLACK);
    g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
}

这篇关于JavaFX - 当屏幕调整大小时调整画布大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆