From 448074cfbb1ace3fbab2c39f602df7bc5d66073c Mon Sep 17 00:00:00 2001 From: Bill Foote Date: Thu, 23 Aug 2018 20:26:32 -0700 Subject: [PATCH] Don't load images when headless --- src/edu/calpoly/spritely/ImageTile.java | 47 ++++++++++++++----------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/edu/calpoly/spritely/ImageTile.java b/src/edu/calpoly/spritely/ImageTile.java index 9f20190..4d0c74a 100644 --- a/src/edu/calpoly/spritely/ImageTile.java +++ b/src/edu/calpoly/spritely/ImageTile.java @@ -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; @@ -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; }