IMAGE CROPPER IN JAVAFX
this time this a demonstration of an application that create small pieces from a bigger image and save them on disk in jpeg format :
the transparent rectangle is simply a text field :
public TextField createRectangle(double x, double y, double width, double height) { TextField rec = new TextField(); rec.setPrefHeight(1); rec.setPrefWidth(1); container.getChildren().add(rec); rec.setOpacity(0.7f); rec.setAlignment(Pos.BOTTOM_CENTER); rec.setTranslateX(x); rec.setTranslateY(y); rec.setFont(Font.font("System", FontWeight.BOLD, 14)); rec.setPromptText("give a file name"); rec.setCursor(Cursor.OPEN_HAND); currentRect = rec; rec.addEventHandler(MouseEvent.MOUSE_PRESSED, (MouseEvent e) -> { if (currentMode == Mode.SELECT) { markRectangleAsCurrent((TextField) e.getSource()); dragAnchor = new Point2D(e.getSceneX(), e.getSceneY()); initX = ((TextField) e.getSource()).getTranslateX(); initY = ((TextField) e.getSource()).getTranslateY(); } }); rec.addEventHandler(MouseEvent.MOUSE_DRAGGED, (MouseEvent e) -> { ((TextField) e.getSource()).setTranslateX(initX + e.getSceneX() - dragAnchor.getX()); ((TextField) e.getSource()).setTranslateY(initY + e.getSceneY() - dragAnchor.getY()); surroundRectangleWithCorners(((TextField) e.getSource())); }); rec.addEventHandler(MouseEvent.MOUSE_ENTERED, (MouseEvent e) -> { this.currentMode=Mode.SELECT; }); rec.addEventHandler(MouseEvent.MOUSE_EXITED, (MouseEvent e) -> { this.currentMode=Mode.CREATE; }); rec.addEventHandler(KeyEvent.KEY_PRESSED, (KeyEvent e) -> { if (e.getCode() == KeyCode.DELETE && currentRect == ((TextField) e.getSource())) { container.getChildren().remove(currentRect); setCurrentRectangleToNull(); } }); return rec; }
and the red dot are instance of Rectangle:
the code can be found on github :
https://github.com/mooninvader/imageCropperFx
the code can be found on github :
https://github.com/mooninvader/imageCropperFx
No comments:
Post a Comment