Read and Divide Image Files

Learn to read an image file and divide it into sub images.

In this lesson, we’ll use ImageIO to read an image from an image file. You will then get a rectangular portion of that image and make it into an ImageIcon, so it can be used on a button.

Getting an image from a file

Initialize the variables needed for reading the image and breaking it into correctly sized sub images.

  1. Add a private static final string instance variable called FILENAME, set to “SlidingTilesImage.jpg”(Do not worry about the file. We have provided one for you.)
  2. Add two private integer instance variables: one called tileSize, initialized to 50, and the other called gridSize, initialized to 4.
  3. Add a private BufferedImage instance variable called image, initialized to null.
  ...
public class SlidingTiles extends JFrame {
  private static final long serialVersionUID = 1L;
  _______________ FILENAME = "_____________";
 
  _______________ tileSize = ____;
  _______________ gridSize = ____;
  _______________ image = _____;
 
    ...

Get hands-on with 1200+ tech skills courses.