Home Java Example Java Swing Graphics2D Show Oval Icons

Related Tutorials


 
 

Share on Google+Share on Google+

Show Oval Icons

Advertisement
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.

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");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300150);

  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),
  SwingConstants.CENTER);
  label5.setHorizontalTextPosition(SwingConstants.CENTER);
  Container container = frame.getContentPane();
  container.setLayout(new FlowLayout());
  container.add(label1);
  container.add(label2);
  container.add(label3);
  container.add(label4);
  container.add(label5);
  frame.setVisible(true);
  }
}
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, 
  int 
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 11, 2008

Related Tutorials

Discuss: Show Oval Icons  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com