Crop the Image

Learn to crop the image and update the scale values on cropping it.

Cropping the image

Next, add code for when the “Crop” tool button is clicked.

  1. In initGUI(), add an action listener to cropButton to call crop() when the button is clicked. (Use JButton’s addActionListener() method, with a new ActionListener object as the parameter value. In the ActionListener, add a public method called actionPerformed(). The actionPerformed() method should have one parameter, an ActionEvent, and return nothing. In actionPerformed(), call crop().

Note: Do not jump into execution yet. The code will have a syntax error because crop() does not exist yet.

...
public void initGUI() {
    ...

    // crop options
    JButton cropButton = new JButton(cropIcon);
    cropButton.setToolTipText("Crop Image");
    cropButton.addActionListener(__________________ {
        _______________ actionPerformed(_____________) {
            ___________();
        }
    });
    toolbar.add(cropButton);

    ...

Get hands-on with 1200+ tech skills courses.