KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > WaferMapPlot


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------
27  * WaferMapPlot.java
28  * -----------------
29  *
30  * (C) Copyright 2003, 2004, by Robert Redburn and Contributors.
31  *
32  * Original Author: Robert Redburn;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * Changes
36  * -------
37  * 25-Nov-2003 : Version 1 contributed by Robert Redburn (DG);
38  * 05-May-2005 : Updated draw() method parameters (DG);
39  *
40  */

41  
42 package org.jfree.chart.plot;
43
44 import java.awt.BasicStroke JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.Graphics2D JavaDoc;
47 import java.awt.Paint JavaDoc;
48 import java.awt.Shape JavaDoc;
49 import java.awt.Stroke JavaDoc;
50 import java.awt.geom.Arc2D JavaDoc;
51 import java.awt.geom.Ellipse2D JavaDoc;
52 import java.awt.geom.Point2D JavaDoc;
53 import java.awt.geom.Rectangle2D JavaDoc;
54 import java.io.Serializable JavaDoc;
55 import java.util.ResourceBundle JavaDoc;
56
57 import org.jfree.chart.LegendItemCollection;
58 import org.jfree.chart.event.PlotChangeEvent;
59 import org.jfree.chart.event.RendererChangeEvent;
60 import org.jfree.chart.event.RendererChangeListener;
61 import org.jfree.chart.renderer.WaferMapRenderer;
62 import org.jfree.data.general.WaferMapDataset;
63 import org.jfree.ui.RectangleInsets;
64
65 /**
66  * A wafer map plot.
67  */

68 public class WaferMapPlot extends Plot implements RendererChangeListener,
69                                                   Cloneable JavaDoc,
70                                                   Serializable JavaDoc {
71
72     /** For serialization. */
73     private static final long serialVersionUID = 4668320403707308155L;
74     
75     /** The default grid line stroke. */
76     public static final Stroke JavaDoc DEFAULT_GRIDLINE_STROKE = new BasicStroke JavaDoc(0.5f,
77         BasicStroke.CAP_BUTT,
78         BasicStroke.JOIN_BEVEL,
79         0.0f,
80         new float[] {2.0f, 2.0f},
81         0.0f);
82
83     /** The default grid line paint. */
84     public static final Paint JavaDoc DEFAULT_GRIDLINE_PAINT = Color.lightGray;
85
86     /** The default crosshair visibility. */
87     public static final boolean DEFAULT_CROSSHAIR_VISIBLE = false;
88
89     /** The default crosshair stroke. */
90     public static final Stroke JavaDoc DEFAULT_CROSSHAIR_STROKE
91         = DEFAULT_GRIDLINE_STROKE;
92
93     /** The default crosshair paint. */
94     public static final Paint JavaDoc DEFAULT_CROSSHAIR_PAINT = Color.blue;
95
96     /** The resourceBundle for the localization. */
97     protected static ResourceBundle JavaDoc localizationResources =
98         ResourceBundle.getBundle("org.jfree.chart.plot.LocalizationBundle");
99
100     /** The plot orientation.
101      * vertical = notch down
102      * horizontal = notch right
103      */

104     private PlotOrientation orientation;
105
106     /** The dataset. */
107     private WaferMapDataset dataset;
108
109     /**
110      * Object responsible for drawing the visual representation of each point
111      * on the plot.
112      */

113     private WaferMapRenderer renderer;
114
115     /**
116      * Creates a new plot.
117      *
118      * @param dataset the dataset (<code>null</code> permitted).
119      */

120     public WaferMapPlot(WaferMapDataset dataset) {
121         this(dataset, null);
122     }
123
124     /**
125      * Creates a new plot.
126      *
127      * @param dataset the dataset (<code>null</code> permitted).
128      * @param renderer the renderer (<code>null</code> permitted).
129      */

130     public WaferMapPlot(WaferMapDataset dataset, WaferMapRenderer renderer) {
131
132         super();
133
134         this.orientation = PlotOrientation.VERTICAL;
135         
136         this.dataset = dataset;
137         if (dataset != null) {
138             dataset.addChangeListener(this);
139         }
140
141         this.renderer = renderer;
142         if (renderer != null) {
143             renderer.setPlot(this);
144             renderer.addChangeListener(this);
145         }
146
147     }
148
149     /**
150      * Returns the plot type as a string.
151      *
152      * @return A short string describing the type of plot.
153      */

154     public String JavaDoc getPlotType() {
155         return ("WMAP_Plot");
156     }
157
158     /**
159      * Sets the item renderer, and notifies all listeners of a change to the
160      * plot. If the renderer is set to <code>null</code>, no chart will be
161      * drawn.
162      *
163      * @param renderer the new renderer (<code>null</code> permitted).
164      */

165     public void setRenderer(WaferMapRenderer renderer) {
166
167         if (this.renderer != null) {
168             this.renderer.removeChangeListener(this);
169         }
170
171         this.renderer = renderer;
172         if (renderer != null) {
173             renderer.setPlot(this);
174         }
175
176         notifyListeners(new PlotChangeEvent(this));
177
178     }
179     
180     /**
181      * Draws the wafermap view.
182      *
183      * @param g2 the graphics device.
184      * @param area the plot area.
185      * @param anchor the anchor point (<code>null</code> permitted).
186      * @param state the plot state.
187      * @param info the plot rendering info.
188      */

189     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc area, Point2D JavaDoc anchor,
190                      PlotState state,
191                      PlotRenderingInfo info) {
192
193         // if the plot area is too small, just return...
194
boolean b1 = (area.getWidth() <= MINIMUM_WIDTH_TO_DRAW);
195         boolean b2 = (area.getHeight() <= MINIMUM_HEIGHT_TO_DRAW);
196         if (b1 || b2) {
197             return;
198         }
199
200         // record the plot area...
201
if (info != null) {
202             info.setPlotArea(area);
203         }
204
205         // adjust the drawing area for the plot insets (if any)...
206
RectangleInsets insets = getInsets();
207         insets.trim(area);
208
209         drawChipGrid(g2, area);
210         drawWaferEdge(g2, area);
211         
212     }
213
214     /**
215      * Calculates and draws the chip locations on the wafer.
216      *
217      * @param g2 the graphics device.
218      * @param plotArea the plot area.
219      */

