Created
March 12, 2014 10:41
-
-
Save abhinayagarwal/9504509 to your computer and use it in GitHub Desktop.
A simple application to show the feature of Clipboard and its usage with only Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.event.EventHandler; | |
import javafx.scene.Scene; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.input.Clipboard; | |
import javafx.scene.input.DataFormat; | |
import javafx.scene.input.MouseButton; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.layout.StackPane; | |
import javafx.stage.Stage; | |
/** | |
* A simple application to show the feature of Clipboard and its usage. | |
* | |
* Run the application, copy any image and double click on the scene would paste the image onto the scene | |
* The images will stack on each other ! | |
*/ | |
public class ClipboardImageCopying extends Application { | |
@Override | |
public void start(Stage stage) throws Exception { | |
final StackPane stack = new StackPane(); | |
stack.setPrefSize(500, 500); | |
Scene scene = new Scene(stack); | |
stage.setScene(scene); | |
stage.show(); | |
stack.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() | |
{ | |
@Override | |
public void handle(MouseEvent mouseEvent) { | |
if (mouseEvent.getButton().equals( | |
MouseButton.PRIMARY)) { | |
if (mouseEvent.getClickCount() == 2) { | |
System.out.println("Double Click Triggered"); | |
Clipboard clipboard = Clipboard.getSystemClipboard(); | |
if(clipboard.hasContent(DataFormat.IMAGE)) | |
{ | |
final ImageView image = new ImageView(); | |
image.setImage(clipboard.getImage()); | |
AnchorPane anchorPane = new AnchorPane(); | |
AnchorPane.setLeftAnchor(image, mouseEvent.getX()); | |
AnchorPane.setTopAnchor(image, mouseEvent.getY()); | |
anchorPane.getChildren().addAll(image); | |
stack.getChildren().add(anchorPane); | |
} | |
} | |
} | |
} | |
}); | |
} | |
public static void main(String args[]) | |
{ | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment