Ok, I inserted the GraphPanel class and I managed to compile with no errors...yay!!
But... the UI is blank when the program starts up.
Is this something I did while inserting the GraphPanel? Here is what the program looks like from the "read from sequential file" section down. Could you take a look and see if I'm missing something? Or do you maybe know why it is blank already from the previous code? Thanks for your time.
//reading from sequential file
private void load() {
Reader fis;
try {
fis = new InputStreamReader(getClass().getResourceAsStream("data.txt"));
BufferedReader br = new BufferedReader( fis );
termArray = new int[4];
rateArray = new double[4];
String line;
int i = 0;
while((line = br.readLine()) != null) {
String[] s = line.split(",");
termArray[ i ] = Integer.parseInt(s[0].trim());
rateArray[ i ] = Double.parseDouble(s[1].trim());
i++;
}
br.close();
} catch ( Exception e1 ) {
e1.printStackTrace( );
}
}
class GraphPanel extends JPanel
{
final int
HPAD = 60,
VPAD = 40;
int[] data;
Font font;
float[] principalAmts;
float[] interestAmts;
public GraphPanel(float[] principalAmts, float[] interestAmts)
{
font = new Font("lucida sans regular", Font.PLAIN, 16);
setBackground(Color.white);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
int w = getWidth();
int h = getHeight();
// scales
float xInc = (w - HPAD - VPAD) / (interestAmts.length - 1);//11f; //distance between each point
float yInc = (h - 2*VPAD) / 10f;
int[] dataVals = getDataVals(); //min and max values for y-axis
float yScale = dataVals[2] / 10f;
// ordinate (y - axis)
g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
// plot tic marks
float x1 = HPAD, y1 = VPAD, x2 = HPAD - 3, y2;
for(int j = 0; j < 10; j++)
{
g2.draw(new Line2D.Double(x1, y1, x2, y1));
y1 += yInc;
}
// labels
String text; LineMetrics lm;
float xs, ys, textWidth, height;
for(int j = 0; j <= 10; j++)
{
text = String.valueOf(dataVals[1] - (int)(j * yScale));
textWidth = (float)font.getStringBounds(text, frc).getWidth();
lm = font.getLineMetrics(text, frc);
height = lm.getAscent();
xs = HPAD - textWidth - 7;
ys = VPAD + (j * yInc) + height/2;
g2.drawString(text, xs, ys);
if(j == 0) g2.drawString("Principal", xs, ys - 15);
}
// abcissa (x - axis)
g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
// tic marks
x1 = HPAD; y1 = h - VPAD; y2 = y1 + 3;
for(int j = 0; j < interestAmts.length; j++)
{
g2.draw(new Line2D.Double(x1, y1, x1, y2));
x1 += xInc;
}
// labels
ys = h - VPAD;
for(int j = 0; j < interestAmts.length; j++)
{
text = String.valueOf(j + 1);
textWidth = (float)font.getStringBounds(text, frc).getWidth();
lm = font.getLineMetrics(text, frc);
height = lm.getHeight();
xs = HPAD + j * xInc - textWidth/2;
g2.drawString(text, xs, ys + height);
if( j == (interestAmts.length - 1)) g2.drawString("Year", (int)w /2 , ys + height + 15);
}
// plot data
float yy2 = 0, yy1 = 0, xx2 = 0, xx1;
x1 = HPAD;
xx1 = HPAD;
yScale = (float)(h - 2*VPAD) / dataVals[2];
for(int j = 0; j < interestAmts.length; j++)
{
g.setColor(Color.blue);
y1 = VPAD + (h - 2*VPAD) - (principalAmts[j] - dataVals[0]) * yScale;
g2.fillOval((int)x1, (int)y1 - 2, 5, 5);
if(j > 0)
g2.draw(new Line2D.Double(x1, y1, x2, y2));
x2 = x1;
y2 = y1;
x1 += xInc;
g.setColor(Color.red);
yy1 = VPAD + (h - 2*VPAD) - (interestAmts[j] - dataVals[0]) * yScale;
g2.fillOval((int)xx1, (int)yy1 - 1, 5, 5);
if(j > 0)
g2.draw(new Line2D.Double(xx1, yy1, xx2, yy2));
xx2 = xx1;
yy2 = yy1;
xx1 += xInc;
}
}
private int[] getDataVals()
{
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int j = interestAmts.length -1;
max = (int)principalAmts[j];
min = (int)interestAmts[j];
int span = max - min;
return new int[] { min, max, span };
}
}
public static void main(String[] args) {
MC smc = new MC();
}
}