[an error occurred while processing this directive]

Show Oval Icons


In this section, you will studied how to display Oval Icons.

An icon is an image that shows some concept or specific entity with meaning for the user. To show Oval Icons, we have used the class Container. The object of class Container added different components in a list. The order of the list will define the components in a stacking order within the container. 

The method setHorizontalTextPosition() returns the horizontal position of the label's text, relative to its image. To arrange the components in a left-to-right flow, like the lines of text in a paragraph, class FlowLayout is used.  

Following code draws the icon at the specified location:

public void paintIcon(Component container, Graphics g, int p, int q) {
g.drawOval(p, q, width - 1, height - 1);

The method paintIcon() is provided by the class Icon.

Here is the code of OvalIconExample.java

import java.awt.*;
import javax.swing.*;

public class OvalIconExample {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Oval Icons");

    JLabel label1 = new JLabel(new Oval(1050));
  JLabel label2 = new JLabel(new Oval(2040));
  JLabel label3 = new JLabel(new Oval(3030));
    JLabel label4 = new JLabel(new Oval(4020));
    JLabel label5 = new JLabel(new Oval(5080),
    Container container = frame.getContentPane();
    container.setLayout(new FlowLayout());
class Oval implements Icon {
  private int width, height;
  public Oval(int wid, int ht) {
    width = wid;
    height = ht;
  public void paintIcon(Component container, Graphics g, 
p, int q) {
    g.drawOval(p, q, width - 1, height - 1);
  public int getIconWidth() {
    return width;
  public int getIconHeight() {
    return height;

Output will be displayed as:

Download Source Code


[an error occurred while processing this directive]