KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > OverlayIcon


1 /*
2  * Gruntspud
3  *
4  * Copyright (C) 2002 Brett Smith.
5  *
6  * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public License
10  * as published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */

21 package com.sshtools.ui.swing;
22
23 import java.awt.Component JavaDoc;
24 import java.awt.Graphics JavaDoc;
25
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.SwingConstants JavaDoc;
28
29 /**
30  * Create a {@link Icon} from an existing {@link Icon} by overlaying another
31  * {@link Icon}.
32  * <p>
33  * The overlaid icon may be placed at any compass point.
34  *
35  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
36  * @see SwingConstants
37  */

38 public class OverlayIcon implements Icon JavaDoc {
39     
40     // Private instance variables
41

42     private Icon JavaDoc icon;
43     private Icon JavaDoc overlayIcon;
44     private int position;
45
46     /**
47      * Constructor for the OverlayIcon object
48      *
49      * @param overlayIcon Description of the Parameter
50      * @param icon Description of the Parameter
51      * @param position Description of the Parameter
52      */

53     public OverlayIcon(Icon JavaDoc overlayIcon, Icon JavaDoc icon, int position) {
54         this.icon = icon;
55         this.overlayIcon = overlayIcon;
56         this.position = position;
57     }
58
59     /**
60      * Get the overlay icon
61      *
62      * @return overlay icon
63      */

64     public Icon JavaDoc getOverlayIcon() {
65         return overlayIcon;
66     }
67
68     /* (non-Javadoc)
69      * @see javax.swing.Icon#getIconHeight()
70      */

71     public int getIconHeight() {
72         return (icon == null) ? 16 : icon.getIconHeight();
73     }
74
75     /* (non-Javadoc)
76      * @see javax.swing.Icon#getIconWidth()
77      */

78     public int getIconWidth() {
79         return (icon == null) ? 16 : icon.getIconWidth();
80     }
81
82     /* (non-Javadoc)
83      * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
84      */

85     public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
86         if (icon != null) {
87             icon.paintIcon(c, g, x, y);
88
89             switch (position) {
90                 // TODO complete compass points and center
91
case SwingConstants.NORTH_WEST:
92                     overlayIcon.paintIcon(c, g, x, y);
93                     break;
94                 case SwingConstants.SOUTH_WEST:
95                     overlayIcon.paintIcon(c, g, x, (y + icon.getIconHeight()) - overlayIcon.getIconHeight());
96                     break;
97                 case SwingConstants.NORTH_EAST:
98                     overlayIcon.paintIcon(c, g, (x + icon.getIconWidth()) - overlayIcon.getIconWidth(), y);
99                 default:
100                     overlayIcon.paintIcon(c, g, (x + icon.getIconWidth()) - overlayIcon.getIconWidth(), (y + icon.getIconHeight())
101                                     - overlayIcon.getIconHeight());
102             }
103         }
104     }
105 }
106
Popular Tags