JavaFX-获取一个ImageView的图像并将其分配给另一个ImageView [英] JavaFX- Get image of one ImageView and assign it to another ImageView

查看:1680
本文介绍了JavaFX-获取一个ImageView的图像并将其分配给另一个ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个应用程序,用户通过单击图像(ImageView1)可以在另一个ImageView2中看到它。所以我尝试在变量中获取ImageView1的图像

I am doing an application which user by clicking on an image(ImageView1) can see that in another ImageView2. so I try to get the image of ImageView1 in a variable

BufferedImage img= SwingFXUtils.fromFXImage(ImageView1.getImage(), null);

然后将该变量分配给ImageView2

And then assign that variable to ImageView2

ImageView2.setImage(SwingFXUtils.toFXImage(img, null));

但是看起来,setImage成功完成,但ImageView2没有显示任何内容。有人可以帮我找到更好的解决方案吗?

But it seems however, setImage is done successfully, but ImageView2 is not showing anything. anyone can help me for a better solution?

以下是代码示例:
Controller。 ImageView1

Here is the code example: Controller. ImageView1

@FXML
private void HandleMousePressedOnImageOne()
{
    BufferedImage img= SwingFXUtils.fromFXImage(ImageOne.getImage(), null);

    try
    {       
        ImageSelection imgSelection= ImageSelection.getImageSelectionInstance();
        imgSelection.SetBufferedImageOne(img);

        FXMLLoader loader= new FXMLLoader();
        SplitPane p= loader.load(getClass().getResource("ImageSelection.fxml").openStream());
        ImageSelectionController imgSelectionController= (ImageSelectionController)loader.getController();

        imgSelectionController.HandleImageOne();
        System.out.println("Image One has been Pressed!");
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

Model.ImageSelection

Model.ImageSelection

public class ImageSelection {
private BufferedImage bufferedImageOne;

private static ImageSelection imageSelectionInstace= new ImageSelection();

private ImageSelection(){}

public static ImageSelection getImageSelectionInstance()
{
    return imageSelectionInstace;
}

public void SetBufferedImageOne(BufferedImage img)
{
    this.bufferedImageOne= img;
}

public BufferedImage getBufferedImageOne()
{
    return this.bufferedImageOne;
}

}

Controller2.ImageView2

Controller2.ImageView2

public void HandleImageOne(){
    ImageSelection imgSelection= ImageSelection.getImageSelectionInstance();
    BufferedImage img= imgSelection.getBufferedImageOne();
    ImageOne.setImage(SwingFXUtils.toFXImage(img, null));
}


推荐答案

您的代码不起作用因为您正在加载FXML文件中定义的UI的新副本 ImageSelection.fxml 。您不显示此新副本。当您从加载该UI副本的加载器中检索控制器并调用

Your code doesn't work because you are loading a new copy of the UI defined in the FXML file ImageSelection.fxml. You don't display this new copy. When you retrieve the controller from the loader that loads that copy of the UI and call

imgSelectionController.HandleImageOne();

您更改 ImageView 中的图片是UI新实例的一部分。由于没有显示该实例,因此您看不到该调用的任何影响。

you change the image in the ImageView that is part of that new instance of the UI. Since that instance is not displayed, you don't see any effect from that call.

更好的方法,这也避免了您的第一个控制器依赖于第二个控制器在您的代码中,是在您的模型中创建 ObjectProperty< Image> ,并从第二个控制器观察它:

A better approach, which also avoids your first controller having a dependency on the second controller as in your code, is to create an ObjectProperty<Image> in your model, and observe it from the second controller:

public class ImageSelection {

    private final ObjectProperty<Image> image = new SimpleObjectProperty<>();

    private static ImageSelection imageSelectionInstance= new ImageSelection();

    private ImageSelection(){}

    public static ImageSelection getImageSelectionInstance() {
        return imageSelectionInstance;
    }

    public ObjectProperty<Image> imageProperty() {
        return image ;
    }

    public final void setImage(Image image) {
        imageProperty().set(image);
    }

    public final Image getImage()
    {
        return imageProperty().get();
    }

}

在第二个控制器中,

public class ImageSelectionController {

    @FXML
    private ImageView imageOne ;

    public void initialize() {
        ImageSelection.getImageSelectionInstance().imageProperty()
            .addListener((obs, oldImage, newImage) -> imageOne.setImage(newImage));
    }

    // ...
}

现在所有第一个控制器需要做的是在模型中设置图像:

Now all the first controller needs to do is set the image in the model:

@FXML
private void handleMousePressedOnImageOne() {
    ImageSelection.getImageSelectionInstance().setImage(imageOne.getImage());
}

请注意,绝对不需要将JavaFX图像转换为 BufferedImage 并返回。

Note that there's absolutely no need to convert from a JavaFX image to a BufferedImage and back.

我还建议不要使用单例模式,对于一些(记录良好;只是谷歌) 使用单身人士有什么问题)的原因。创建一个实例并将其传递给每个控制器,或使用依赖注入框架为您管理。

I also recommend not using a singleton pattern, for a number of (well-documented; just google "what is wrong with using singletons") reasons. Create a single instance and pass it to each of the controllers, or use a dependency injection framework to manage that for you.

这篇关于JavaFX-获取一个ImageView的图像并将其分配给另一个ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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