Java examples for 2D Graphics:Image Load
load Image
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.imageio.ImageIO; public class Main{ public static BufferedImage loadImage(String filename) { try {/*from w ww. java 2 s . co m*/ InputStream inputStream = openResource(filename); BufferedImage result = ImageIO.read(inputStream); inputStream.close(); return result; } catch (IOException e) { throw new RuntimeException("Could not read file '" + filename + "'", e); } } public static InputStream openResource(String filename) throws IOException { URL url = toURL(filename); return openResource(url); } public static InputStream openResource(URL url) throws IOException { InputStream result; URLConnection conn = url.openConnection(); result = conn.getInputStream(); return result; } public static URL toURL(String filename) throws FileNotFoundException { URL url = GUIUtils.class.getResource("/" + filename); if (url == null) { try { File file = new File(filename); if (file.exists()) { url = new File(filename).toURI().toURL(); } else { throw new FileNotFoundException("File not found: " + filename); } } catch (MalformedURLException e) { e.printStackTrace(); return null; } } return url; } }