我正在制作纸牌游戏(Patience / solitaire / klondike),其中我正在使用JavaFX的拖放功能。为了显示我正在拖动的卡片的鬼影,我使用了Dragboard。
Dragboard db;
db.setDragView(source.getImage());
这一切都很好,但是我无法弄清楚如何处理整个堆栈的拖动。(多张卡片彼此分层,只有每张卡片的顶部可见)每堆卡片存储在一个arraylist但由于我一次只能在拖拽板上添加1张图像,所以我卡住了。
快速问题:
dragDetected eventhandler:
/*
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);
}
}
}
答案 0 :(得分:0)
我会在这里分享我的更新代码,万一有人发现自己处于类似情况。
由于@James_D
,我能够找到解决方案对于我想要添加到图片中的每个元素,我创建一个新的ImageView,将其添加到窗格并正确定位。
添加完所有元素后,我会对窗格进行快照并使用我得到的图像作为我的Dragboard图像。
我的新拖动检测到代码:
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);
}
}
}