Posted Wednesday, October 17, 2007 @ 11:34:38
Ok, I think I'm more confused now than before. I made MonitorChart implement CellComponent:
public class MonitorChart extends JPanel implements CellComponent
Made MonitorChart the CellRenderer for the newly created column for it:
chartColumn.setUserCellRenderer(new MonitorChart(minY, maxY));
(MonitorChar gets instantiated once [ I sort of thought it would be once for each cell in the row] )
I load the data to plot using QuickTables setValueAt method for each row in the column:
for (int row = 0; row < qTable.getRowCount(); row++) {
qTable.setValueAt(dataArray.get(row), row, CHART_COLUMN - 1); // I created the new column with a '1' based nubmer system but have to reference it here with a '0' based number system
}
and now I expect that the Object that is passed to MonitorChart in the [CellCOmponent].setValue(Object o) method will be the same object that I set using the qTable.setValueAt() method above. What I do get is an object of the same Type as I set but now its contents are empty. I also expect that the graphics object in:
public void paint(Graphics g)
of MonitorChart will cover the part of the canvas that is for the cell at that row,column. However, its not, since when I force it to draw something its drawing it at the (0,0) point of the quickTable object.
Here is the entire MonitorChart class now (sorry, its gotten messy with all the changes)
public class MonitorChart
extends JPanel implements CellComponent {
double xMin = +9999999999999.9;
double yMin = +9999999999999.9;
double xMax = -9999999999999.9;
double yMax = -9999999999999.9;
ArrayList<DoublePoint> xyList = new ArrayList<DoublePoint> ();
int xyListSize = 0;
BorderLayout borderLayout_cell = new BorderLayout();
public MonitorChart(double yMin, double yMax) {
this.yMax = yMax;
this.yMin = yMin;
this.setLayout(borderLayout_cell);
this.setPreferredSize(new Dimension(40, 20));
this.setMinimumSize(new Dimension(30, 15));
}
/**
*
*/
private void computeMax() {
for (int i = 0; i < xyListSize; i++) {
if (xyList.get(i).getY() > yMax) {
yMax = xyList.get(i).getY();
}
if (xyList.get(i).getY() < yMin) {
yMin = xyList.get(i).getY();
}
if (xyList.get(i).getX() > xMax) {
xMax = xyList.get(i).getX();
}
if (xyList.get(i).getX() < xMin) {
xMin = xyList.get(i).getX();
}
}
}
/**
*
* @param aListener ActionListener
*/
public void addActionListener(ActionListener aListener) {
// we won't do anything since clicking on the chart does nothing for us
}
/**
*
* @param o Object
*/
public void setValue(Object o) {
if (o instanceof ArrayList) {
this.xyList = xyList;
xyListSize = xyList.size();
System.out.println("xyListSize upon setValue(Object o) " + xyListSize);
computeMax();
}
}
/**
*
* @return Object
*/
public Object getValue() {
return xyList;
}
/**
*
* @return JComponent
*/
public JComponent getComponent() {
return this;
}
/**
*
* @param g Graphics
*/
public void paint(Graphics g) {
if (xyList.size() < 2) {
System.out.println("No data in MonitorChart to plot");
return;
}
Graphics2D g2 = (Graphics2D) g;
// now paint the chart in the g2d object
double xScale = this.getWidth() / (xMax - xMin);
double yScale = this.getHeight() / (yMax - yMin);
g2.clearRect(0, 0, this.getWidth(), this.getHeight());
g2.setPaint(Color.red);
for (int i = 1; i < xyListSize; i++) {
// draw the line from xyList(i-1) to xyList(i)
g2.draw(new Line2D.Double(xyList.get(i - 1).getX() * xScale,
xyList.get(i - 1).getY() * yScale,
xyList.get(i).getX() * xScale,
xyList.get(i).getY() * yScale));
}
}
public String toString() {
return "MonitorChart class " + yMax + " " + yMin;
// return "";
}
}