import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.font.*;
import java.awt.Font;
import java.awt.geom.*;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.io.FileReader;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Mortgage5 extends JFrame implements ActionListener
{
JLabel Llabel; //amount label
JTextField Ltextfield;//amount textfield
JLabel Olabel;//option label
JComboBox options;//option combobox
JLabel Tlabel;//term label
JTextField Ttextfield;//term textfield
JLabel Rlabel;//rate label
JTextField Rtextfield;//rate textfield
JLabel Plabel; //payment label
JLabel $label; //field for monthly payment amount
JButton calculate; //calculate button
JButton reset; //reset button
JButton end; //end button
JTable table;//create table
JMenuItem mnuExit = new JMenuItem("Exit");
DefaultTableModel model;//table model
int[] trmArray;
double[] intrstArray;
JButton graph;
private float[] yearlyPrinciple;
private float[] yearlyInterest;
// header
public Mortgage5 ()
{
super("Mortgage Payment Calculator");
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
load();
init();
pack();
setVisible(true);
}
//reading from sequential file
public void load()
{
Reader fis;
try
{
fis = new FileReader("data.txt");
BufferedReader b = new BufferedReader( fis );
String[] line = b.readLine( ).split(",");
trmArray = new int[line.length];
for ( int i = 0; i < line.length; i++ )
{
trmArray[ i ] = Integer.parseInt(line[i].trim());
}
line = b.readLine( ).split(",");
intrstArray = new double[line.length];
for ( int i = 0; i < line.length; i++ )
{
intrstArray[ i ] = Double.parseDouble(line[i].trim());
}
b.close();
fis.close();
}
catch ( Exception e1 )
{
e1.printStackTrace( );
}
}
//creates labels, buttons and textfields
public void init()
{
Mortgage5Layout customLayout = new Mortgage5Layout();
Container con = getContentPane();
con.setLayout(customLayout);
con.setFont(new Font("Arial", Font.PLAIN, 12));
con.setLayout(customLayout);
Llabel = new JLabel("Mortgage Loan Amount $ (no comma)");//amount label
con.add(Llabel);
Ltextfield = new JTextField("");//amount textfield
con.add(Ltextfield);
Olabel = new JLabel("Preset Term & Interest Rate %");//option label
con.add(Olabel);
options = new JComboBox();
con.add(options);
options.addItem(" (choose rate)");
options.addItem("7 years at 5.35%");
options.addItem("15 years at 5.5%");
options.addItem("30 years at 5.75%");
options.setEnabled(false);
{
options.addItem("");
}
for (int i = 0; i < trmArray.length; i++)
{
options.addItem("");
}
Tlabel = new JLabel("Term (years)");//term label
con.add(Tlabel);
Ttextfield = new JTextField("");//term textfield
con.add(Ttextfield);
Rlabel = new JLabel("Interest Rate");//rate label
con.add(Rlabel);
Rtextfield = new JTextField("");//rate textfield
con.add(Rtextfield);
Plabel = new JLabel("Monthly Payment Amount");//payment label
con.add(Plabel);
$label = new JLabel("");//payment textfield
con.add($label);
calculate = new JButton("Calculate");//calculate button
con.add(calculate);
calculate.setBackground(Color.green);
reset = new JButton("Reset");//reset button
con.add(reset);
reset.setBackground(Color.yellow);
end = new JButton ("End");//end button
con.add(end);
end.setBackground(Color.red);
//table header names
String[] columnNames = {"Payment #","Payment Amount", "Interest", "Principle Reduction",
"Remaining Balance"};
//create table and table model
model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
JScrollPane scroll = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension (10, 600));
con.add (scroll);
graph = new JButton ("Display Graph");//Display Graph button
con.add(graph);
graph.setBackground(Color.blue);
//action listeners
Ltextfield.addActionListener(this); //loanfield
options.addActionListener(this); //interestfield
calculate.addActionListener(this); //calButtion
reset.addActionListener(this); //resetButton
end.addActionListener(this); //endButton
graph.addActionListener(this); //graphButton
}
//action event from listeners
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == calculate)
{
startCalculations();
}
if (source == reset)
{
reset();
}
if (source==options)
{
setRate();
}
if (source == end)
{
exit();
}
if (source == mnuExit)
{
exitGraph();
}
if (source == graph)
{
mFrame = new JFrame("Mortgage Graph");
mFrame.getContentPane().add(new GraphPanel(yearlyPrinciple, yearlyInterest));
mFrame.setSize(800,600);
mFrame.setLocation(200,100);
//trying to create menu
// Create an instance of the menu (Creates the Menu Bar)
JMenuBar mnuBar = new JMenuBar();
mFrame.setJMenuBar(mnuBar);
// Construct and populate the Exit menu (Creates the Exit Menu)
JMenu mnuExitbar = new JMenu ("End", true);
mnuBar.add(mnuExitbar);
mnuExitbar.add(mnuExit);
mFrame.setVisible(true);
//exit listener
mnuExit.addActionListener(this); //exitButton
}
}
public JFrame mFrame = new JFrame();
void exitGraph()
{
mFrame.dispose();
mFrame = null;
}
void setRate()
{
int index = options.getSelectedIndex();
//term and interest error check
if (index > 0)
{
try
{
Ttextfield.setText(Integer.toString(trmArray[index-1]));
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ttextfield.setText(null);
}
try
{
Rtextfield.setText(Double.toString(intrstArray[index-1]));
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Rtextfield.setText(null);
}
}
}
//calculation section
void startCalculations()
{
Thread thisThread = Thread.currentThread();
NumberFormat currency = NumberFormat.getCurrencyInstance();
double amt = 0;
double trm = 0;
double intrst = 0;
double moIn = 0;
double moTrm = 0;
double prin = 0;
double paymt = 0;
//amount error check
try
{
amt = Double.parseDouble(Ltextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Missing Amount or Use of Commas",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ltextfield.setText(null);
Ttextfield.setText(null);
Rtextfield.setText(null);
options.setSelectedIndex(0);
}
//term and interest error check
try
{
trm = Double.parseDouble(Ttextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ttextfield.setText(null);
}
try
{
intrst = Double.parseDouble(Rtextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Rtextfield.setText(null);
}
if (amt > 0)
{
amt = Double.parseDouble(Ltextfield.getText());
moIn = (intrst / 1200);//monthly interest rate
moTrm = trm * 12;//number of payments
paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));//amount forumla
yearlyPrinciple = new float[(int)trm]; // initialize the arrays to store yearly principle and interest
yearlyInterest = new float[(int)trm];
$label.setText("" + currency.format(paymt));
double newPrin = amt;
for (int i = 0; i < trm; i++)
{
yearlyInterest[i] = 0.0f; //start at 0.0
yearlyPrinciple[i] = 0.0f; //start at 0.0
for(int j = 1; j <=12; j++)
{
double newIn = moIn * newPrin;//monthly interest amount
double reduct = paymt - newIn;//monthly principle
yearlyInterest[i] += newIn; // accumalate the interest
yearlyPrinciple[i] += reduct; //accumalate the principle
newPrin = newPrin - reduct;//balance
//stops showing negative balance
if (newPrin < 0)
newPrin = 0;
else
newPrin = newPrin;
//inserts different amounts into the table
model.addRow(new Object[] { Integer.toString((i*12) + j), currency.format(paymt),
currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
}
//for testing purposes only
// model.addRow(new Object[] { Integer.toString(i), currency.format(0.0),
// currency.format(yearlyInterest[i]), currency.format(yearlyPrinciple[i]), currency.format(0.0) });
}
}
//less than 0 error check
if (amt < 0)
{
JOptionPane.showMessageDialog(null, "Please Enter Positie Numbers Only.",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ltextfield.setText(null);
}
}
//resets all fields
void reset()
{
Ltextfield.setText(null);
Ttextfield.setText(null);
Rtextfield.setText(null);
options.setSelectedIndex(0);
$label.setText(null);
model.setRowCount(0);
}
//exit the program with thank you message
void exit()
{
JOptionPane.showMessageDialog(null, " Thank you for using \n Mortgage Calculator",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Mortgage5().setVisible(true);
}
});
}
}
//creates class for container layout and placement
class Mortgage5Layout implements LayoutManager{
public Mortgage5Layout() {}
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}
public Dimension preferredLayoutSize(Container parent)
{
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 600 + insets.left + insets.right;
dim.height = 425 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent)
{
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,250,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+104,112,24);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,250,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+136,112,24);}
c = parent.getComponent(10);
if (c.isVisible()) {c.setBounds(insets.left+50,insets.top+168,96,24);}
c = parent.getComponent(11);
if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+168,112,24);}
c = parent.getComponent(12);
if (c.isVisible()) {c.setBounds(insets.left+400,insets.top+168,96,24);}
c = parent.getComponent(13);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);}
c = parent.getComponent(14);
if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+375,112,24);}
}
}
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);
}
// 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);
}
// 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;
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;
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 };
}
}
Help with displayJava Gui with display
Page 1 of 1
3 Replies - 905 Views - Last Post: 17 May 2008 - 09:51 AM
#1
Help with display
Posted 15 May 2008 - 06:53 PM
code compiles but does not display please help
Replies To: Help with display
#2
Re: Help with display
Posted 15 May 2008 - 09:44 PM
msnider9, on 15 May, 2008 - 06:53 PM, said:
code compiles but does not display please help
Add a few System.out.println() in your load() method.
I thing you have problem reading your data.txt file.
As I don't have a copy of your data.txt file I did the following modification to your code:
It was a guess....
int[] trmArray = {1000, 5000, 10000};
double[] intrstArray = {5.0, 10.0, 15.0};
//reading from sequential file
public void load()
{
return;
}
It works like a charm... calculating with I don't know which interest rate
The graph is well displayed
no way to select the interest rate thought... the comboBox is not enabled but setRate() used trmRate[0]
options.addItem(" (choose rate)");
options.addItem("7 years at 5.35%");
options.addItem("15 years at 5.5%");
options.addItem("30 years at 5.75%");
options.setEnabled(false);
for (int i = 0; i < trmArray.length; i++)
{
options.addItem("");
}
This seems weird... I gues you try to read years and interest from the file
If you read from the file why do you add hardcoded values ?
So what do you add "" for as many times you have a trmArray ?
#3
Re: Help with display
Posted 17 May 2008 - 01:14 AM
Still having a few issues. The menu choices are not working. What did I do wrong with them? I still do not think it is reading the rates.txt file so need to know what to change with it. Also I am getting a blank gui frame How do I get rid of it. Thanks for your help
Are the listeners correct for the menu options?
Here is the rates.txt sequential file
7,5.35
15,5.5
30,5.75
import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.font.*;
import java.awt.Font;
import java.awt.geom.*;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.io.FileReader;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Mortgage5 extends JFrame implements ActionListener
{
JLabel Llabel; //amount label
JTextField Ltextfield;//amount textfield
JLabel Olabel;//option label
JComboBox options;//option combobox
JLabel Tlabel;//term label
JTextField Ttextfield;//term textfield
JLabel Rlabel;//rate label
JTextField Rtextfield;//rate textfield
JLabel Plabel; //payment label
JLabel $label; //field for monthly payment amount
JButton calculate; //calculate button
JButton reset; //reset button
JButton end; //end button
JTable table;//create table
JMenuItem mnuExit = new JMenuItem("Exit");
DefaultTableModel model;//table model
int[] trmArray = {7,15,30};
double[] intrstArray = {5.35,5.5,5.75};
JButton graph;
private float[] yearlyPrinciple;
private float[] yearlyInterest;
// header
public Mortgage5()
{
super("Mortgage Payment Calculator");
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
load();
init();
pack();
setVisible(true);
// create a instance of a menu
MenuBar mnuMyBar = new MenuBar();
setMenuBar(mnuMyBar);
// constructs File menu and populates
Menu mnuMyFile = new Menu("File",true);
mnuMyBar.add(mnuMyFile);
MenuItem mnuMyFileExit = new MenuItem("Exit");
mnuMyFile.add(mnuMyFileExit);
// constructs File menu and populates
Menu mnuAvailLoans = new Menu("Available Loans", true);
mnuMyBar.add(mnuAvailLoans);
MenuItem mnuAvailLoans1 = new MenuItem("5.35% Interest at 7 Years");
mnuAvailLoans.add(mnuAvailLoans1);
MenuItem mnuAvailLoans2 = new MenuItem("5.50% Interest at 15 Years");
mnuAvailLoans.add(mnuAvailLoans2);
MenuItem mnuAvailLoans3 = new MenuItem("5.75% Interest at 30 Years");
mnuAvailLoans.add(mnuAvailLoans3);
// add ActionListener to menu items
mnuMyFileExit.addActionListener(this);
mnuAvailLoans1.addActionListener(this);
mnuAvailLoans2.addActionListener(this);
mnuAvailLoans3.addActionListener(this);
// add ActionCommand to menu items
mnuMyFileExit.setActionCommand("Exit");
mnuAvailLoans1.setActionCommand("Loans1");
mnuAvailLoans2.setActionCommand("Loans2");
mnuAvailLoans3.setActionCommand("Loans3");
mFrame.setVisible(true);
//exit listener
mnuExit.addActionListener(this); //exitButton
}
//reading from sequential file
public void load()
{
return;
}
Reader fis;
{
try
{
fis = new FileReader("rates.txt");
BufferedReader b = new BufferedReader( fis );
String[] line = b.readLine( ).split(",");
trmArray = new int[line.length];
for ( int i = 0; i < line.length; i++ )
{
trmArray[ i ] = Integer.parseInt(line[i].trim());
}
line = b.readLine( ).split(",");
intrstArray = new double[line.length];
for ( int i = 0; i < line.length; i++ )
{
intrstArray[ i ] = Double.parseDouble(line[i].trim());
}
b.close();
fis.close();
}
catch ( Exception e1 )
{
e1.printStackTrace( );
}
}
//creates labels, buttons and textfields
public void init()
{
MortgageLayout customLayout = new MortgageLayout();
Container con = getContentPane();
con.setLayout(customLayout);
con.setFont(new Font("Arial", Font.PLAIN, 12));
con.setLayout(customLayout);
Llabel = new JLabel("Mortgage Loan Amount $ (no comma)");//amount label
con.add(Llabel);
Ltextfield = new JTextField("");//amount textfield
con.add(Ltextfield);
Olabel = new JLabel("Preset Term & Interest Rate %");//option label
con.add(Olabel);
options = new JComboBox();
con.add(options);
options.addItem(" (choose rate)");
options.addItem("7 years at 5.35%");
options.addItem("15 years at 5.5%");
options.addItem("30 years at 5.75%");
options.setEnabled(false);
{
options.addItem("");
}
for (int i = 0; i < trmArray.length; i++)
{
options.addItem("");
}
Tlabel = new JLabel("Term (years)");//term label
con.add(Tlabel);
Ttextfield = new JTextField("");//term textfield
con.add(Ttextfield);
Rlabel = new JLabel("Interest Rate");//rate label
con.add(Rlabel);
Rtextfield = new JTextField("");//rate textfield
con.add(Rtextfield);
Plabel = new JLabel("Monthly Payment Amount");//payment label
con.add(Plabel);
$label = new JLabel("");//payment textfield
con.add($label);
calculate = new JButton("Calculate");//calculate button
con.add(calculate);
calculate.setBackground(Color.green);
reset = new JButton("Reset");//reset button
con.add(reset);
reset.setBackground(Color.yellow);
end = new JButton ("End");//end button
con.add(end);
end.setBackground(Color.red);
//table header names
String[] columnNames = {"Payment #","Payment Amount", "Interest", "Principle Reduction",
"Remaining Balance"};
//create table and table model
model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
JScrollPane scroll = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension (10, 600));
con.add (scroll);
graph = new JButton ("Display Graph");//Display Graph button
con.add(graph);
graph.setBackground(Color.blue);
//action listeners
Ltextfield.addActionListener(this); //loanfield
options.addActionListener(this); //interestfield
calculate.addActionListener(this); //calButtion
reset.addActionListener(this); //resetButton
end.addActionListener(this); //endButton
graph.addActionListener(this); //graphButton
}
//action event from listeners
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == calculate)
{
startCalculations();
}
if (source == reset)
{
reset();
}
if (source==options)
{
setRate();
}
if (source == end)
{
exit();
}
if (source == mnuExit)
{
exitGraph();
}
if (source == graph)
{
mFrame = new JFrame("Mortgage Graph");
mFrame.getContentPane().add(new GraphP(yearlyPrinciple, yearlyInterest));
mFrame.setSize(800,600);
mFrame.setLocation(200,100);
// create a instance of a menu
MenuBar mnuMyBar = new MenuBar();
setMenuBar(mnuMyBar);
// constructs File menu and populates
Menu mnuMyFile = new Menu("File",true);
mnuMyBar.add(mnuMyFile);
MenuItem mnuMyFileExit = new MenuItem("Exit");
mnuMyFile.add(mnuMyFileExit);
// constructs File menu and populates
Menu mnuAvailLoans = new Menu("Available Loans", true);
mnuMyBar.add(mnuAvailLoans);
MenuItem mnuAvailLoans1 = new MenuItem("5.35% Interest at 7 Years");
mnuAvailLoans.add(mnuAvailLoans1);
MenuItem mnuAvailLoans2 = new MenuItem("5.50% Interest at 15 Years");
mnuAvailLoans.add(mnuAvailLoans2);
MenuItem mnuAvailLoans3 = new MenuItem("5.75% Interest at 30 Years");
mnuAvailLoans.add(mnuAvailLoans3);
// add ActionListener to menu items
mnuMyFileExit.addActionListener(this);
mnuAvailLoans1.addActionListener(this);
mnuAvailLoans2.addActionListener(this);
mnuAvailLoans3.addActionListener(this);
// add ActionCommand to menu items
mnuMyFileExit.setActionCommand("Exit");
mnuAvailLoans1.setActionCommand("Loans1");
mnuAvailLoans2.setActionCommand("Loans2");
mnuAvailLoans3.setActionCommand("Loans3");
mFrame.setVisible(true);
//exit listener
mnuExit.addActionListener(this); //exitButton
}
}
public JFrame mFrame = new JFrame();
void exitGraph()
{
mFrame.dispose();
mFrame = null;
}
void setRate()
{
int index = options.getSelectedIndex();
//term and interest error check
if (index > 0)
{
try
{
Ttextfield.setText(Integer.toString(trmArray[index-1]));
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ttextfield.setText(null);
}
try
{
Rtextfield.setText(Double.toString(intrstArray[index-1]));
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Rtextfield.setText(null);
}
}
}
//calculation section
void startCalculations()
{
Thread thisThread = Thread.currentThread();
NumberFormat currency = NumberFormat.getCurrencyInstance();
double amt = 0;
double trm = 0;
double intrst = 0;
double moIn = 0;
double moTrm = 0;
double prin = 0;
double paymt = 0;
//amount error check
try
{
amt = Double.parseDouble(Ltextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Missing Amount or Use of Commas",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ltextfield.setText(null);
Ttextfield.setText(null);
Rtextfield.setText(null);
options.setSelectedIndex(0);
}
//term and interest error check
try
{
trm = Double.parseDouble(Ttextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ttextfield.setText(null);
}
try
{
intrst = Double.parseDouble(Rtextfield.getText());
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Invalid or missing Interest Rate. Please try again!",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Rtextfield.setText(null);
}
if (amt > 0)
{
amt = Double.parseDouble(Ltextfield.getText());
moIn = (intrst / 1200);//monthly interest rate
moTrm = trm * 12;//number of payments
paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));//amount forumla
yearlyPrinciple = new float[(int)trm]; // initialize the arrays to store yearly principle and interest
yearlyInterest = new float[(int)trm];
$label.setText("" + currency.format(paymt));
double newPrin = amt;
for (int i = 0; i < trm; i++)
{
yearlyInterest[i] = 0.0f; //start at 0.0
yearlyPrinciple[i] = 0.0f; //start at 0.0
for(int j = 1; j <=12; j++)
{
double newIn = moIn * newPrin;//monthly interest amount
double reduct = paymt - newIn;//monthly principle
yearlyInterest[i] += newIn; // accumalate the interest
yearlyPrinciple[i] += reduct; //accumalate the principle
newPrin = newPrin - reduct;//balance
//stops showing negative balance
if (newPrin < 0)
newPrin = 0;
else
newPrin = newPrin;
//inserts different amounts into the table
model.addRow(new Object[] { Integer.toString((i*12) + j), currency.format(paymt),
currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
}
//for testing purposes only
// model.addRow(new Object[] { Integer.toString(i), currency.format(0.0),
// currency.format(yearlyInterest[i]), currency.format(yearlyPrinciple[i]), currency.format(0.0) });
}
}
//less than 0 error check
if (amt < 0)
{
JOptionPane.showMessageDialog(null, "Please Enter Positive Numbers Only.",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
Ltextfield.setText(null);
}
}
//resets all fields
void reset()
{
Ltextfield.setText(null);
Ttextfield.setText(null);
Rtextfield.setText(null);
options.setSelectedIndex(0);
$label.setText(null);
model.setRowCount(0);
}
//exit the program with thank you message
void exit()
{
JOptionPane.showMessageDialog(null, " Thank you for using \n Mortgage Calculator",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Mortgage5().setVisible(true);
}
});
}
}
//creates class for container layout and placement
class MortgageLayout implements LayoutManager{
public MortgageLayout() {}
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}
public Dimension preferredLayoutSize(Container parent)
{
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 600 + insets.left + insets.right;
dim.height = 425 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent)
{
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent)
{
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,250,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+104,112,24);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,250,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+136,112,24);}
c = parent.getComponent(10);
if (c.isVisible()) {c.setBounds(insets.left+50,insets.top+168,96,24);}
c = parent.getComponent(11);
if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+168,112,24);}
c = parent.getComponent(12);
if (c.isVisible()) {c.setBounds(insets.left+400,insets.top+168,96,24);}
c = parent.getComponent(13);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);}
c = parent.getComponent(14);
if (c.isVisible()) {c.setBounds(insets.left+225,insets.top+375,112,24);}
}
}
class GraphP extends JPanel
{
final int
HPAD = 60,
VPAD = 40;
int[] data;
Font font;
float[] principleData;
float[] interestData;
public GraphP(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);
}
// 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);
}
// 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;
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;
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 };
}
}
Are the listeners correct for the menu options?
Here is the rates.txt sequential file
7,5.35
15,5.5
30,5.75
#4
Re: Help with display
Posted 17 May 2008 - 09:51 AM
msnider9, on 17 May, 2008 - 01:14 AM, said:
]
Still having a few issues. The menu choices are not working. What did I do wrong with them? I still do not think it is reading the rates.txt file so need to know what to change with it. Also I am getting a blank gui frame How do I get rid of it. Thanks for your help
Still having a few issues. The menu choices are not working. What did I do wrong with them? I still do not think it is reading the rates.txt file so need to know what to change with it. Also I am getting a blank gui frame How do I get rid of it. Thanks for your help
Listener not correct for the menu:
// add ActionListener to menu items
mnuMyFileExit.addActionListener(this);
mnuAvailLoans1.addActionListener(this);
mnuAvailLoans2.addActionListener(this);
mnuAvailLoans3.addActionListener(this);
// add ActionCommand to menu items
mnuMyFileExit.setActionCommand("Exit");
mnuAvailLoans1.setActionCommand("Loans1");
mnuAvailLoans2.setActionCommand("Loans2");
mnuAvailLoans3.setActionCommand("Loans3");
You do not check for ActionCommand Loans1, Loans2, Loans3
in your actionPerformed()
Even worst, in you select a Graph in your actionPerformed you do
if (source == graph)
{
// constructs File menu and populates
Menu mnuAvailLoans = new Menu("Available Loans", true);
mnuMyBar.add(mnuAvailLoans);
MenuItem mnuAvailLoans1 = new MenuItem("5.35% Interest at 7 Years");
mnuAvailLoans.add(mnuAvailLoans1);
MenuItem mnuAvailLoans2 = new MenuItem("5.50% Interest at 15 Years");
mnuAvailLoans.add(mnuAvailLoans2);
MenuItem mnuAvailLoans3 = new MenuItem("5.75% Interest at 30 Years");
mnuAvailLoans.add(mnuAvailLoans3);
// add ActionListener to menu items
mnuMyFileExit.addActionListener(this);
mnuAvailLoans1.addActionListener(this);
mnuAvailLoans2.addActionListener(this);
mnuAvailLoans3.addActionListener(this);
// add ActionCommand to menu items
mnuMyFileExit.setActionCommand("Exit");
mnuAvailLoans1.setActionCommand("Loans1");
mnuAvailLoans2.setActionCommand("Loans2");
mnuAvailLoans3.setActionCommand("Loans3");
When you will try to check for Loans1, Loans2, Loans3 it wont be obvious to find out if they are coming from which JFrame
The way to commented out load() is not good
//reading from sequential file
public void load()
{
return;
}
Reader fis;
{
try
{
Ok load() does nothing;
There is an instance variable fis of type Reader which is created
and the code
{
try
{
....
} // up to the close bracket
is executed as static code at the first creation of an object of type Mortgage5
you should do:
//reading from sequential file
public void load()
{
Reader fis = null;
if(fis == null)
return;
{
try
{
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|