Read and Divide Image Files
Learn to read an image file and divide it into sub images.
We'll cover the following
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.
- 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.) - Add two private integer instance variables: one called
tileSize
, initialized to50
, and the other calledgridSize
, initialized to4
. - Add a private
BufferedImage
instance variable calledimage
, initialized tonull
.
...
public class SlidingTiles extends JFrame {
private static final long serialVersionUID = 1L;
_______________ FILENAME = "_____________";
_______________ tileSize = ____;
_______________ gridSize = ____;
_______________ image = _____;
...
Get hands-on with 1200+ tech skills courses.