Description
Loads an image from a resource.
License
Open Source License
Parameter
Parameter | Description |
---|
resourceName | the name of the resource to load. |
Exception
Parameter | Description |
---|
IOException | if an error occurs while opening or reading theresource stream. |
Return
the image named by the specified resource, or null if the resource could not be found.
Declaration
public static Image loadImage(String resourceName) throws IOException
Method Source Code
//package com.java2s;
/*//from w ww . j a va2 s . c o m
* ResourceUtilities.java (Class: com.madphysicist.tools.util.ResourceUtilities)
*
* Mad Physicist JTools Project (General Purpose Utilities)
*
* The MIT License (MIT)
*
* Copyright (c) 2013 by Joseph Fox-Rabinovitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class Main {
/**
* Determines whether image loading methods should use {@code javax.imageio.ImageIO.read()} or
* {@code java.awt.Toolkit}.
*
* @see javax.imageio.ImageIO#read(File)
* @see javax.imageio.ImageIO#read(URL)
* @see java.awt.Toolkit#createImage(String)
* @see java.awt.Toolkit#createImage(URL)
* @since 1.1.0
*/
private static final boolean PREFER_TOOLKIT = false;
/**
* Loads an image from a resource. To load the image as an {@code Icon}, use the {@link #loadIcon(java.lang.String)
* loadIcon()} method.
*
* @param resourceName the name of the resource to load.
* @return the image named by the specified resource, or {@code null} if the
* resource could not be found.
* @throws IOException if an error occurs while opening or reading the
* resource stream.
* @since 1.0.0
*/
public static Image loadImage(String resourceName) throws IOException {
if (PREFER_TOOLKIT) {
return Toolkit.getDefaultToolkit().createImage(ClassLoader.getSystemResource(resourceName));
} else {
try (InputStream input = ClassLoader.getSystemResourceAsStream(resourceName)) {
return (input == null) ? null : ImageIO.read(input);
}
}
}
}
Related
- loadImage(String image)
- loadImage(String imageName)
- loadImage(String imageName, Component c)
- loadImage(String path)
- loadImage(String path)
- loadImage(String url)
- loadImage(URL resource)
- loadImage(URL url)
- loadImageData(BufferedImage image)