【问题标题】:Adding multiple images to DragView将多个图像添加到 DragView
【发布时间】:2017-07-06 14:45:27
【问题描述】:

我正在制作一个纸牌游戏(耐心/纸牌/克朗代克),其中我使用了 JavaFX 的拖放功能。为了显示我正在拖动的卡片的幻影图像,我使用了 Dragboard。

Dragboard db;
db.setDragView(source.getImage());

这一切都可以正常工作,但我不知道如何解决整个堆栈的拖动问题。(多张卡片相互叠加,只有每张卡片的顶部可见)每叠卡片都存储在一个arraylist 但由于我一次只能将 1 个图像添加到拖动板,所以我被卡住了。

所以快速的问题:

  1. 有没有办法将多个图像添加到拖动板?
  2. 或者是 可以用另一种方式达到同样的效果吗?
  3. 或者是否可以从多个 Image 对象中创建 1 个 Image 对象以 将此新图像放在拖动板上?

dragDetected 事件处理程序:

    /*
    On dragDetected check the source of the event, copy to clipboard and show a ghost image
 */
private void dragDetected(MouseEvent mouseEvent) {
    Dragboard db;
    ClipboardContent cc;


    if(mouseEvent.getSource() instanceof CardPane){
        CardPane source = (CardPane) mouseEvent.getSource();
        cc = new ClipboardContent();
        db = source.startDragAndDrop(TransferMode.MOVE);

        if(source.getCard() != null) {
            //IF from stacks or nextCards
            if (stacks.contains(source) || nextCards.contains(source)) {
                db.setDragView(source.getCard().getImage());
                cc.putString(String.valueOf(source.getCard().getNumber()));
            } else {
                //PLACE MULTIPLE IMAGES TO THE DRAGBOARD HERE
            }

            db.setContent(cc);
            source.setVisible(false);
        }
    }


}

【问题讨论】:

  • 从每个图像中创建一个ImageView,将图像视图添加到某个适当的窗格,然后snapshot该窗格以获取图像。
  • 哇,我真的做到了。绝妙的解决方案!

标签: java javafx


【解决方案1】:

我会在这里分享我更新后的代码,以防有人遇到类似情况。

感谢@James_D,我能够找到解决方案

对于我想添加到图片中的每个元素,我都会创建一个新的 ImageView,将其添加到 Pane 并正确定位。

添加完所有元素后,我会对窗格进行快照并将返回的图像用作拖动板图像。

我的新拖动检测代码:

    private void dragDetected(MouseEvent mouseEvent) {
    Dragboard db;
    ClipboardContent cc;


    if(mouseEvent.getSource() instanceof CardPane){
        CardPane source = (CardPane) mouseEvent.getSource();
        cc = new ClipboardContent();
        db = source.startDragAndDrop(TransferMode.MOVE);

        if(source.getCard() != null) {
            //IF from stacks or nextCards
            if (stacks.contains(source) || nextCards.contains(source)) {
                db.setDragView(source.getCard().getImage());
                cc.putString(String.valueOf(source.getCard().getNumber()));
            } else {
                //PLACE MULTIPLE IMAGES TO THE DRAGBOARD HERE
                Pane stackImagePane = new Pane();
                Optional<List<CardPane>> stackListOptional = cards.stream()
                                                    .filter(list -> list.stream()
                                                    .anyMatch(e -> e.equals(source))
                                                    )
                                                    .findFirst();

                if(stackListOptional.isPresent()){
                    ArrayList<CardPane> stackList = (ArrayList) stackListOptional.get();

                    double y = 0;
                    for (CardPane e : stackList) {
                        ImageView view = new ImageView(e.getImage());
                        view.setY(y);
                        y += 20;
                        stackImagePane.getChildren().add(view);
                    }

                    Image ghostStack = stackImagePane.snapshot(null,null);
                    db.setDragView(ghostStack);
                    cc.putString(String.valueOf(source.getCard().getNumber()));
                }

            }

            db.setContent(cc);
            source.setVisible(false);
        }
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多