Add an Image Filter and Resize the Image
Learn to add an image filter on file chooser as well as scale the image appropriately and add a size menu on the menu bar.
An image file filter
One way to make the program easier to use, and to avoid errors when opening a file, would be to make the file chooser show only image files.
Creating an image file filter
Create a file filter that allows only “.jpg”, “.png”, and “.gif” files. All file filters need a string description of the types of files allowed.
- Create a new class in the
slidingtiles
package calledImageFileFilter
, with superclassFileFilter
and with no stub code. - Add a public method called
getDescription()
. It should take no parameters and return a string.getDescription()
should return “All image files: *.jpg, *.png, *.gif”.
package slidingtiles;
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class ImageFileFilter extends FileFilter {
________________ getDescription(_______) {
return ________________________;
}
}
Get hands-on with 1200+ tech skills courses.