/** * Reads and returns a {@link TextureData} for an image from a specified file URL. * * @param fileUrl the URL of the image file to read. * @return a <code>TextureData</code> instance for the image. */ protected TextureData readImage(URL fileUrl) { try { return TextureIO.newTextureData(fileUrl, this.isUseMipMaps(), null); } catch (Exception e) { String msg = Logging.getMessage( "layers.TextureLayer.ExceptionAttemptingToReadTextureFile", this.getImageSource()); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); this.textureInitializationFailed = true; return null; } }
/** * Creates a {@link Texture} from this instance's {@link TextureData} if the <code>TextureData * </code> exists. * * @param dc the current draw context. * @return the newly created texture, or null if this instance has no current <code>TextureData * </code> or if texture creation failed. */ protected Texture makeTextureFromTextureData(DrawContext dc) { if (dc == null) { String message = Logging.getMessage("nullValue.DrawContextIsNull"); Logging.logger().severe(message); throw new IllegalStateException(message); } if (this.getTextureData() == null) // texture not in cache yet texture data is null, can't initialize { String msg = Logging.getMessage("nullValue.TextureDataIsNull"); Logging.logger().severe(msg); throw new IllegalStateException(msg); } try { Texture texture = TextureIO.newTexture(this.getTextureData()); if (texture == null) { this.textureInitializationFailed = true; return null; } this.width = texture.getWidth(); this.height = texture.getHeight(); this.texCoords = texture.getImageTexCoords(); this.setTextureParameters(dc, texture); // Cache the texture and release the texture data. dc.getTextureCache().put(this.getImageSource(), texture); this.setTextureData(null); return texture; } catch (Exception e) { String name = this.isBufferedImageSource() ? "BufferedImage" : this.getImageSource().toString(); String msg = Logging.getMessage("generic.ExceptionAttemptingToCreateTexture", name); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); return null; } }
/** * Creates this instance's {@link Texture} if the image source is a <code>BufferedImage<code>. * * @param dc the current draw context. * * @return the newly created texture, or null if the texture was not created. * * @throws IllegalStateException if the image source is null or not a <code>BufferedImage</code>. */ protected Texture makeBufferedImageTexture(DrawContext dc) { if (this.getImageSource() == null || !(this.getImageSource() instanceof BufferedImage)) { String message = Logging.getMessage("generic.NotABufferedImage"); Logging.logger().severe(message); throw new IllegalStateException(message); } try { TextureData td = TextureIO.newTextureData((BufferedImage) this.getImageSource(), this.isUseMipMaps()); if (td == null) return null; this.setTextureData(td); return this.makeTextureFromTextureData(dc); } catch (Exception e) { String msg = Logging.getMessage("generic.IOExceptionDuringTextureInitialization"); Logging.logger().log(java.util.logging.Level.SEVERE, msg, e); this.textureInitializationFailed = true; return null; } }