KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > traces > painters > TracePainterFill


1 /*
2  * TracePainterFill.java, <enter purpose here>.
3  * Copyright (C) 2005 Achim Westermann, Achim.Westermann@gmx.de
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  */

22 package info.monitorenter.gui.chart.traces.painters;
23
24 import info.monitorenter.gui.chart.Chart2D;
25
26 import java.awt.Graphics2D JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * A trace painter that fills the area between trace to render and the x axis
33  * baseline with it's color.
34  * <p>
35  *
36  * Additionally it increases performance by summing up all points to render for
37  * a paint iteration ({@link #startPaintIteration()},
38  * {@link #endPaintIteration()}) and only invoking only one polygon paint for a
39  * paint call of the corresponding {@link info.monitorenter.gui.chart.Chart2D}.
40  * <p>
41  *
42  *
43  *
44  * @author <a HREF="mailto:Achim.Westermann@gmx.de">Achim Westermann </a>
45  *
46  * @version $Revision: 1.1 $
47  *
48  */

49 public class TracePainterFill extends ATracePainter {
50
51   /**
52    * Stores the corresponding chart to know the coordinate roots for closing the
53    * polygon to fill.
54    */

55   private Chart2D m_chart;
56
57   /** The list of x coordinates collected in one paint iteration. */
58   private List JavaDoc m_xPoints;
59
60   /** The list of y coordinates collected in one paint iteration. */
61   private List JavaDoc m_yPoints;
62
63   /**
64    * Constructor with the corresponding chart.
65    * <p>
66    *
67    * @param chart
68    * needed to get the start pixel coordinates of traces.
69    */

70   public TracePainterFill(final Chart2D chart) {
71     this.m_chart = chart;
72   }
73
74   /**
75    * @see info.monitorenter.gui.chart.ITracePainter#discontinue()
76    */

77   public void discontinue() {
78     this.endPaintIteration();
79     this.startPaintIteration();
80   }
81
82   /**
83    * @see info.monitorenter.gui.chart.ITracePainter#endPaintIteration()
84    */

85   public void endPaintIteration() {
86     if (this.m_graphics != null) {
87
88       int[] x = new int[this.m_xPoints.size() + 4];
89       x[0] = this.m_chart.getXChartStart();
90       Iterator JavaDoc it = this.m_xPoints.iterator();
91       int count = 1;
92       while (it.hasNext()) {
93         x[count] = ((Integer JavaDoc) it.next()).intValue();
94         count++;
95       }
96       x[count] = this.m_lastX;
97       // step down (or up) to the y=0 for the last value (in y)
98
x[count + 1] = this.m_lastX;
99       // step back to startx,starty (root)
100
x[count + 2] = this.m_chart.getXChartStart();
101
102       int[] y = new int[this.m_yPoints.size() + 4];
103       y[0] = this.m_chart.getYChartStart();
104       it = this.m_yPoints.iterator();
105       count = 1;
106       while (it.hasNext()) {
107         y[count] = ((Integer JavaDoc) it.next()).intValue();
108         count++;
109       }
110       y[count] = this.m_lastY;
111       // step down (or up) to the y=0 for the last value (in y)
112
y[count + 1] = this.m_chart.getYChartStart();
113       // step back to startx,starty (root)
114
y[count + 2] = this.m_chart.getYChartStart();
115
116       this.m_graphics.fillPolygon(x, y, x.length);
117     }
118   }
119
120   /**
121    * @see info.monitorenter.gui.chart.ITracePainter#paintPoint(int, int, int, int,
122    * java.awt.Graphics2D)
123    */

124   public void paintPoint(final int absoluteX, final int absoluteY, final int nextX,
125       final int nextY, final Graphics2D JavaDoc g) {
126
127     this.m_xPoints.add(new Integer JavaDoc(absoluteX));
128     this.m_yPoints.add(new Integer JavaDoc(absoluteY));
129     // don't loose the last point:
130
this.m_lastX = nextX;
131     this.m_lastY = nextY;
132     if (this.m_graphics == null) {
133       this.m_graphics = g;
134     }
135   }
136
137   /**
138    * @see info.monitorenter.gui.chart.ITracePainter#startPaintIteration()
139    */

140   public void startPaintIteration() {
141     this.m_xPoints = new LinkedList JavaDoc();
142     this.m_yPoints = new LinkedList JavaDoc();
143     this.m_graphics = null;
144   }
145 }
146
Popular Tags