canvas教程

当屏幕调整大小时调整画布大小

字号+ 作者:H5之家 来源:H5之家 2017-07-29 08:00 我要评论( )

JavaFX - 当屏幕调整大小时调整画布大小(JavaFX - Resize Canvas when screen is resized) - IT屋-程序员软件开发技术分享社区

问 题

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()); }

本文地址:IT屋 » JavaFX - Resize Canvas when screen is resized

问 题

我正在使用我在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>

本文地址:IT屋 » JavaFX - 当屏幕调整大小时调整画布大小

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • canvas上的Mousemove事件将重载鼠标事件,因此点击事件不会触发

    canvas上的Mousemove事件将重载鼠标事件,因此点击事件不会触发

    2017-07-29 12:04

  • 有没办法获取video视频里的图片

    有没办法获取video视频里的图片

    2017-07-28 16:08

  • Canvas 绘制粒子动画背景

    Canvas 绘制粒子动画背景

    2017-07-27 12:05

  • HTML5 Canvas 爆炸动画特效

    HTML5 Canvas 爆炸动画特效

    2017-07-27 11:10

网友点评