Skip to content

Commit

Permalink
Improved crop image usability. Issue #39 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Asis2019 committed May 9, 2021
1 parent c74bbed commit c2289d8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/com/asis/controllers/IconCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class IconCreator {
@FXML
private ColorPicker borderPicker, fillPicker;

private Image characterImage;
private static Image characterImage;
private static Image finalImage;

public void initialize() {
Expand Down
50 changes: 29 additions & 21 deletions src/com/asis/controllers/dialogs/DialogCropImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public class DialogCropImage {
private static Image finalImage;

public void initialize() {
Platform.runLater(()->{
Platform.runLater(() -> {
mainImageViewPane = new ImageViewPane();
mainImageViewPane.setImageView(mainImageView);
mainImageView.setPreserveRatio(true);
mainImageViewPane.minWidthProperty().bind(selectionGroup.getScene().widthProperty());
mainImageViewPane.minHeightProperty().bind(selectionGroup.getScene().heightProperty());

selectionGroup.getChildren().add(mainImageViewPane);

mainImageView.setImage(finalImage);
Expand Down Expand Up @@ -75,14 +79,19 @@ private void cropImage(Bounds bounds, ImageView imageView) {
}

private void clearSelection(Group group) {
group.getChildren().remove(1,group.getChildren().size());
group.getChildren().remove(1, group.getChildren().size());
}

public void actionSaveSelection() {
cropImage(areaSelection.selectArea(selectionGroup).getBoundsInParent(),mainImageView);
try {
cropImage(areaSelection.selectArea(selectionGroup).getBoundsInParent(), mainImageView);

Stage stage = (Stage) selectionGroup.getScene().getWindow();
stage.close();
Stage stage = (Stage) selectionGroup.getScene().getWindow();
stage.close();
} catch (NullPointerException e) {
DialogMessage.messageDialog("WARNING", "Please create a selection by clicking and dragging",
300, 150);
}
}

public static Image openImageCrop(File image) {
Expand All @@ -93,7 +102,7 @@ public static Image openImageCrop(File image) {
FXMLLoader fxmlLoader = new FXMLLoader(DialogCropImage.class.getResource("/resources/fxml/dialog_crop_image.fxml"));
Parent root = fxmlLoader.load();

Scene main_scene = new Scene(root, 800, 800);
Scene main_scene = new Scene(root);

stage.setResizable(true);
stage.getIcons().add(new Image(Controller.class.getResourceAsStream("/resources/images/icon.png")));
Expand All @@ -118,7 +127,7 @@ private class AreaSelection {
private ResizableRectangle selectionRectangle = null;
private double rectangleStartX;
private double rectangleStartY;
private final Paint darkAreaColor = Color.color(0,0,0,0.5);
private final Paint darkAreaColor = Color.color(0, 0, 0, 0.5);

private ResizableRectangle selectArea(Group group) {
this.group = group;
Expand All @@ -140,7 +149,6 @@ private ResizableRectangle selectArea(Group group) {
clearSelection(group);

selectionRectangle = new ResizableRectangle(rectangleStartX, rectangleStartY, 0, 0, group);

darkenOutsideRectangle(selectionRectangle);
};

Expand All @@ -152,19 +160,19 @@ private ResizableRectangle selectArea(Group group) {

double size = max(offsetX, offsetY);

if(size > 0) {
if (size > 0) {
selectionRectangle.setWidth(size);
selectionRectangle.setHeight(size);
}
};

private void darkenOutsideRectangle(Rectangle rectangle) {
Rectangle darkAreaTop = new Rectangle(0,0,darkAreaColor);
Rectangle darkAreaLeft = new Rectangle(0,0,darkAreaColor);
Rectangle darkAreaRight = new Rectangle(0,0,darkAreaColor);
Rectangle darkAreaBottom = new Rectangle(0,0,darkAreaColor);
Rectangle darkAreaTop = new Rectangle(0, 0, darkAreaColor);
Rectangle darkAreaLeft = new Rectangle(0, 0, darkAreaColor);
Rectangle darkAreaRight = new Rectangle(0, 0, darkAreaColor);
Rectangle darkAreaBottom = new Rectangle(0, 0, darkAreaColor);

darkAreaTop.widthProperty().bind(finalImage.widthProperty());
darkAreaTop.widthProperty().bind(selectionGroup.getScene().widthProperty());
darkAreaTop.heightProperty().bind(rectangle.yProperty());

darkAreaLeft.yProperty().bind(rectangle.yProperty());
Expand All @@ -173,20 +181,20 @@ private void darkenOutsideRectangle(Rectangle rectangle) {

darkAreaRight.xProperty().bind(rectangle.xProperty().add(rectangle.widthProperty()));
darkAreaRight.yProperty().bind(rectangle.yProperty());
darkAreaRight.widthProperty().bind(finalImage.widthProperty().subtract(
darkAreaRight.widthProperty().bind(selectionGroup.getScene().widthProperty().subtract(
rectangle.xProperty().add(rectangle.widthProperty())));
darkAreaRight.heightProperty().bind(rectangle.heightProperty());

darkAreaBottom.yProperty().bind(rectangle.yProperty().add(rectangle.heightProperty()));
darkAreaBottom.widthProperty().bind(finalImage.widthProperty());
darkAreaBottom.heightProperty().bind(finalImage.heightProperty().subtract(
darkAreaBottom.widthProperty().bind(selectionGroup.getScene().widthProperty());
darkAreaBottom.heightProperty().bind(selectionGroup.getScene().heightProperty().subtract(
rectangle.yProperty().add(rectangle.heightProperty())));

// adding dark area rectangles before the selectionRectangle. So it can't overlap rectangle
group.getChildren().add(1,darkAreaTop);
group.getChildren().add(1,darkAreaLeft);
group.getChildren().add(1,darkAreaBottom);
group.getChildren().add(1,darkAreaRight);
group.getChildren().add(1, darkAreaTop);
group.getChildren().add(1, darkAreaLeft);
group.getChildren().add(1, darkAreaBottom);
group.getChildren().add(1, darkAreaRight);

// make dark area container layer as well
darkAreaTop.addEventHandler(MouseEvent.MOUSE_PRESSED, onMousePressedEventHandler);
Expand Down
4 changes: 2 additions & 2 deletions src/resources/fxml/dialog_crop_image.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.Group?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane id="darkThemeBackground" stylesheets="@../css/scene_details_stylesheet.css" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.asis.controllers.dialogs.DialogCropImage">
<BorderPane id="darkThemeBackground" prefHeight="480.0" prefWidth="640.0" stylesheets="@../css/scene_details_stylesheet.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.asis.controllers.dialogs.DialogCropImage">
<center>
<ScrollPane id="darkThemeBackground" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<ScrollPane id="darkThemeBackground" fitToHeight="true" fitToWidth="true" BorderPane.alignment="CENTER">
<content>
<Group id="darkThemeBackground" fx:id="selectionGroup">

Expand Down
4 changes: 3 additions & 1 deletion src/resources/fxml/icon_creator.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
<bottom>
<ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Button mnemonicParsing="false" onAction="#actionUseFile" text="Use file" />
<Button mnemonicParsing="false" onAction="#actionUseFile" text="Use file" >
<tooltip><Tooltip text="Use an already existing icon"/></tooltip>
</Button>
<Pane HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="#actionCancel" text="Cancel" />
<Button mnemonicParsing="false" onAction="#actionSaveIcon" text="Save Icon" />
Expand Down

0 comments on commit c2289d8

Please sign in to comment.