KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JLineGraph


1 package JSci.swing;
2
3 import java.awt.*;
4 import JSci.awt.*;
5 /**
6 * A line graph Swing component.
7 * There is some support for the handling of NaN values.
8 * @version 1.4
9 * @author Mark Hale
10 */

11 public class JLineGraph extends JGraph2D {
12         /**
13         * Constructs a line graph.
14         */

15         public JLineGraph(Graph2DModel gm) {
16                 super(gm);
17         }
18         /**
19         * Draws the graph data.
20         * Override this method to change how the graph data is plotted.
21         */

22         protected void drawData(Graphics g) {
23 // lines
24
Point p1, p2;
25                 int i;
26                 model.firstSeries();
27                 if(model.seriesLength() > 0) {
28                         g.setColor(seriesColor[0]);
29                         drawSeries(g);
30                 }
31                 for(int n=1;model.nextSeries();n++) {
32                         if(model.seriesLength() > 0) {
33                                 g.setColor(seriesColor[n]);
34                                 drawSeries(g);
35                         }
36                 }
37                 super.drawData(g);
38         }
39         private void drawSeries(Graphics g) {
40                 Point p1 = null;
41                 for(int i=0; i<model.seriesLength(); i++) {
42                         final float y = model.getYCoord(i);
43                         if(Float.isNaN(y)) {
44                                 p1 = null;
45                         } else if(p1 == null) {
46                                 p1 = dataToScreen(model.getXCoord(i), y);
47                         } else {
48                                 Point p2 = dataToScreen(model.getXCoord(i), y);
49                                 g.drawLine(p1.x, p1.y, p2.x, p2.y);
50                                 p1 = p2;
51                         }
52                 }
53         }
54 }
55
56
Popular Tags