Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 画布在drawImage()之后为空_Java_Canvas_Javafx_Javafx 8 - Fatal编程技术网

Java 画布在drawImage()之后为空

Java 画布在drawImage()之后为空,java,canvas,javafx,javafx-8,Java,Canvas,Javafx,Javafx 8,我有一个绘图窗口,允许用户打开图像,在上面绘图,然后保存 我把它放在画布上,我可以创建一个空白的画布,在上面画画,然后保存下来,没有任何问题。但当我试图打开该图像并执行以下操作时: GraphicsContext gc = canvas.getGraphicsContext2D(); File file = new File(path); Image img = new Image(file.toURI().toString()); gc.drawImage(img,0,0); 它做的和以前一

我有一个绘图窗口,允许用户打开图像,在上面绘图,然后保存

我把它放在画布上,我可以创建一个空白的画布,在上面画画,然后保存下来,没有任何问题。但当我试图打开该图像并执行以下操作时:

GraphicsContext gc = canvas.getGraphicsContext2D();
File file = new File(path);
Image img = new Image(file.toURI().toString());
gc.drawImage(img,0,0);
它做的和以前一样。我得到一张空白画布,那张图片永远不会出现

我在代码中做错什么了吗?我知道路径是正确的,因为我可以在程序的其他部分的缩略图中看到该图像

更新 这是一个完整的示例程序

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class GUITest extends Application{
    
    String path = "10-9-2016-22-28.png";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane root = new Pane();
        Canvas canvas = new Canvas(600,800);
        
        GraphicsContext gc = canvas.getGraphicsContext2D();
        Image original = new Image(getClass().getResourceAsStream(path));
        gc.drawImage(original, 0, 0);
        
        root.getChildren().add(canvas);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

它现在可以工作,直到我执行
canvas.widthProperty().bind(root.widthProperty())
canvas.heightProperty().bind(root.heightProperty())。将画布维度与其父级维度绑定是否有问题?

当执行第一个布局过程时,
窗格的大小变为非0。在大小为
(0,0)
之前。由于大小为
(0,0)
的画布中没有空间,因此将删除所有内容。稍后当
Canvas
增长到根的大小时,您不会重新绘制图像,因此不会显示任何内容

因此,每次调整
画布的大小时,您都需要再次绘制画布内容(或至少直到大小变为非零)

出于同样的原因,您需要在根元素处设置场景的首选大小,而不是通过设置
Canvas
size

@Override
public void start(Stage primaryStage) throws Exception {
    Pane root = new Pane();
    root.setPrefSize(600, 800);
    Canvas canvas = new Canvas();

    GraphicsContext gc = canvas.getGraphicsContext2D();
    Image original = new Image(getClass().getResourceAsStream(path));

    root.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
        canvas.setWidth(newValue.getWidth());
        canvas.setHeight(newValue.getHeight());
        gc.drawImage(original, 0, 0);
    });

    root.getChildren().add(canvas);
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}

执行第一个布局过程时,
窗格的大小变为非0。在大小为
(0,0)
之前。由于大小为
(0,0)
的画布中没有空间,因此将删除所有内容。稍后当
Canvas
增长到根的大小时,您不会重新绘制图像,因此不会显示任何内容

因此,每次调整
画布的大小时,您都需要再次绘制画布内容(或至少直到大小变为非零)

出于同样的原因,您需要在根元素处设置场景的首选大小,而不是通过设置
Canvas
size

@Override
public void start(Stage primaryStage) throws Exception {
    Pane root = new Pane();
    root.setPrefSize(600, 800);
    Canvas canvas = new Canvas();

    GraphicsContext gc = canvas.getGraphicsContext2D();
    Image original = new Image(getClass().getResourceAsStream(path));

    root.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
        canvas.setWidth(newValue.getWidth());
        canvas.setHeight(newValue.getHeight());
        gc.drawImage(original, 0, 0);
    });

    root.getChildren().add(canvas);
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.show();
}

尝试关闭文件/图像如果使用默认的操作系统程序打开它,它可以工作?如何保存文件?使用
ImageIO
?@吓人的是,关闭文件/图像是什么意思?@Go,当我通过默认操作系统程序打开时,它工作,在我程序的其他地方也工作。我将其另存为.pngc您可以尝试从画布的根目录中删除并读取画布吗?这可能是刷新问题,但我不确定。请尝试关闭文件/图像如果您使用默认操作系统程序打开它,它会工作吗?如何保存文件?使用
ImageIO
?@吓人的是,关闭文件/图像是什么意思?@Go,当我通过默认操作系统程序打开时,它工作,在我程序的其他地方也工作。我将其另存为.pngc您可以尝试从画布的根目录中删除并读取画布吗?这可能是刷新问题,但我不确定。谢谢!这很有道理,谢谢你!这是有道理的