|
Java example source code file (ImageIcon.java)
The ImageIcon.java Java example source code
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing;
import java.awt.*;
import java.awt.image.*;
import java.beans.ConstructorProperties;
import java.beans.Transient;
import java.net.URL;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.Locale;
import javax.accessibility.*;
import sun.awt.AppContext;
import java.lang.reflect.Field;
import java.security.*;
/**
* An implementation of the Icon interface that paints Icons
* from Images. Images that are created from a URL, filename or byte array
* are preloaded using MediaTracker to monitor the loaded state
* of the image.
*
* <p>
* For further information and examples of using image icons, see
* <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html">How to Use Icons
* in <em>The Java Tutorial.
*
* <p>
* <strong>Warning:
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans™
* has been added to the <code>java.beans package.
* Please see {@link java.beans.XMLEncoder}.
*
* @author Jeff Dinkins
* @author Lynn Monsanto
*/
public class ImageIcon implements Icon, Serializable, Accessible {
/* Keep references to the filename and location so that
* alternate persistence schemes have the option to archive
* images symbolically rather than including the image data
* in the archive.
*/
transient private String filename;
transient private URL location;
transient Image image;
transient int loadStatus = 0;
ImageObserver imageObserver;
String description = null;
/**
* Do not use this shared component, which is used to track image loading.
* It is left for backward compatibility only.
* @deprecated since 1.8
*/
@Deprecated
protected final static Component component;
/**
* Do not use this shared media tracker, which is used to load images.
* It is left for backward compatibility only.
* @deprecated since 1.8
*/
@Deprecated
protected final static MediaTracker tracker;
static {
component = AccessController.doPrivileged(new PrivilegedAction<Component>() {
public Component run() {
try {
final Component component = createNoPermsComponent();
// 6482575 - clear the appContext field so as not to leak it
Field appContextField =
Component.class.getDeclaredField("appContext");
appContextField.setAccessible(true);
appContextField.set(component, null);
return component;
} catch (Throwable e) {
// We don't care about component.
// So don't prevent class initialisation.
e.printStackTrace();
return null;
}
}
});
tracker = new MediaTracker(component);
}
private static Component createNoPermsComponent() {
// 7020198 - set acc field to no permissions and no subject
// Note, will have appContext set.
return AccessController.doPrivileged(
new PrivilegedAction<Component>() {
public Component run() {
return new Component() {
};
}
},
new AccessControlContext(new ProtectionDomain[]{
new ProtectionDomain(null, null)
})
);
}
/**
* Id used in loading images from MediaTracker.
*/
private static int mediaTrackerID;
private final static Object TRACKER_KEY = new StringBuilder("TRACKER_KEY");
int width = -1;
int height = -1;
/**
* Creates an ImageIcon from the specified file. The image will
* be preloaded by using MediaTracker to monitor the loading state
* of the image.
* @param filename the name of the file containing the image
* @param description a brief textual description of the image
* @see #ImageIcon(String)
*/
public ImageIcon(String filename, String description) {
image = Toolkit.getDefaultToolkit().getImage(filename);
if (image == null) {
return;
}
this.filename = filename;
this.description = description;
loadImage(image);
}
/**
* Creates an ImageIcon from the specified file. The image will
* be preloaded by using MediaTracker to monitor the loading state
* of the image. The specified String can be a file name or a
* file path. When specifying a path, use the Internet-standard
* forward-slash ("/") as a separator.
* (The string is converted to an URL, so the forward-slash works
* on all systems.)
* For example, specify:
* <pre>
* new ImageIcon("images/myImage.gif") </pre>
* The description is initialized to the <code>filename string.
*
* @param filename a String specifying a filename or path
* @see #getDescription
*/
@ConstructorProperties({"description"})
public ImageIcon (String filename) {
this(filename, filename);
}
/**
* Creates an ImageIcon from the specified URL. The image will
* be preloaded by using MediaTracker to monitor the loaded state
* of the image.
* @param location the URL for the image
* @param description a brief textual description of the image
* @see #ImageIcon(String)
*/
public ImageIcon(URL location, String description) {
image = Toolkit.getDefaultToolkit().getImage(location);
if (image == null) {
return;
}
this.location = location;
this.description = description;
loadImage(image);
}
/**
* Creates an ImageIcon from the specified URL. The image will
* be preloaded by using MediaTracker to monitor the loaded state
* of the image.
* The icon's description is initialized to be
* a string representation of the URL.
* @param location the URL for the image
* @see #getDescription
*/
public ImageIcon (URL location) {
this(location, location.toExternalForm());
}
/**
* Creates an ImageIcon from the image.
* @param image the image
* @param description a brief textual description of the image
*/
public ImageIcon(Image image, String description) {
this(image);
this.description = description;
}
/**
* Creates an ImageIcon from an image object.
* If the image has a "comment" property that is a string,
* then the string is used as the description of this icon.
* @param image the image
* @see #getDescription
* @see java.awt.Image#getProperty
*/
public ImageIcon (Image image) {
this.image = image;
Object o = image.getProperty("comment", imageObserver);
if (o instanceof String) {
description = (String) o;
}
loadImage(image);
}
/**
* Creates an ImageIcon from an array of bytes which were
* read from an image file containing a supported image format,
* such as GIF, JPEG, or (as of 1.3) PNG.
* Normally this array is created
* by reading an image using Class.getResourceAsStream(), but
* the byte array may also be statically stored in a class.
*
* @param imageData an array of pixels in an image format supported
* by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
* @param description a brief textual description of the image
* @see java.awt.Toolkit#createImage
*/
public ImageIcon (byte[] imageData, String description) {
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
if (image == null) {
return;
}
this.description = description;
loadImage(image);
}
/**
* Creates an ImageIcon from an array of bytes which were
* read from an image file containing a supported image format,
* such as GIF, JPEG, or (as of 1.3) PNG.
* Normally this array is created
* by reading an image using Class.getResourceAsStream(), but
* the byte array may also be statically stored in a class.
* If the resulting image has a "comment" property that is a string,
* then the string is used as the description of this icon.
*
* @param imageData an array of pixels in an image format supported by
* the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
* @see java.awt.Toolkit#createImage
* @see #getDescription
* @see java.awt.Image#getProperty
*/
public ImageIcon (byte[] imageData) {
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
if (image == null) {
return;
}
Object o = image.getProperty("comment", imageObserver);
if (o instanceof String) {
description = (String) o;
}
loadImage(image);
}
/**
* Creates an uninitialized image icon.
*/
public ImageIcon() {
}
/**
* Loads the image, returning only when the image is loaded.
* @param image the image
*/
protected void loadImage(Image image) {
MediaTracker mTracker = getTracker();
synchronized(mTracker) {
int id = getNextID();
mTracker.addImage(image, id);
try {
mTracker.waitForID(id, 0);
} catch (InterruptedException e) {
System.out.println("INTERRUPTED while loading Image");
}
loadStatus = mTracker.statusID(id, false);
mTracker.removeImage(image, id);
width = image.getWidth(imageObserver);
height = image.getHeight(imageObserver);
}
}
/**
* Returns an ID to use with the MediaTracker in loading an image.
*/
private int getNextID() {
synchronized(getTracker()) {
return ++mediaTrackerID;
}
}
/**
* Returns the MediaTracker for the current AppContext, creating a new
* MediaTracker if necessary.
*/
private MediaTracker getTracker() {
Object trackerObj;
AppContext ac = AppContext.getAppContext();
// Opt: Only synchronize if trackerObj comes back null?
// If null, synchronize, re-check for null, and put new tracker
synchronized(ac) {
trackerObj = ac.get(TRACKER_KEY);
if (trackerObj == null) {
Component comp = new Component() {};
trackerObj = new MediaTracker(comp);
ac.put(TRACKER_KEY, trackerObj);
}
}
return (MediaTracker) trackerObj;
}
/**
* Returns the status of the image loading operation.
* @return the loading status as defined by java.awt.MediaTracker
* @see java.awt.MediaTracker#ABORTED
* @see java.awt.MediaTracker#ERRORED
* @see java.awt.MediaTracker#COMPLETE
*/
public int getImageLoadStatus() {
return loadStatus;
}
/**
* Returns this icon's <code>Image.
* @return the <code>Image object for this
Other Java examples (source code examples)Here is a short list of links related to this Java ImageIcon.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.