| 1 |
//---PACKAGE DECLARATION--- |
| 2 |
|
| 3 |
//---IMPORTS--- |
| 4 |
|
| 5 |
import java.util.*; |
| 6 |
import java.awt.image.*; |
| 7 |
|
| 8 |
/** |
| 9 |
* |
| 10 |
* @author $Author: pjm2 $ |
| 11 |
* @version $Id: IscreamChart.java,v 1.2 2001/02/03 17:26:07 pjm2 Exp $ |
| 12 |
*/ |
| 13 |
public class IscreamChart { |
| 14 |
|
| 15 |
//---FINAL ATTRIBUTES--- |
| 16 |
|
| 17 |
/** |
| 18 |
* The current CVS revision of this class |
| 19 |
*/ |
| 20 |
public final String REVISION = "$Revision: 1.2 $"; |
| 21 |
|
| 22 |
//---STATIC METHODS--- |
| 23 |
|
| 24 |
//---CONSTRUCTORS--- |
| 25 |
|
| 26 |
public IscreamChart(long start, long end){ |
| 27 |
_minX = start; |
| 28 |
_maxX = end; |
| 29 |
_minY = 0; |
| 30 |
} |
| 31 |
|
| 32 |
//---PUBLIC METHODS--- |
| 33 |
|
| 34 |
public BufferedImage drawChart(PlotData pd, int width, int height, int gradeX, int gradeY, double maxY){ |
| 35 |
|
| 36 |
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| 37 |
|
| 38 |
int gradeColour = IscreamColour.ISCREAM_BLUE; |
| 39 |
int fgColour = IscreamColour.BLACK; |
| 40 |
int bgColour = IscreamColour.WHITE; |
| 41 |
|
| 42 |
|
| 43 |
// Fill in the background. |
| 44 |
for (int i = 0; i < width; i++){ |
| 45 |
for (int j = 0; j < height; j++){ |
| 46 |
img.setRGB(i, j, bgColour); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
// Draw the vertical grid lines. |
| 52 |
for (int i = 0; i <= gradeX; i++){ |
| 53 |
for (int j = 0; j < height; j++){ |
| 54 |
img.setRGB((i*width-1)/gradeX, j, gradeColour); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
// Draw the horizontal grid lines. |
| 60 |
for (int j = 0; j <= gradeY; j++){ |
| 61 |
for (int i = 0; i < width; i++){ |
| 62 |
img.setRGB(i, (j*height-1)/gradeY, gradeColour); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
// Plot each point with the foreground colour. |
| 68 |
Iterator it = pd.iterator(); |
| 69 |
while (it.hasNext()){ |
| 70 |
|
| 71 |
PairData p = (PairData)it.next(); |
| 72 |
|
| 73 |
// Scale each coordinate to fit on the graph perfectly. |
| 74 |
int x = (int)p.getX(); |
| 75 |
double yDouble = p.getY(); |
| 76 |
|
| 77 |
yDouble = ((p.getY() - _minY) * (double)height / (maxY - _minY)); |
| 78 |
|
| 79 |
int y = (int) yDouble; |
| 80 |
|
| 81 |
// Don't forget that y starts from the top! |
| 82 |
y = height - y - 1; |
| 83 |
|
| 84 |
// Make sure integer casting has not placed anything out of bounds. |
| 85 |
if (x >= width){ |
| 86 |
x = width - 1; |
| 87 |
} |
| 88 |
else if (x < 0){ |
| 89 |
x = 0; |
| 90 |
} |
| 91 |
if (y >= height){ |
| 92 |
y = height - 1; |
| 93 |
} |
| 94 |
else if (y < 0){ |
| 95 |
y = 0; |
| 96 |
} |
| 97 |
|
| 98 |
// Draw a line representing this value. |
| 99 |
for (int y2 = y; y2 < height; y2++){ |
| 100 |
img.setRGB(x, y2, fgColour); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
return img; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 110 |
* method to provide clean logging (every class should have this). |
| 111 |
* |
| 112 |
* @return the name of this class and its CVS revision |
| 113 |
*/ |
| 114 |
public String toString() { |
| 115 |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 116 |
} |
| 117 |
|
| 118 |
//---PRIVATE METHODS--- |
| 119 |
|
| 120 |
//---ACCESSOR/MUTATOR METHODS--- |
| 121 |
|
| 122 |
//---ATTRIBUTES--- |
| 123 |
|
| 124 |
// Boundary values. |
| 125 |
private double _minX, _maxX, _minY, _maxY; |
| 126 |
|
| 127 |
//---STATIC ATTRIBUTES--- |
| 128 |
|
| 129 |
} |