Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Show Icon Displayer 
 

An icon is an image, picture, or representation. To display the icon, we have defined an image.

 

Show Icon Displayer

                         

In this section, you will studied how to display a single icon one or more times in a row.

An icon  is an image, picture, or representation. To display the icon, we have defined an image. The class ImageIcon is responsible to paint the icons from Images. The MediaTracker preloads the images that are created from a URL, filename or byte array.

The variable space is defined to set the space between the images. The method createEmptyBorder(this.space, this.space,this.space, this.space) creates an empty border that takes up space. The class Insets defines the borders of a container which specifies the space at each of its edges that a container must leave .

The method getPreferredSize()  is used to determine how the layout should be arranged. The method getClipBounds() gets a rectangle and encloses the clipping region of the Graphics object. To give the blending and transparency effects with graphics and images for combining source and destination pixels, the class AlphaComposite is used.

Following code returns an ImageIcon:

protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = IconDisplayerExample.class.getResource(path);
return new ImageIcon(imgURL);
}

Here is the code of IconDisplayerExample.java

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

public class IconDisplayerExample extends JComponent {
  private Icon icon;
  private int numOfImages = 2;
  private int space = 5
  private Rectangle rect1 = new Rectangle();
  private Rectangle rect2 = new Rectangle();
public IconDisplayerExample(Icon icon, int numOfImages, int space) {
    this.icon = icon;
    if (numOfImages > 0) {
      this.numOfImages = numOfImages;
    }
    if (this.space > 0) {
      setBorder(BorderFactory.createEmptyBorder(this.space, this.space,
          this.space, this.space));
    }
  }
  public Dimension getPreferredSize() {
      Insets insets = getInsets();
      return new Dimension(icon.getIconWidth() * numOfImages + space
          * (numOfImages - 1) + insets.left + insets.right,
          icon.getIconHeight() + insets.top + insets.bottom);
   }
  public Dimension getMinimumSize() {
      Insets insets = getInsets();
    return new Dimension(icon.getIconWidth() + insets.left
          + insets.right, icon.getIconHeight() + insets.top
          + insets.bottom);
   }
  protected void paintComponent(Graphics g) {
      Insets insets = getInsets();
      int iconWidth = icon.getIconWidth();
      int p = getWidth() - insets.right - iconWidth;
      int q = insets.top;
      boolean shaded = false;
    Graphics2D g2 = (Graphics2D) g.create();
    g.getClipBounds(rect2);
      while (p >= insets.left) {
        rect1.setBounds(p, q, iconWidth, icon
            .getIconHeight());
        if (rect1.intersects(rect2)) {
          icon.paintIcon(this, g2, p, q);
        }
        p -= (iconWidth + space);
        if (!shaded) {
          g2.setComposite(AlphaComposite.getInstance(
              AlphaComposite.SRC_OVER, 0.1f));
          shaded = true;
        }
      }
    g2.dispose(); 
    }
 protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = IconDisplayerExample.class.getResource(path);
    return new ImageIcon(imgURL);
   }
 private static void createAndShowGUI() {
    JFrame frame = new JFrame("Icon Displayer Example");
  frame.setSize(400,150);
    ImageIcon imageIcon = createImageIcon("alert.gif");
    IconDisplayerExample iconDisplayer = new IconDisplayerExample(
         imageIcon, 
6, -1);
    frame.getContentPane().add(iconDisplayer, BorderLayout.CENTER);
    frame.setVisible(true);
  }
  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c string com class orm ant form object io sed format screen font display vi metrics id render oo define

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.