Winstink
asked on
creating a chart in Java
I am trying to create a chart in Java that shows mortgage payments, that as the years go on, the amount of interest paid goes down, and the amount of principal paid goes up. The calculator itself works fine, but the chart only shows 7 years and only shows only the interest. It may be related, but when I hit "calculate" it changes the values.
well, perhaps post your code?
ASKER
yes, I realized I submitted it after I hit submit. sorry! tried to edit it, but then you commented.
Attached is the main calculator code, the chart code and a text file the calculator calls.
Attached is the main calculator code, the chart code and a text file the calculator calls.
import java.awt.BorderLayout; //Import Window Layout
import java.awt.GridLayout; //Import Grid Format Layout
import java.awt.event.ActionEvent; //Import Action
import java.awt.event.ActionListener; //Import Listener for Action
import javax.swing.JButton; //Import Button Use
import javax.swing.JFrame; //Import Frame Use
import javax.swing.JLabel; //Import Label Use
import javax.swing.JPanel; //Import Panel Use
import javax.swing.JTextField; //Import Text Field Use
import javax.swing.JTextArea; //Import Text Area Use
import javax.swing.JScrollPane; //Import Scroll Pane Use
import javax.swing.JComboBox; //Import Combo Box
import java.text.DecimalFormat; //Eliminates Spaces In Decimal Format
import javax.swing.JRadioButton; //Import Radio Button
import javax.swing.ButtonGroup; //Import Button Group
import javax.swing.JOptionPane; //Import Option Pane
import java.awt.Dimension; //Import Dimension
import java.io.*; //Import Input/Output package (Data streams, Filesystem, Serialization)
import java.util.StringTokenizer; //Import Utilities package (Collections, Event model, Date/time, International support)
//Creating the Mortgage Payment Class
public class SR_MF_03_CR7 extends JFrame
{
DecimalFormat DF = new DecimalFormat("$###,###.00");
private double Term; //Term of the Loan (in years)
private static int numberOfLinesGenerated=1;
//Graphical elements displaying current information
private JLabel LoanAmountLabel = new JLabel (" Please enter the loan Amount ");
private JTextField LoanAmountText = new JTextField (20);
private JLabel TermLabel = new JLabel(" Please enter Loan Term in Years ");
private JTextField TermText = new JTextField(6);
private JLabel InterestRateLabel = new JLabel(" Enter your Interest Rate ");
private JTextField InterestRateText = new JTextField (8);
//calling the radio buttons
private JRadioButton rbutton1 = new JRadioButton();
private JRadioButton rbutton2 = new JRadioButton();
private ButtonGroup buttongroup = new ButtonGroup();
private JTextArea result = new JTextArea(10, 20);
JScrollPane Scroll = new JScrollPane(result);
// Create Calculate and Reset buttons
private JButton ButtonCalculate = new JButton("Calculate");
private JButton ButtonReset = new JButton("Reset");
private JButton ButtonNext = new JButton("Next");
private JButton ButtonExit = new JButton("Exit");
private JButton ButtonChart = new JButton("Chart");
private JPanel Panel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JPanel nextButtonPanel = new JPanel();
JPanel BPanel=new JPanel(new BorderLayout());
String scheme[] = {"Select Interest Rate","7 years at 5.35%","15 years at 5.5%","30 years at 5.75%"};
private JLabel schemeLabel = new JLabel(" Please Select Loan Term ");
private JComboBox combo = new JComboBox(scheme);
public SR_MF_03_CR7() {
ButtonCalculate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
onButtonCalc();
}
});
ButtonReset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
onButtonReset();
}
});
ButtonNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
onButtonNext();
}
});
ButtonExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
ButtonChart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent w)
{
onButtonChart();
}
});
Panel.setLayout(new GridLayout(5, 2,6,10));
Panel.add(LoanAmountLabel);
Panel.add(LoanAmountText);
rbutton1.setText("Get Values From File OR ");
rbutton2.setText("Choose Values ");
buttongroup.add(rbutton1);
rbutton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rbutton1ActionPerformed(evt);
}
});
rbutton1.setSelected(true);
buttongroup.add(rbutton2);
rbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
rbutton2ActionPerformed(evt);
}
});
Panel.add(rbutton1);
Panel.add(rbutton2);
Panel.add(TermLabel);
Panel.add(TermText);
Panel.add(InterestRateLabel);
Panel.add(InterestRateText);
nextButtonPanel.add(ButtonChart);
nextButtonPanel.add(ButtonNext);
nextButtonPanel.add(ButtonExit);
setLayout(new BorderLayout());
add(Panel, BorderLayout.NORTH);
BPanel.add(Scroll, BorderLayout.SOUTH);
BPanel.add(nextButtonPanel, BorderLayout.NORTH);
ButtonExit.setEnabled(false);
ButtonNext.setEnabled(false);
ButtonChart.setEnabled(false);
add(BPanel, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE );
setResizable(false);
setTitle("Sample Mortgage Calculator");
setVisible(true);
}
private void rbutton1ActionPerformed(ActionEvent evt)
{
if(rbutton1.isSelected()){
Panel.removeAll();
Panel.add(LoanAmountLabel);
Panel.add(LoanAmountText);
Panel.add(rbutton1);
Panel.add(rbutton2);
Panel.add(TermLabel);
Panel.add(TermText);
Panel.add(InterestRateLabel);
Panel.add(InterestRateText);
Panel.add(ButtonCalculate);
Panel.add(ButtonReset);
setVisible(true);
}
}
private void rbutton2ActionPerformed(ActionEvent evt)
{
if(rbutton2.isSelected()){
Panel.removeAll();
Panel.add(LoanAmountLabel);
Panel.add(LoanAmountText);
Panel.add(rbutton1);
Panel.add(rbutton2);
Panel.add(schemeLabel);
Panel.add(combo);
Panel.add(ButtonCalculate);
Panel.add(ButtonReset);
setVisible(true);
}
}
//Calculate the information
public void onButtonCalc()
{
double LoanAmt=Double.parseDouble(LoanAmountText.getText());
double rate=0.0;
int term=0;
if(rbutton1.isSelected())
{
try
{
StreamTokenizer st=new StreamTokenizer(new BufferedReader(new FileReader("loandata.txt")));
int linecount=0;
while(st.nextToken()!=StreamTokenizer.TT_EOF)
{
if(st.lineno() ==numberOfLinesGenerated)
{
if(st.ttype==StreamTokenizer.TT_NUMBER)
term =(int) st.nval;
TermText.setText(""+st.nval);
st.nextToken();
if(st.ttype==StreamTokenizer.TT_NUMBER)
rate= st.nval;
InterestRateText.setText(""+st.nval);
break;
}
System.out.println (st.lineno()) ;
}
}catch (Exception addException){//Catch the exception if any
JOptionPane.showMessageDialog(null, "Error While reading Loandata file\n" + addException);
}
}
else
if(rbutton2.isSelected())
{
String str = (String)combo.getSelectedItem();
if(str.equals("7 years at 5.35%"))
{
rate = 5.35 ;
term = 7 ;
}
else
if(str.equals("10 years at 5.0%"))
{
rate = 5.0;
term = 10;
}
else
if(str.equals("30 years at 5.75%"))
{
rate = 5.75;
term = 30;
}
}
Term = term;
//create the new loan
Loan loan = new Loan(LoanAmt, rate, term);
//setting the result text
result.setText("Loan Amount = " + DF.format(LoanAmt)
+"\nInterest Rate = " + rate +"%"+"\nLength of Loan = "
+ Math.round(term* 12.0) + " months"+"\nMonthly Payment = "
+ DF.format(loan.ComputeMonthlyPayment())
+ loan.MonthlyloanAmount());//calling the ComputeMonthlyPayment() method of loan class
ButtonCalculate.setEnabled(false);
ButtonReset.setEnabled(false);
ButtonExit.setEnabled(true);
ButtonNext.setEnabled(true);
ButtonChart.setEnabled(true);
}
//Information for the buttons
public void onButtonReset()
{
LoanAmountText.setText("");
TermText.setText("");
InterestRateText.setText("");
result.setText("");
}
public void onButtonNext()
{
ButtonCalculate.setEnabled(true);
ButtonReset.setEnabled(true);
ButtonExit.setEnabled(false);
ButtonNext.setEnabled(false);
ButtonChart.setEnabled(true);
}
public void onButtonChart()
{
new Chart((int)(Term));
System.out.println(""+Term);
}
// Beginning of Main part of the calculator
public static void main(String[] args)
{
SR_MF_03_CR7 newloan = new SR_MF_03_CR7(); //Create an instance of class
}
}
//Loan class store loan data
class Loan {
double principal; //original amount of the loan
double interest; //amount of interest paid annually on the loan
int terminyears; //term of the loan (in years)
DecimalFormat DF = new DecimalFormat("$###,###.00");
//constructor
public Loan(double principal, double interest, int terminyears)
{
//initialize member instance
this.principal = principal;
this.interest = interest;
this.terminyears = terminyears;
}
//method to calculate and return the monthly payment
public double ComputeMonthlyPayment() {
double interestMonthly = 0; // Interest paid Each Month
double numberOfPayments = 0; //setting the number of payments
interestMonthly = ComputeMonthlyInterest( interest );
numberOfPayments = terminyears * 12.0;
return (principal * interestMonthly) / (1.0-(Math.pow((1.0 + interestMonthly),-numberOfPayments)));
}
public double ComputeMonthlyInterest( double interestYearly) {
return interest / (100.0 * 12.0);
}
public String MonthlyloanAmount() {
String result= "\n\nMonth\tLoanBalance\tInterest Paid\n" ;
double loanTerm = terminyears * 12.0;
double newloanTerm = loanTerm;
while (newloanTerm>0) {
double loanMonths = loanTerm-newloanTerm;
double newMonthlyBalance = principal*((Math.pow((1 + ComputeMonthlyInterest(interest)),loanTerm))-(Math.pow((1 + ComputeMonthlyInterest(interest)),loanMonths)))/(Math.pow((1 + ComputeMonthlyInterest(interest)),loanTerm)-1);
double monthlyamountInterest = newMonthlyBalance * ComputeMonthlyInterest(interest);
result+=(""+Math.round(newloanTerm) + "\t" + DF.format(newMonthlyBalance) + "\t" + DF.format(monthlyamountInterest) + "\n");
newloanTerm = newloanTerm - 1;
}
return result;
}
}
ASKER
i am an idiot. I only attached one part. Attached is the chart code and the text file.
import java.awt.*; // Calling common GUI elements
import javax.swing.*; // Used to make all the classes visible with using only one used at a time
public class Chart extends JFrame
{
// Serializing the public class GraphicChart
private static final long serialVersionUID = 1L;
private int Time;
//Method for the Chart
public Chart( int Time)
{
// Title of Calculator Graphic
super( "Johnny's Change Request 7" );
this.Time = Time;
// Sets size of screen
setSize( 1100, 710 );
setVisible( true );
}
// This is what paints the container
public void paint( Graphics ME )
{
// This forwards the paint to any lightweight component
super.paint( ME );
// Extends the graphic class to provide more control over coordinate transformations
Graphics2D DRAW = ( Graphics2D )ME;
// Sets the text on the x and y grid of chart
DRAW.setColor( Color.black );
DRAW.drawLine(110,30,110,655);
DRAW.drawLine(110,655,1100, 655);
DRAW.setFont( new Font("Font.PLAIN", Font.PLAIN , 19));
int TotalMort = 200000;
int line = 55 ;
for(int i=0; i< 660; i+=60){
// Setting up the Money line
DRAW.drawString("$"+ TotalMort, 30, 60+i);
DRAW.drawLine(105,line+i ,115,line+i);
TotalMort = TotalMort - 20000;
}
// Descriptor of bottom line for time
DRAW.drawString("Time in Years", 500, 690);
// Setting the 7,15,30 year loan lines
if(Time == 7){
int Number1 =0;
for(int i=0; i<= 990; i+=120)
{
// Sets line X and Y , also line color
DRAW.drawString(""+Number1, 107+i, 675);
DRAW.drawLine(110+i,650,110+i,660);
Number1++;
}
// Sets line and line color
DRAW.setColor( Color.blue );
DRAW.drawLine(110,55,950,655);
}
else
if(Time == 15){
int Number2 =0;
for(int i=0; i<= 990; i+=60)
{
// Sets line X and Y , also line color
DRAW.drawString(""+Number2, 107+i, 675);
DRAW.drawLine(110+i,650,110+i,655);
Number2++;
}
// Sets line and line color
DRAW.setColor( Color.orange );
DRAW.drawLine(110,55,1010,655);
}
else
if(Time == 30){
int Number3 =0;
for(int i=0; i<= 990; i+=30)
{
// Sets line X and Y , also line color
DRAW.drawString(""+Number3, 107+i, 675);
DRAW.drawLine(110+i,650,110+i,655);
Number3++;
}
// Sets line X and Y , also line color
DRAW.setColor( Color.red );
DRAW.drawLine(110,55,1010,655);
}
// Sets up Title to Chart
DRAW.setFont( new Font("Font.BOLD", Font.PLAIN , 21));
DRAW.drawString("McBride Financial Mortgage Calculator", 300, 50);
}
}//End
loandata.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.