Draw a Crop Box

Learn to draw a crop box on image no matter what width, height and background.

XOR mode

Some photos are dark, some are light, most have dark and light areas scattered throughout. How can we choose a drawing color that would show up on all photos?

By setting the drawing color to black, then setting XOR mode to white, Java will choose black or white or some color in between, for every pixel drawn. Java will choose each pixel color, based on the color of the pixel being replaced.

Drawing a crop box on the image

At this point, the program can crop an image. However, choosing the values to crop an exact area of the image is difficult.

Next, we will add code to allow the user to draw a box on the image to mark how they want the image cropped.

  1. In ImagePanel class, add four private integer instance variables called cropX, cropY, cropW, and cropH. For testing purposes, set cropX and cropY to 10, and cropW and cropH to 100.
  ...
 
  private ImageResizer imageResizer = null;
  private BufferedImage image = null;
  ___________ cropX = ______;
  ___________ cropY = ______;
  ___________ cropW = ______;
  ___________ cropH = ______;
 
    ...

Get hands-on with 1200+ tech skills courses.