Class ImageIcon
- All Implemented Interfaces:
-
Serializable
,Accessible
,Icon
public class ImageIcon extends Object implements Icon, Serializable, Accessible
For further information and examples of using image icons, see How to Use Icons in The Java Tutorial.
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 java.beans
package. Please see XMLEncoder
.
- Since:
- 1.2
Nested Class Summary
Modifier and Type | Class | Description |
---|---|---|
protected class |
ImageIcon.AccessibleImageIcon |
This class implements accessibility support for the ImageIcon class. |
Field Summary
Modifier and Type | Field | Description |
---|---|---|
protected static final Component |
component |
Deprecated. since 1.8 |
protected static final MediaTracker |
tracker |
Deprecated. since 1.8 |
Constructor Summary
Constructor | Description |
---|---|
ImageIcon() |
Creates an uninitialized image icon. |
ImageIcon |
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. |
ImageIcon |
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. |
ImageIcon |
Creates an ImageIcon from an image object. |
ImageIcon |
Creates an ImageIcon from the image. |
ImageIcon |
Creates an ImageIcon from the specified file. |
ImageIcon |
Creates an ImageIcon from the specified file. |
ImageIcon |
Creates an ImageIcon from the specified URL. |
ImageIcon |
Creates an ImageIcon from the specified URL. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
AccessibleContext |
getAccessibleContext() |
Gets the AccessibleContext associated with this ImageIcon. |
String |
getDescription() |
Gets the description of the image. |
int |
getIconHeight() |
Gets the height of the icon. |
int |
getIconWidth() |
Gets the width of the icon. |
Image |
getImage() |
Returns this icon's Image . |
int |
getImageLoadStatus() |
Returns the status of the image loading operation. |
ImageObserver |
getImageObserver() |
Returns the image observer for the image. |
protected void |
loadImage |
Loads the image, returning only when the image is loaded. |
void |
paintIcon |
Paints the icon. |
void |
setDescription |
Sets the description of the image. |
void |
setImage |
Sets the image displayed by this icon. |
void |
setImageObserver |
Sets the image observer for the image. |
String |
toString() |
Returns a string representation of this image. |
Field Details
component
@Deprecated protected static final Component component
tracker
@Deprecated protected static final MediaTracker tracker
Constructor Details
ImageIcon
public ImageIcon(String filename, String description)
- Parameters:
-
filename
- the name of the file containing the image -
description
- a brief textual description of the image - See Also:
ImageIcon
@ConstructorProperties("description") public ImageIcon(String filename)
new ImageIcon("images/myImage.gif")The description is initialized to the
filename
string.- Parameters:
-
filename
- a String specifying a filename or path - See Also:
ImageIcon
public ImageIcon(URL location, String description)
- Parameters:
-
location
- the URL for the image -
description
- a brief textual description of the image - See Also:
ImageIcon
public ImageIcon(URL location)
- Parameters:
-
location
- the URL for the image - See Also:
ImageIcon
public ImageIcon(Image image, String description)
- Parameters:
-
image
- the image -
description
- a brief textual description of the image
ImageIcon
public ImageIcon(Image image)
- Parameters:
-
image
- the image - See Also:
ImageIcon
public ImageIcon(byte[] imageData, String description)
- Parameters:
-
imageData
- an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG -
description
- a brief textual description of the image - See Also:
ImageIcon
public ImageIcon(byte[] imageData)
- Parameters:
-
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 Also:
ImageIcon
public ImageIcon()
Method Details
loadImage
protected void loadImage(Image image)
- Parameters:
-
image
- the image
getImageLoadStatus
public int getImageLoadStatus()
- Returns:
- the loading status as defined by java.awt.MediaTracker
- See Also:
getImage
public Image getImage()
Image
.- Returns:
- the
Image
object for thisImageIcon
setImage
public void setImage(Image image)
- Parameters:
-
image
- the image
getDescription
public String getDescription()
- Returns:
- a brief textual description of the image
setDescription
public void setDescription(String description)
- Parameters:
-
description
- a brief textual description of the image
paintIcon
public void paintIcon(Component c, Graphics g, int x, int y)
x
, y
) in the coordinate space of the graphics context g
. If this icon has no image observer, this method uses the c
component as the observer.getIconWidth
public int getIconWidth()
- Specified by:
-
getIconWidth
in interfaceIcon
- Returns:
- the width in pixels of this icon
getIconHeight
public int getIconHeight()
- Specified by:
-
getIconHeight
in interfaceIcon
- Returns:
- the height in pixels of this icon
setImageObserver
public void setImageObserver(ImageObserver observer)
icon = new ImageIcon(...) button.setIcon(icon); icon.setImageObserver(button);
- Parameters:
-
observer
- the image observer
getImageObserver
public ImageObserver getImageObserver()
- Returns:
- the image observer, which may be null
toString
public String toString()
getAccessibleContext
@BeanProperty(expert=true, description="The AccessibleContext associated with this ImageIcon.") public AccessibleContext getAccessibleContext()
- Specified by:
-
getAccessibleContext
in interfaceAccessible
- Returns:
- an AccessibleImageIcon that serves as the AccessibleContext of this ImageIcon
- Since:
- 1.3
© 1993, 2021, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/ImageIcon.html