View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.ledder;
21  import java.awt.Color;
22  import java.awt.Component;
23  import java.awt.Graphics;
24  import java.awt.Graphics2D;
25  import java.awt.Shape;
26  import java.awt.geom.Rectangle2D;
27  
28  import javax.swing.ImageIcon;
29  
30  /**
31   * This is the default icon used when displaying leds. This class is 
32   * instantiated when led icon could not be loaded from the resources. It 
33   * displays filled circle of appropriate color.
34   * 
35   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
36   * @version $id$
37   * 
38   * @see com.cosylab.gui.components.ledder.Led
39   * @see com.cosylab.gui.components.ledder.LedIconFactory
40   */
41  public class DefaultLedIcon extends ImageIcon {
42  
43  	// Parameters defining rendering of the led.
44  	private LedPaintParameters parameters;
45  
46  	// Area to be painted.
47  	private Rectangle2D rectangle;
48  
49  	// Paint implementation used to render the led.
50  	private LedPaint paint;
51  	
52  	protected int size=14;
53  
54  	/**
55  	 * Default constructor for DefaultLedIcon. If the color parameter is null,
56  	 * color will be set to <code>java.awt.Color.BLACK</code>
57  	 * 
58  	 * @param color Color
59  	 */
60  	protected DefaultLedIcon(Color color) {
61  		super();
62  
63  		if (color == null)
64  			color = Color.BLACK;
65  
66  		parameters = createDefaultParameters();
67  		
68  		getParameters().color = color;
69  	}
70  
71  	/**
72  	 * Returns default parameters to be used for rendering of leds.
73  	 * 
74  	 * @return LedPaintParameters
75  	 */
76  	protected LedPaintParameters createDefaultParameters() {
77  		LedPaintParameters lpp = new LedPaintParameters();
78  		
79  		//lpp.centerX = 7;
80  		//lpp.centerY = 7;
81  		lpp.radius = 6;
82  		lpp.highlightFactor = 0.85;
83  		lpp.highlightFocus = 3.0;
84  		lpp.shadowIntensity = 1.0;
85  		lpp.borderSize = 0.05;
86  		lpp.lightX = -0.20;
87  		lpp.lightY = -0.50;
88  		lpp.antialiasing = 3;
89  		
90  		return lpp;
91  	}
92  
93  	/**
94  	 * Returns area to be painted. This is a rectangle with dimensions of
95  	 * the icon.
96  	 * 
97  	 * @return Rectangle2D
98  	 */
99  	private final Rectangle2D getRectangle() {
100 		if (rectangle == null) {
101 			rectangle =
102 				new Rectangle2D.Double(0, 0, getIconWidth(), getIconHeight());
103 		}
104 		return rectangle;
105 	}
106 
107 	/**
108 	 * Returns LedPaint object that renders the icon.
109 	 * 
110 	 * @return LedPaint
111 	 */
112 	private final LedPaint getPaint() {
113 		if (paint==null) {
114 			paint = new LedPaint(getParameters());
115 		}
116 		return paint;
117 	}
118 
119 	/**
120 	 * Returns LedPaintParameters that are initialized in the constructor.
121 	 * @return LedPaintParameters
122 	 */
123 	private final LedPaintParameters getParameters() {
124 		return parameters;
125 	}
126 
127 	/**
128 	 * Returns the height of this icon. This value is set to 16.
129 	 * 
130 	 * @return int
131 	 * @see javax.swing.Icon#getIconHeight()
132 	 */
133 	public final int getIconHeight() {
134 		return size;
135 	}
136 
137 	/**
138 	 * Returns the width of this icon. This value is set to 16.
139 	 * 
140 	 * @return int
141 	 * @see javax.swing.Icon#getIconWidth()
142 	 */
143 	public final int getIconWidth() {
144 		return size;
145 	}
146 	
147 	/**
148 	 * Renders the icon.
149 	 * 
150 	 * @param component Component
151 	 * @param g Graphics
152 	 * @param x int
153 	 * @param y int
154 	 * @see javax.swing.Icon#paintIcon(Component, Graphics, int, int)
155 	 */
156 	public void paintIcon(Component component, Graphics g, int x, int y) {
157 		Graphics2D g2 = (Graphics2D) g;
158 		getPaint().setCenter(x+parameters.radius+2,y+parameters.radius+2);
159 		g2.setPaint(getPaint());
160 		g2.fill(new Rectangle2D.Double(x, y, getIconWidth(), getIconHeight()));
161 
162 	}
163 }