Skip to content

Commit

Permalink
improves file handling in Picture and GrayscalePicture
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-wayne committed Aug 29, 2019
1 parent 05e4006 commit 7b3073e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
31 changes: 20 additions & 11 deletions src/main/java/edu/princeton/cs/algs4/GrayscalePicture.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,40 @@ public GrayscalePicture(GrayscalePicture picture) {
/**
* Creates a grayscale picture by reading an image from a file or URL.
*
* @param filename the name of the file (.png, .gif, or .jpg) or URL.
* @param name the name of the file (.png, .gif, or .jpg) or URL.
* @throws IllegalArgumentException if cannot read image
* @throws IllegalArgumentException if {@code filename} is {@code null}
* @throws IllegalArgumentException if {@code name} is {@code null}
*/
public GrayscalePicture(String filename) {
if (filename == null) throw new IllegalArgumentException("constructor argument is null");
this.filename = filename;
public GrayscalePicture(String name) {
if (name == null) throw new IllegalArgumentException("constructor argument is null");
this.filename = name;
try {
// try to read from file in working directory
File file = new File(filename);
File file = new File(name);
if (file.isFile()) {
image = ImageIO.read(file);
}

// now try to read from file in same directory as this .class file
else {
URL url = getClass().getResource(filename);

// resource relative to .class file
URL url = getClass().getResource(name);

// resource relative to classloader root
if (url == null) {
url = getClass().getClassLoader().getResource(name);
}

// or URL from web
if (url == null) {
url = new URL(filename);
url = new URL(name);
}

image = ImageIO.read(url);
}

if (image == null) {
throw new IllegalArgumentException("could not read image file: " + filename);
throw new IllegalArgumentException("could not read image: " + name);
}

width = image.getWidth(null);
Expand All @@ -162,7 +171,7 @@ public GrayscalePicture(String filename) {
}
}
catch (IOException ioe) {
throw new IllegalArgumentException("could not open image file: " + filename, ioe);
throw new IllegalArgumentException("could not open image: " + name, ioe);
}
}

Expand Down
37 changes: 23 additions & 14 deletions src/main/java/edu/princeton/cs/algs4/Picture.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ public final class Picture implements ActionListener {
*
* @param width the width of the picture
* @param height the height of the picture
* @throws IllegalArgumentException if {@code width} is negative
* @throws IllegalArgumentException if {@code height} is negative
* @throws IllegalArgumentException if {@code width} is negative or zero
* @throws IllegalArgumentException if {@code height} is negative or zero
*/
public Picture(int width, int height) {
if (width < 0) throw new IllegalArgumentException("width must be non-negative");
if (height < 0) throw new IllegalArgumentException("height must be non-negative");
if (width <= 0) throw new IllegalArgumentException("width must be positive");
if (height <= 0) throw new IllegalArgumentException("height must be positive");
this.width = width;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Expand Down Expand Up @@ -131,39 +131,48 @@ public Picture(Picture picture) {
/**
* Creates a picture by reading an image from a file or URL.
*
* @param filename the name of the file (.png, .gif, or .jpg) or URL.
* @param name the name of the file (.png, .gif, or .jpg) or URL.
* @throws IllegalArgumentException if cannot read image
* @throws IllegalArgumentException if {@code filename} is {@code null}
* @throws IllegalArgumentException if {@code name} is {@code null}
*/
public Picture(String filename) {
if (filename == null) throw new IllegalArgumentException("constructor argument is null");
public Picture(String name) {
if (name == null) throw new IllegalArgumentException("constructor argument is null");

this.filename = filename;
this.filename = name;
try {
// try to read from file in working directory
File file = new File(filename);
File file = new File(name);
if (file.isFile()) {
image = ImageIO.read(file);
}

// now try to read from file in same directory as this .class file
else {

// resource relative to .class file
URL url = getClass().getResource(filename);

// resource relative to classloader root
if (url == null) {
url = new URL(filename);
url = getClass().getClassLoader().getResource(name);
}

// or URL from web
if (url == null) {
url = new URL(name);
}

image = ImageIO.read(url);
}

if (image == null) {
throw new IllegalArgumentException("could not read image file: " + filename);
throw new IllegalArgumentException("could not read image: " + name);
}

width = image.getWidth(null);
height = image.getHeight(null);
}
catch (IOException ioe) {
throw new IllegalArgumentException("could not open image file: " + filename, ioe);
throw new IllegalArgumentException("could not open image: " + name, ioe);
}
}

Expand Down

0 comments on commit 7b3073e

Please sign in to comment.