001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.game.model;
042:
043: import java.awt.Component;
044: import java.awt.Graphics2D;
045: import java.awt.GraphicsConfiguration;
046: import java.awt.GraphicsEnvironment;
047: import java.awt.Image;
048: import java.awt.MediaTracker;
049: import java.awt.image.BufferedImage;
050: import java.io.IOException;
051: import java.net.URL;
052: import javax.imageio.ImageIO;
053:
054: public class ImageUtils {
055:
056: private final static Component component = new Component() {
057: };
058: private final static GraphicsConfiguration gc;
059:
060: static {
061: GraphicsEnvironment ge = GraphicsEnvironment
062: .getLocalGraphicsEnvironment();
063: gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
064: }
065:
066: public static BufferedImage loadImage(URL imageURL)
067: throws IllegalArgumentException {
068: try {
069: BufferedImage im = ImageIO.read(imageURL);
070: if (im == null) {
071: return null;
072: }
073: int transparency = im.getColorModel().getTransparency();
074: BufferedImage copy = gc.createCompatibleImage(
075: im.getWidth(), im.getHeight(), transparency);
076: Graphics2D g2d = copy.createGraphics();
077: g2d.drawImage(im, 0, 0, null);
078: g2d.dispose();
079: return copy;
080: } catch (IOException e) {
081: System.err.println("Load Image error for " + imageURL
082: + ":\n" + e); // NOI18N
083: e.printStackTrace();
084: return null;
085: }
086: }
087:
088: //XXX - this method must be removed!!!
089: public static Image getScaledImage(Image image, int maxWidth,
090: int maxHeight) {
091: Image scaledImage = image;
092: //if we want to scale the image (passed into the constructor) then do it
093: if (maxWidth != 0 && maxHeight != 0) {
094: float imgRatio = (float) image.getHeight(null)
095: / image.getWidth(null);
096: float previewRatio = (float) maxHeight / maxWidth;
097: if (previewRatio > imgRatio) {
098: scaledImage = image
099: .getScaledInstance(maxWidth, -1,
100: Image.SCALE_SMOOTH
101: | Image.SCALE_AREA_AVERAGING);
102: } else {
103: scaledImage = image
104: .getScaledInstance(-1, maxHeight,
105: Image.SCALE_SMOOTH
106: | Image.SCALE_AREA_AVERAGING);
107: }
108: }
109: MediaTracker mediaTracker = new MediaTracker(component);
110: mediaTracker.addImage(scaledImage, 0);
111: try {
112: mediaTracker.waitForID(0);
113: } catch (InterruptedException e) {
114: mediaTracker.removeImage(scaledImage, 0);
115: e.printStackTrace();
116: }
117: mediaTracker.removeImage(scaledImage, 0);
118: return scaledImage;
119: }
120:
121: }
|