2016-07-30 76 views
0

是否可以在dragView中有多個圖像?我有一個drag and drop應用程序一次移動多個圖像。我正在使用ArrayList移動此圖像。拖放正在工作。但我的問題是,拖動事件期間沒有圖像在鼠標光標後面。DragView中的多個圖像

如何將圖片放在我的ArrayList中的dragView

回答

1

您不能指定多個Image S,但你可以使用一個圖像包含圖像:

public Image combine(double offsetX, double offsetY, double sizeX, double sizeY, List<? extends Image> images) { 
    double width = (images.size()-1) * offsetX + sizeX; 
    double height = (images.size()-1) * offsetY + sizeY; 

    canvas.setWidth(width); 
    canvas.setHeight(height); 
    gc.clearRect(0, 0, width, height); 
    gc.setFill(Color.WHITE); 

    for (int i = 0, size = images.size(); i < size; i++) { 
     double x = i * offsetX; 
     double y = i * offsetY; 
     gc.fillRect(x, y, sizeX, sizeY); 
     Image img = images.get(i); 

     gc.drawImage(img, x, y, sizeX, sizeY); 
    } 

    return canvas.snapshot(PARAMETERS, null); 
} 

private Canvas canvas; 
private GraphicsContext gc; 
private static final SnapshotParameters PARAMETERS = new SnapshotParameters(); 

static { 
    PARAMETERS.setFill(Color.TRANSPARENT); 
} 

private void initCanvas() { 
    canvas = new Canvas(); 
    gc = canvas.getGraphicsContext2D(); 
    gc.setFill(Color.WHITE); 
} 


@Override 
public void start(Stage primaryStage) { 
    initCanvas(); 

    Button btn = new Button("Drag Source"); 
    Image img1 = new Image("http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/se/se-icon.png?v=93426798a1d4", 100, 100, true, true); 
    Image img2 = new Image("http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/sf/sf-icon.png?v=6c3100d858bb", 100, 100, true, true); 
    Image img3 = new Image("http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a", 100, 100, true, true); 

    DataFormat format = new DataFormat("application/custom"); 

    btn.setOnDragDetected(evt -> { 
     Dragboard db = btn.startDragAndDrop(TransferMode.COPY); 

     db.setDragView(combine(25, 25, img1.getWidth(), img1.getHeight(), Arrays.asList(img1, img2, img3))); 

     ClipboardContent content = new ClipboardContent(); 
     content.put(format, "something"); 
     db.setContent(content); 

     evt.consume(); 
    }); 
    ... 

結果(光標從頂部缺失,左拖圖像)

Result(cursor missing)