220     private void drawChipGrid(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea) {
221         
222         Shape JavaDoc savedClip = g2.getClip();
223         g2.setClip(getWaferEdge(plotArea));
224         Rectangle2D JavaDoc chip = new Rectangle2D.Double JavaDoc();
225         int xchips = 35;
226         int ychips = 20;
227         double space = 1d;
228         if (this.dataset != null) {
229             xchips = this.dataset.getMaxChipX() + 2;
230             ychips = this.dataset.getMaxChipY() + 2;
231             space = this.dataset.getChipSpace();
232         }
233         double startX = plotArea.getX();
234         double startY = plotArea.getY();
235         double chipWidth = 1d;
236         double chipHeight = 1d;
237         if (plotArea.getWidth() != plotArea.getHeight()) {
238             double major = 0d;
239             double minor = 0d;
240             if (plotArea.getWidth() > plotArea.getHeight()) {
241                 major = plotArea.getWidth();
242                 minor = plotArea.getHeight();
243             }
244             else {
245                 major = plotArea.getHeight();
246                 minor = plotArea.getWidth();
247             }
248             //set upperLeft point
249
if (plotArea.getWidth() == minor) { // x is minor
250
startY += (major - minor) / 2;
251                 chipWidth = (plotArea.getWidth() - (space * xchips - 1))
252                     / xchips;
253                 chipHeight = (plotArea.getWidth() - (space * ychips - 1))
254                     / ychips;
255             }
256             else { // y is minor
257
startX += (major - minor) / 2;
258                 chipWidth = (plotArea.getHeight() - (space * xchips - 1))
259                     / xchips;
260                 chipHeight = (plotArea.getHeight() - (space * ychips - 1))
261                     / ychips;
262             }
263         }
264         
265         for (int x = 1; x <= xchips; x++) {
266             double upperLeftX = (startX - chipWidth) + (chipWidth * x)
267                 + (space * (x - 1));
268             for (int y = 1; y <= ychips; y++) {
269                 double upperLeftY = (startY - chipHeight) + (chipHeight * y)
270                     + (space * (y - 1));
271                 chip.setFrame(upperLeftX, upperLeftY, chipWidth, chipHeight);
272                 g2.setColor(Color.white);
273                 if (this.dataset.getChipValue(x - 1, ychips - y - 1) != null) {
274                     g2.setPaint(
275                         this.renderer.getChipColor(
276                             this.dataset.getChipValue(x - 1, ychips - y - 1)
277                         )
278                     );
279                 }
280                 g2.fill(chip);
281                 g2.setColor(Color.lightGray);
282                 g2.draw(chip);
283             }
284         }
285         g2.setClip(savedClip);
286     }
287
288     /**
289      * Calculates the location of the waferedge.
290      *
291      * @param plotArea the plot area.
292      *
293      * @return The wafer edge.
294      */

