Java Icon interface
Introduction
Java's Icon interface is part of the Swing framework, which provides a set of lightweight (all-Java language) components that, to the maximum degree possible, work the same on all platforms. The Icon interface is used to encapsulate visual information (i.e., icons) that can be displayed within GUI components. It allows developers to create custom icons for various components like buttons, labels, and more, enhancing the user interface of Java applications.
Understanding the Icon Interface
The Icon interface resides in the javax.swing package and represents an object that can be used to paint an icon. It provides a way to define how an icon should be drawn and its dimensions. The Icon interface itself is quite simple, containing only three methods:
public interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); }
1. getIconWidth(): Returns the fixed width of the icon.
2. getIconHeight(): Returns the fixed height of the icon.
3. paintIcon(Component c, Graphics g, int x, int y): Paints the icon at the specified location. The Component parameter serves as a reference to the component that will display the icon, and Graphics is the context in which to paint.
Implementing the Icon Interface
To create a custom icon, we need to implement the Icon interface. Here is a simple example of a custom icon implementation:
import javax.swing.*; import java.awt.*; public class MyIcon implements Icon { private int width; private int height; public MyIcon(int width, int height) { this.width = width; this.height = height; } @Override public int getIconWidth() { return width; } @Override public int getIconHeight() { return height; } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.RED); g.fillRect(x, y, width, height); } public static void main(String[] args) { JFrame frame = new JFrame(); JButton button = new JButton("Click Me"); button.setIcon(new MyIcon(20, 20)); frame.setLayout(new FlowLayout()); frame.add(button); frame.setSize(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Output:
![Java Icon interface](https://static.tutorialandexample.com/java/java-icon-interface1.png)
Explanation: The MyIcon class implements the Icon interface and the constructor allows specifying the width and height of the icon and the paintIcon() method uses the Graphics object to draw a red rectangle, representing the icon.
Using Icons in Swing Components
Icons are widely used in Swing components to enhance the visual appeal and improve the user experience. Here are a few common components where icons are used:
JButton with Icon
import javax.swing.*; import java.awt.*; public class JButtonExample { public static void main(String[] args) { JFrame frame = new JFrame(); // Creating a button with text and an icon JButton button = new JButton("Button Text", new MyIcon(16, 16)); // Changing the icon of the button after it is created button.setIcon(new MyIcon(16, 16)); frame.setLayout(new FlowLayout()); frame.add(button); frame.setSize(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } static class MyIcon implements Icon { private int width; private int height; public MyIcon(int width, int height) { this.width = width; this.height = height; } @Override public int getIconWidth() { return width; } @Override public int getIconHeight() { return height; } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.RED); g.fillRect(x, y, width, height); } } }
Output:
![Java Icon interface](https://static.tutorialandexample.com/java/java-icon-interface2.png)
Explanation: The example demonstrates how to use the MyIcon class to create a JButton with an icon. The button is created with both text and an icon. The icon of the button is then changed after it is created.
JLabel with Icon
import javax.swing.*; import java.awt.*; public class JLabelExample { public static void main(String[] args) { JFrame frame = new JFrame(); // Creating a label with text and an icon JLabel label = new JLabel("Label Text", new MyIcon(16, 16), JLabel.CENTER); frame.setLayout(new FlowLayout()); frame.add(label); frame.setSize(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } static class MyIcon implements Icon { private int width; private int height; public MyIcon(int width, int height) { this.width = width; this.height = height; } @Override public int getIconWidth() { return width; } @Override public int getIconHeight() { return height; } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.RED); g.fillRect(x, y, width, height); } } }
Output:
![Java Icon interface](https://static.tutorialandexample.com/java/java-icon-interface3.png)
Explanation: The example shows how to use the MyIcon class to create a JLabel with both text and an icon. The label is aligned to the center, and the icon is drawn next to the text.
JCheckBox with Icon
import javax.swing.*; import java.awt.*; public class JCheckBoxExample { public static void main(String[] args) { JFrame frame = new JFrame(); // Creating a checkbox with text JCheckBox checkBox = new JCheckBox("Check Me"); // Setting the default icon checkBox.setIcon(new MyIcon(16, 16)); // Setting the icon when the checkbox is selected checkBox.setSelectedIcon(new MyIcon(16, 16)); frame.setLayout(new FlowLayout()); frame.add(checkBox); frame.setSize(200, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } static class MyIcon implements Icon { private int width; private int height; public MyIcon(int width, int height) { this.width = width; this.height = height; } @Override public int getIconWidth() { return width; } @Override public int getIconHeight() { return height; } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.RED); g.fillRect(x, y, width, height); } } }
Output:
![Java Icon interface](https://static.tutorialandexample.com/java/java-icon-interface4.png)
Explanation: The example demonstrates how to use the MyIcon class to create a JCheckBox with an icon. The checkbox is created with text, and a default icon is set. Additionally, a different icon is set for when the checkbox is selected.