0

I'm printing images on a canvas using the right click of a mouse , I'm trying to create 2 buttons undo/redo buttons , but I can't think of a way to do it, I rather am deleting the photos that are being displayed using the left click of the mouse , it's not completely functional but it does the job , but I don't want to use the left click of the mouse , I want to have the undo/redo buttons but I can't think of a way , could someone please show me how it's done ?

import java.awt.image.RenderedImage;
import java.io.*;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class Main extends Application {
    Stack<Shape> undoHistory = new Stack(), redoHistory = new Stack();

    private static final int CANVAS_SIZE = 200;
    private static final String FISH_IMAGE = "https://www.shareicon.net/data/128x128/2015/03/28/14104_animal_256x256.png";

    @Override
    public void start(Stage primaryStage) {
        Image image = new Image(FISH_IMAGE, 100, 100, false, false);
        Button save = new Button("Save");
        VBox btns = new VBox(save);

        /* ----------Draw Canvas---------- */
        Canvas canvas = new Canvas(CANVAS_SIZE, CANVAS_SIZE);
        GraphicsContext gc  = canvas.getGraphicsContext2D();
        gc.drawImage(image, 50, 50);

        /* ----------UNDO & REDO---------- */
        if (e.getButton() == MouseButton.SECONDARY) {
            //gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
            double img1 = image1.getWidth() * 1.5;
            double img2 = image1.getHeight() * 1.5;
            gc.clearRect(e.getX() - img1 / 2 , e.getY() - img2 / 2 , img2, img1);
        }

        BorderPane pane = new BorderPane();
        pane.setLeft(btns);
        vpane.setCenter(canvas);

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
0009laH
  • 1,735
  • 11
  • 24
  • 2
    This answer may help you: [Implementing Undo/Redo in JavaFX](https://stackoverflow.com/a/40644102/13561971) – anko Sep 26 '21 at 14:49

0 Answers0