295     private Ellipse2D JavaDoc getWaferEdge(Rectangle2D JavaDoc plotArea) {
296         Ellipse2D JavaDoc edge = new Ellipse2D.Double JavaDoc();
297         double diameter = plotArea.getWidth();
298         double upperLeftX = plotArea.getX();
299         double upperLeftY = plotArea.getY();
300         //get major dimension
301
if (plotArea.getWidth() != plotArea.getHeight()) {
302             double major = 0d;
303             double minor = 0d;
304             if (plotArea.getWidth() > plotArea.getHeight()) {
305                 major = plotArea.getWidth();
306                 minor = plotArea.getHeight();
307             }
308             else {
309                 major = plotArea.getHeight();
310                 minor = plotArea.getWidth();
311             }
312             //ellipse diameter is the minor dimension
313
diameter = minor;
314             //set upperLeft point
315
if (plotArea.getWidth() == minor) { // x is minor
316
upperLeftY = plotArea.getY() + (major - minor) / 2;
317             }
318             else { // y is minor
319
upperLeftX = plotArea.getX() + (major - minor) / 2;
320             }
321         }
322         edge.setFrame(upperLeftX, upperLeftY, diameter, diameter);
323         return edge;
324     }
325
326     /**
327      * Draws the waferedge, including the notch.
328      *
329      * @param g2 the graphics device.
330      * @param plotArea the plot area.
331      */

332     private void drawWaferEdge(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea) {
333         // draw the wafer
334
Ellipse2D JavaDoc waferEdge = getWaferEdge(plotArea);
335         g2.setColor(Color.black);
336         g2.draw(waferEdge);
337         // calculate and draw the notch
338
// horizontal orientation is considered notch right
339
// vertical orientation is considered notch down
340
Arc2D JavaDoc notch = null;
341         Rectangle2D JavaDoc waferFrame = waferEdge.getFrame();
342         double notchDiameter = waferFrame.getWidth() * 0.04;
343         if (this.orientation == PlotOrientation.HORIZONTAL) {
344             Rectangle2D JavaDoc notchFrame =
345                 new Rectangle2D.Double JavaDoc(
346                     waferFrame.getX() + waferFrame.getWidth()
347                     - (notchDiameter / 2), waferFrame.getY()
348                     + (waferFrame.getHeight() / 2) - (notchDiameter / 2),
349                     notchDiameter, notchDiameter
350                 );
351             notch = new Arc2D.Double JavaDoc(notchFrame, 90d, 180d, Arc2D.OPEN);
352         }
353         else {
354             Rectangle2D JavaDoc notchFrame =
355                 new Rectangle2D.Double JavaDoc(
356                     waferFrame.getX() + (waferFrame.getWidth() / 2)
357                     - (notchDiameter / 2), waferFrame.getY()
358                     + waferFrame.getHeight() - (notchDiameter / 2),
359                     notchDiameter, notchDiameter
360                 );
361             notch = new Arc2D.Double JavaDoc(notchFrame, 0d, 180d, Arc2D.OPEN);
362         }
363         g2.setColor(Color.white);
364         g2.fill(notch);
365         g2.setColor(Color.black);
366         g2.draw(notch);
367         
368     }
369
370     /**
371      * Returns the dataset
372      *
373      * @return The dataset.
374      */

375     public WaferMapDataset getDataset() {
376         return this.dataset;
377     }
378
379     /**
380      * Return the legend items from the renderer.
381      *
382      * @return The legend items.
383      */

384     public LegendItemCollection getLegendItems() {
385         return this.renderer.getLegendCollection();
386     }
387
388     /**
389      * Notifies all registered listeners of a renderer change.
390      *
391      * @param event the event.
392      */

393     public void rendererChanged(RendererChangeEvent event) {
394         notifyListeners(new PlotChangeEvent(this));
395     }
396
397 }
398
Popular Tags