|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +import javafx.application.Application; |
| 27 | +import javafx.embed.swing.SwingFXUtils; |
| 28 | +import javafx.geometry.Pos; |
| 29 | +import javafx.scene.Scene; |
| 30 | +import javafx.scene.control.Label; |
| 31 | +import javafx.scene.image.Image; |
| 32 | +import javafx.scene.image.ImageView; |
| 33 | +import javafx.scene.input.ClipboardContent; |
| 34 | +import javafx.scene.input.Dragboard; |
| 35 | +import javafx.scene.input.TransferMode; |
| 36 | +import javafx.scene.layout.VBox; |
| 37 | +import javafx.stage.Stage; |
| 38 | + |
| 39 | +import java.awt.image.BufferedImage; |
| 40 | + |
| 41 | +public class DndTestDragViewRawImage extends Application { |
| 42 | + Image image = createImage(240, 240); |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + Application.launch(args); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void start(Stage stage) { |
| 50 | + ImageView imageView = new ImageView(image); |
| 51 | + imageView.setOnDragDetected(event -> { |
| 52 | + ClipboardContent content = new ClipboardContent(); |
| 53 | + content.putImage(image); |
| 54 | + Dragboard dragboard = imageView.startDragAndDrop(TransferMode.ANY); |
| 55 | + dragboard.setContent(content); |
| 56 | + dragboard.setDragView(image); |
| 57 | + }); |
| 58 | + |
| 59 | + Label label = new Label("Click the image and drag. " + |
| 60 | + "The drag image displayed with the cursor (drag view) " + |
| 61 | + "should match the source image"); |
| 62 | + |
| 63 | + VBox vBox = new VBox(label, imageView); |
| 64 | + vBox.setSpacing(5.0); |
| 65 | + vBox.setAlignment(Pos.CENTER); |
| 66 | + stage.setScene(new Scene(vBox, 480, 480)); |
| 67 | + stage.setTitle("Drag View Image Colors"); |
| 68 | + stage.show(); |
| 69 | + } |
| 70 | + |
| 71 | + private static Image createImage(int width, int height) { |
| 72 | + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| 73 | + for (int y = 0; y < height; y++) { |
| 74 | + for (int x = 0; x < width; x++) { |
| 75 | + if (x < width * 0.33) { |
| 76 | + image.setRGB(x, y, 0xFF0000); |
| 77 | + } else if (x < width * 0.66) { |
| 78 | + image.setRGB(x, y, 0x00FF00); |
| 79 | + } else { |
| 80 | + image.setRGB(x, y, 0x0000FF); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return SwingFXUtils.toFXImage(image, null); |
| 85 | + } |
| 86 | +} |
0 commit comments