View Single Post
  #3 (permalink)  
Old 07-31-2007, 04:59 AM
rhivka rhivka is offline
Member
 
Join Date: Jul 2007
Posts: 11
rhivka is on a distinguished road
Hi again hardwired ... So if I'm understanding you correctly, the graph code should look like the following? Because it was like this before and I thought it wouldn't work for some reason and changed a lot of variables. I thought they had to be principalAmts, interestAmts.

Code:
//creates class for graphpanel class GraphPanel extends JPanel { final int HPAD = 60, VPAD = 40; int[] data; Font font; float[] principleData; float[] interestData; public GraphPanel(float[] p, float[] i) { principleData = p; interestData = i; 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) / (interestData.length - 1);//11f; //distance between each plot 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("Principle", 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 < interestData.length; j++) { g2.draw(new Line2D.Double(x1, y1, x1, y2)); x1 += xInc; } // labels ys = h - VPAD; for(int j = 0; j < interestData.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 == (interestData.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 < interestData.length; j++) { g.setColor(Color.blue); y1 = VPAD + (h - 2*VPAD) - (principleData[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) - (interestData[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 = interestData.length -1; max = (int)principleData[j]; min = (int)interestData[j]; int span = max - min; return new int[] { min, max, span }; } } }
About the other changes... I kinda knew but forgot to make corrections and take out the comments.

One other thing... I wanted the input from the ComboBox to insert into the TextBoxes and then be calculated so I entered the following under ActionEvents:


Code:
if (command==options){ int index = options.getSelectedIndex(); if (index>=0) { termTxt.setText(termArray[index]); rateTxt.setText(pf.format(rateArray[index])); } }
But it's throwing the following error:

java:170: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (int)
termTxt.setText(termArray[index]);
Reply With Quote