Skip to content

Commit

Permalink
Don't load images when headless
Browse files Browse the repository at this point in the history
  • Loading branch information
zathras committed Aug 24, 2018
1 parent 5c7b7da commit 448074c
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/edu/calpoly/spritely/ImageTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package edu.calpoly.spritely;


import java.awt.GraphicsEnvironment;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.AffineTransform;
Expand Down Expand Up @@ -63,28 +64,32 @@ public ImageTile(File imageFile, Size size, char text)
throws IOException
{
BufferedImage im;
if (!imageFile.exists()) {
throw new IOException("File not found: " + imageFile);
if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) {
im = null;
} else {
if (!imageFile.exists()) {
throw new IOException("File not found: " + imageFile);
}
try {
im = ImageIO.read(imageFile);
} catch (IOException ex) {
System.err.println("*** Error reading " + imageFile);
throw ex;
}
if (im.getWidth() != size.width || im.getHeight() != size.height) {
double scaleX = ((double) size.width) / im.getWidth();
double scaleY = ((double) size.height) / im.getHeight();
BufferedImage after =
new BufferedImage(size.width, size.height,im.getType());
AffineTransform scale =
AffineTransform.getScaleInstance(scaleX, scaleY);
AffineTransformOp scaleOp =
new AffineTransformOp(scale,
AffineTransformOp.TYPE_BILINEAR);
scaleOp.filter(im, after);
im = after;
}
}
try {
im = ImageIO.read(imageFile);
} catch (IOException ex) {
System.err.println("*** Error reading " + imageFile);
throw ex;
}
if (im.getWidth() != size.width || im.getHeight() != size.height) {
double scaleX = ((double) size.width) / im.getWidth();
double scaleY = ((double) size.height) / im.getHeight();
BufferedImage after =
new BufferedImage(size.width, size.height, im.getType());
AffineTransform scale =
AffineTransform.getScaleInstance(scaleX, scaleY);
AffineTransformOp scaleOp =
new AffineTransformOp(scale,
AffineTransformOp.TYPE_BILINEAR);
scaleOp.filter(im, after);
im = after;
}
this.image = im;
this.text = text;
}
Expand Down

0 comments on commit 448074c

Please sign in to comment.