How to make canvas Resizable in javaFX?

前端 未结 6 1700
执笔经年
执笔经年 2020-12-09 10:36

In javaFX to resize a canvas there is no such method to do that, the only solution is to extends from Canvas.

class ResizableCanvas extends Canvas {

    pub         


        
6条回答
  •  甜味超标
    2020-12-09 11:36

    Taken from http://werner.yellowcouch.org/log/resizable-javafx-canvas/: To make a JavaFx canvas resizable all that needs to be done is override the min/pref/max methods. Make it resizable and implement the resize method.

    With this method no width/height listeners are necessary to trigger a redraw. It is also no longer necessary to bind the size of the width and height to the container.

    public class ResizableCanvas extends Canvas {
    
    @Override
    public double minHeight(double width)
    {
        return 64;
    }
    
    @Override
    public double maxHeight(double width)
    {
        return 1000;
    }
    
    @Override
    public double prefHeight(double width)
    {
        return minHeight(width);
    }
    
    @Override
    public double minWidth(double height)
    {
        return 0;
    }
    
    @Override
    public double maxWidth(double height)
    {
        return 10000;
    }
    
    @Override
    public boolean isResizable()
    {
        return true;
    }
    
    @Override
    public void resize(double width, double height)
    {
        super.setWidth(width);
        super.setHeight(height);
        
    }
    

提交回复
热议问题