?
Solved

Gives Cannot find Symbol even though I have declared already

Posted on 2009-02-22
16
Medium Priority
?
375 Views
Last Modified: 2012-05-06
Ok, program works, but then went to add more labels and text field for a balloon payment and get a "cannot find symbol"  error for the labels and text fields i have started.

what am i missing?


/* Joseph Roby
 * LoanProjectUI.java
 * Change Request #7
 * IT-307
 * Created on Feb 21, 2009
 */
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
 
public class LoanProjectUI7 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
    JLabel mLabel; //Month of Balloon Payment
	 JTextField mtextfield;
	 JLabel bLabel; //Amount of Balloon Payment
	 JTextField btextfield;  
	 JButton calculate;  //calculate button
    JButton reset;  //reset button
    JButton exit;  //exit 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;
	
 
 
 
 
   
    public LoanProjectUI7 ()
      {
       
		 //title
		  super("Loan Project 7");
        setDefaultLookAndFeelDecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        load();
        init();
        pack();
        setVisible(true);
      }
 
     //read from sequential file
     public void load()
     {
          Reader fis;
               try
               {
                    fis = new InputStreamReader(getClass().getResourceAsStream("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()
	 
     {
          LoanProjectUILayout customLayout = new LoanProjectUILayout();
 
          Container con = getContentPane();
          con.setLayout(customLayout);
 
          con.setFont(new Font("Tahoma", Font.PLAIN, 12));
          con.setLayout(customLayout);
 
          Llabel = new JLabel("Mortgage Loan Amount");//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();//option combobox
          con.add(options);
 
          MessageFormat mf = new MessageFormat("{0} years at {1,number,#.##}%");
          options.addItem(" (choose rate)");
          for (int i = 0; i < trmArray.length; i++)
          {
               options.addItem(mf.format(new Object[] { new Integer(trmArray[i]),
                                                            new Double(intrstArray[i])}));
          }
 
          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);
 
          reset = new JButton("Reset");//reset button
          con.add(reset);
 
          exit = new JButton ("Exit");//exit button
          con.add(exit);
 
          //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 ("Show Graph");//Display Graph button
          con.add(graph);
			 			 
			 mlabel = new JLabel("Month of Payment");//Month label
          con.add(mlabel);
 
          mtextfield = new JTextField("");//Month textfield
          con.add(mtextfield);
 
          blabel = new JLabel("Balloon Payment Amount");//Balloon label
          con.add(blabel);
 
          btextfield = new JTextField("");//Ballon Amount textfield
          con.add(btextfield);
 
 
          //action listeners
          Ltextfield.addActionListener(this); //loan amount field
          options.addActionListener(this); //interest rate field
          calculate.addActionListener(this);  //calculate Buttion
          reset.addActionListener(this); //reset Button
          exit.addActionListener(this);  //exit Button
          graph.addActionListener(this);  //graph Button
			 mtextfield.addActionListener(this); //month of balloon payment field
			 btextfield.addActionListener(this); //balloon payment amount field
     }
 
     private JFrame mFrame = null;
 
           //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 == exit)
                {
                     exit();
                }
 
                if (source == mnuExit)
                {
                  mFrame.dispose();
                mFrame = null;
                 }
                if (source == graph)
                {
                   //creates new JFrame for graph
                     mFrame = new JFrame("Loan Project Graph Output for Annual Principle and Interest");
                     mFrame.getContentPane().add(new GraphPanel(yearlyPrinciple, yearlyInterest));
                     mFrame.setSize(800,600);
                     mFrame.setLocation(200,100);
 
                   //creates menu bar
                   // 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 ("Exit", true);
                            mnuBar.add(mnuExitbar);
                          mnuExitbar.add(mnuExit);
 
                          mFrame.setVisible(true);
 
                     //exit listener
                      mnuExit.addActionListener(this);  //exit dropdown
                }
     }
 
          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!",
                                                            "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!",
                                                       "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    Rtextfield.setText(null);
               }
          }
     }
 
     //variables declarations
     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;
 
          //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);
          }
 
          try
          {
               trm = Double.parseDouble(Ttextfield.getText());
 
          }
 
          catch (NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term!",
                                                  "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!",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Rtextfield.setText(null);
          }
 
          if (amt > 0)
          {
               amt = Double.parseDouble(Ltextfield.getText());
               moIn = (intrst / 1200);
               moTrm = trm * 12;
               paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));
                  yearlyPrinciple = new float[(int)trm]; 
                  yearlyInterest = new float[(int)trm];
 
               $label.setText("" + currency.format(paymt));
 
               double newPrin = amt;
 
               for (int i = 0; i < trm; i++)
               {
                       yearlyInterest[i] = 0.0f; 
                         yearlyPrinciple[i] = 0.0f;
                       for(int j = 1; j <=12; j++)
                         {
                    double newIn = moIn * newPrin;
                    double reduct = paymt - newIn;
                         yearlyInterest[i] += newIn;
                         yearlyPrinciple[i] += reduct; 
                    newPrin = newPrin - reduct;
 
                	         if (newPrin < 0)
                 				        newPrin = 0;
                    			else
                       			  newPrin = newPrin;
 
                    //populates the table
                    model.addRow(new Object[] { Integer.toString((i*12) + j), currency.format(paymt),
                    currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
                    }
 
               }
          }
 
          //less then 0 error check
          if (amt < 0)
          {
               JOptionPane.showMessageDialog(null, "Positive Numbers Only.",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Ltextfield.setText(null);
          }
 
     }
 
     //resets fields
     void reset()
     {
          Ltextfield.setText(null);
          Ttextfield.setText(null);
          Rtextfield.setText(null);
			 mtextfield.setText(null);
			 bTextfield.setText(null);
          options.setSelectedIndex(0);
          $label.setText(null);
          model.setRowCount(0);
     }
 
     //exits the program
     void exit()
     {
 
          System.exit(0);
     }
 
     public static void main(String args[])
     {
          java.awt.EventQueue.invokeLater(new Runnable()
          {
               public void run()
               {
                    new LoanProjectUI7().setVisible(true);
               }
          });
     }
}
 
//layout and placement of GUI
class LoanProjectUILayout implements LayoutManager
{
     public LoanProjectUILayout() {}
 
     public void addLayoutComponent(String name, Component comp) {}
 
     public void removeLayoutComponent(Component comp) {}
 //sets up the dimensions of the panel
     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;
     }
 //sets up the locations of the components in the container
 
     public void layoutContainer(Container parent)
     {
          Insets insets = parent.getInsets();
 
          Component c;
          c = parent.getComponent(0); //L label (Mortgage Loan Amount)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);}
          c = parent.getComponent(1); // L textfield (Mortgage Loan Input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);}
          c = parent.getComponent(2); // O Label(Term and Interest Rate Options)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);}
          c = parent.getComponent(3); // JComboBox options (Term and Interest Options Box)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);}
          c = parent.getComponent(4); //T Label (Term)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);}
          c = parent.getComponent(5); // T textfield (term input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);}
          c = parent.getComponent(6); // R label (interest rate)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,250,24);}
          c = parent.getComponent(7); // R textfield (interest rate input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+104,112,24);}
          c = parent.getComponent(8); // P label (Monthly Payment Amount)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,250,24);}
          c = parent.getComponent(9); // $ label (label for monthly payment)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+136,112,24);}
          c = parent.getComponent(10); // JButton calculate 
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+375,96,24);}
          c = parent.getComponent(11); // JButton reset
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+375,112,24);}
          c = parent.getComponent(12); // JButton exit
               if (c.isVisible()) {c.setBounds(insets.left+450,insets.top+375,96,24);}
          c = parent.getComponent(13); // JTable table
         		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);}
          c = parent.getComponent(14); // JButton graph
         		if (c.isVisible()) {c.setBounds(insets.left+150,insets.top+375,112,24);}
			 c = parent.getComponent(15); //M Label (month of balloon payment)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+115,250,24);}
          c = parent.getComponent(16); // M textfield (month input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+115,96,24);}
          c = parent.getComponent(17); // B label (balloon payment)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+150,250,24);}
          c = parent.getComponent(18); // B textfield (Balloon Payment input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+150,112,24);}
			
			
			
     }
}
 
//coding for creation of Graph output Panel and the two charecteristics being graphed
class GraphPanel extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
    Font font;
     float[] principleData;
     float[] interestData;
 
 //the floats for principal and interest
    public GraphPanel(float[] p, float[] i)
    {
 
     principleData = p;
     interestData = i;
 
        font = new Font("Tahoma", Font.PLAIN, 16);
        setBackground(Color.gray);
    }
 
    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 for graph
        float xInc = (w - HPAD - VPAD) / (interestData.length - 1);  
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();       
        float yScale = dataVals[2] / 10f;
 
        //y 
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        
        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;
        }
        //  populates labels based upon user inputs or selections
        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);
        }
 
        //x
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        
        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;
        }
        // populates labels based upon user inputs or selections
        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);
        }
 
        // plots the data and sets the characteristics of the variables output
          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.black);
          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.orange);
          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 };
    }
}

Open in new window

0
Comment
Question by:UniversalFrost
  • 8
  • 5
  • 3
16 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 23706621
Change

>>
JLabel mLabel; //Month of Balloon Payment
JLabel bLabel; //Amount of Balloon Payment
>>

to

JLabel mlabel; //Month of Balloon Payment
JLabel blabel; //Amount of Balloon Payment



0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 2000 total points
ID: 23706628
And

btextfield.setText(null);

has case problems too
0
 

Author Comment

by:UniversalFrost
ID: 23706697
thanks, i am down with the flu right now and taking alot of meds. I am suprised i got this far with it. i appreciate the help. man the simple problems are the ones that kill ya.

ya, i know it is messed up, but it is just for a simple undergrad java class i am taking (university wouldn't accept my transfer credits) and could care less other than it works and i get the points so i can be done with the class and continue on with my other masters classes.

thanks again.

JOE
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 92

Expert Comment

by:objects
ID: 23706706
try this:

/* Joseph Roby
 * LoanProjectUI.java
 * Change Request #7
 * IT-307
 * Created on Feb 21, 2009
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Line2D;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.MessageFormat;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class LoanProjectUI7 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
      //JLabel mLabel; // Month of Balloon Payment
      JTextField mtextfield;
      //JLabel bLabel; // Amount of Balloon Payment
      JTextField btextfield;
      JButton calculate; // calculate button
      JButton reset; // reset button
      JButton exit; // exit 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;

      public LoanProjectUI7() {

            // title
            super("Loan Project 7");
            setDefaultLookAndFeelDecorated(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            load();
            init();
            pack();
            setVisible(true);
      }

      // read from sequential file
      public void load() {
            Reader fis;
            try {
                  fis = new InputStreamReader(getClass().getResourceAsStream(
                              "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()

      {
            LoanProjectUILayout customLayout = new LoanProjectUILayout();

            Container con = getContentPane();
            con.setLayout(customLayout);

            con.setFont(new Font("Tahoma", Font.PLAIN, 12));
            con.setLayout(customLayout);

            Llabel = new JLabel("Mortgage Loan Amount");// 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();// option combobox
            con.add(options);

            MessageFormat mf = new MessageFormat("{0} years at {1,number,#.##}%");
            options.addItem(" (choose rate)");
            for (int i = 0; i < trmArray.length; i++) {
                  options.addItem(mf.format(new Object[] { new Integer(trmArray[i]),
                              new Double(intrstArray[i]) }));
            }

            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);

            reset = new JButton("Reset");// reset button
            con.add(reset);

            exit = new JButton("Exit");// exit button
            con.add(exit);

            // 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("Show Graph");// Display Graph button
            con.add(graph);

            JLabel mlabel = new JLabel("Month of Payment");// Month label
            con.add(mlabel);

            mtextfield = new JTextField("");// Month textfield
            con.add(mtextfield);

            JLabel blabel = new JLabel("Balloon Payment Amount");// Balloon label
            con.add(blabel);

            btextfield = new JTextField("");// Ballon Amount textfield
            con.add(btextfield);

            // action listeners
            Ltextfield.addActionListener(this); // loan amount field
            options.addActionListener(this); // interest rate field
            calculate.addActionListener(this); // calculate Buttion
            reset.addActionListener(this); // reset Button
            exit.addActionListener(this); // exit Button
            graph.addActionListener(this); // graph Button
            mtextfield.addActionListener(this); // month of balloon payment field
            btextfield.addActionListener(this); // balloon payment amount field
      }

      private JFrame mFrame = null;

      // 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 == exit) {
                  exit();
            }

            if (source == mnuExit) {
                  mFrame.dispose();
                  mFrame = null;
            }
            if (source == graph) {
                  // creates new JFrame for graph
                  mFrame = new JFrame(
                              "Loan Project Graph Output for Annual Principle and Interest");
                  mFrame.getContentPane().add(
                              new GraphPanel(yearlyPrinciple, yearlyInterest));
                  mFrame.setSize(800, 600);
                  mFrame.setLocation(200, 100);

                  // creates menu bar
                  // 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("Exit", true);
                  mnuBar.add(mnuExitbar);
                  mnuExitbar.add(mnuExit);

                  mFrame.setVisible(true);

                  // exit listener
                  mnuExit.addActionListener(this); // exit dropdown
            }
      }

      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!", "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!", "Message Dialog",
                                    JOptionPane.PLAIN_MESSAGE);
                        Rtextfield.setText(null);
                  }
            }
      }

      // variables declarations
      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;

            // 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);
            }

            try {
                  trm = Double.parseDouble(Ttextfield.getText());

            }

            catch (NumberFormatException e) {
                  JOptionPane.showMessageDialog(null,
                              "Invalid or missing Loan Term!", "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!", "Message Dialog",
                              JOptionPane.PLAIN_MESSAGE);
                  Rtextfield.setText(null);
            }

            if (amt > 0) {
                  amt = Double.parseDouble(Ltextfield.getText());
                  moIn = (intrst / 1200);
                  moTrm = trm * 12;
                  paymt = (amt * moIn) / (1 - Math.pow((1 + moIn), -moTrm));
                  yearlyPrinciple = new float[(int) trm];
                  yearlyInterest = new float[(int) trm];

                  $label.setText("" + currency.format(paymt));

                  double newPrin = amt;

                  for (int i = 0; i < trm; i++) {
                        yearlyInterest[i] = 0.0f;
                        yearlyPrinciple[i] = 0.0f;
                        for (int j = 1; j <= 12; j++) {
                              double newIn = moIn * newPrin;
                              double reduct = paymt - newIn;
                              yearlyInterest[i] += newIn;
                              yearlyPrinciple[i] += reduct;
                              newPrin = newPrin - reduct;

                              if (newPrin < 0)
                                    newPrin = 0;
                              else
                                    newPrin = newPrin;

                              // populates the table
                              model
                                          .addRow(new Object[] {
                                                      Integer.toString((i * 12) + j),
                                                      currency.format(paymt),
                                                      currency.format(newIn),
                                                      currency.format(reduct),
                                                      currency.format(newPrin) });
                        }

                  }
            }

            // less then 0 error check
            if (amt < 0) {
                  JOptionPane.showMessageDialog(null, "Positive Numbers Only.",
                              "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                  Ltextfield.setText(null);
            }

      }

      // resets fields
      void reset() {
            Ltextfield.setText(null);
            Ttextfield.setText(null);
            Rtextfield.setText(null);
            mtextfield.setText(null);
            btextfield.setText(null);
            options.setSelectedIndex(0);
            $label.setText(null);
            model.setRowCount(0);
      }

      // exits the program
      void exit() {

            System.exit(0);
      }

      public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                        new LoanProjectUI7().setVisible(true);
                  }
            });
      }
}

// layout and placement of GUI
class LoanProjectUILayout implements LayoutManager {
      public LoanProjectUILayout() {
      }

      public void addLayoutComponent(String name, Component comp) {
      }

      public void removeLayoutComponent(Component comp) {
      }

      // sets up the dimensions of the panel
      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;
      }

      // sets up the locations of the components in the container

      public void layoutContainer(Container parent) {
            Insets insets = parent.getInsets();

            Component c;
            c = parent.getComponent(0); // L label (Mortgage Loan Amount)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 8, 250, 24);
            }
            c = parent.getComponent(1); // L textfield (Mortgage Loan Input)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 8, 175, 24);
            }
            c = parent.getComponent(2); // O Label(Term and Interest Rate Options)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 40, 250, 24);
            }
            c = parent.getComponent(3); // JComboBox options (Term and Interest
                                                      // Options Box)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 40, 150, 24);
            }
            c = parent.getComponent(4); // T Label (Term)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 72, 250, 24);
            }
            c = parent.getComponent(5); // T textfield (term input)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 72, 96, 24);
            }
            c = parent.getComponent(6); // R label (interest rate)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 104, 250, 24);
            }
            c = parent.getComponent(7); // R textfield (interest rate input)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 104, 112, 24);
            }
            c = parent.getComponent(8); // P label (Monthly Payment Amount)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 136, 250, 24);
            }
            c = parent.getComponent(9); // $ label (label for monthly payment)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 136, 112, 24);
            }
            c = parent.getComponent(10); // JButton calculate
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 375, 96, 24);
            }
            c = parent.getComponent(11); // JButton reset
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 375, 112, 24);
            }
            c = parent.getComponent(12); // JButton exit
            if (c.isVisible()) {
                  c.setBounds(insets.left + 450, insets.top + 375, 96, 24);
            }
            c = parent.getComponent(13); // JTable table
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 200, 575, 160);
            }
            c = parent.getComponent(14); // JButton graph
            if (c.isVisible()) {
                  c.setBounds(insets.left + 150, insets.top + 375, 112, 24);
            }
            c = parent.getComponent(15); // M Label (month of balloon payment)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 115, 250, 24);
            }
            c = parent.getComponent(16); // M textfield (month input)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 115, 96, 24);
            }
            c = parent.getComponent(17); // B label (balloon payment)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 8, insets.top + 150, 250, 24);
            }
            c = parent.getComponent(18); // B textfield (Balloon Payment input)
            if (c.isVisible()) {
                  c.setBounds(insets.left + 300, insets.top + 150, 112, 24);
            }

      }
}

// coding for creation of Graph output Panel and the two charecteristics being
// graphed
class GraphPanel extends JPanel {
      final int HPAD = 60, VPAD = 40;
      int[] data;
      Font font;
      float[] principleData;
      float[] interestData;

      // the floats for principal and interest
      public GraphPanel(float[] p, float[] i) {

            principleData = p;
            interestData = i;

            font = new Font("Tahoma", Font.PLAIN, 16);
            setBackground(Color.gray);
      }

      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 for graph
            float xInc = (w - HPAD - VPAD) / (interestData.length - 1);
            float yInc = (h - 2 * VPAD) / 10f;
            int[] dataVals = getDataVals();
            float yScale = dataVals[2] / 10f;

            // y
            g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));

            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;
            }
            // populates labels based upon user inputs or selections
            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);
            }

            // x
            g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));

            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;
            }
            // populates labels based upon user inputs or selections
            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);
            }

            // plots the data and sets the characteristics of the variables output
            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.black);
                  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.orange);
                  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 };
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23706781
:-)
0
 

Author Comment

by:UniversalFrost
ID: 23706792
yeah, i am an old VMS guy myself.

so if anybody needs VMS help then ask me.

Thanks again.

JOE
0
 
LVL 92

Expert Comment

by:objects
ID: 23706801
been a lot of years since I looked after vms boxes, is vms still being used

0
 

Author Comment

by:UniversalFrost
ID: 23706808
yup,

I work for the gov't and they love it still. it is very stable and as long as programmers are alive they will keep it.

at least the longer it is around the more they have to pay ya, since the code is outliving the programmers

;~}

0
 

Author Comment

by:UniversalFrost
ID: 23706822
oh yeah foregot to mention i still work on old DEC alpha proc boxes as well.

I'm living in the stone age and lov'n it.

0
 
LVL 92

Expert Comment

by:objects
ID: 23706863
VMS and a DEC alpha, that takes me back :)
Let me know if you ever need any further java guidance

0
 

Author Comment

by:UniversalFrost
ID: 23707259
ok, got everything to work except for the addition of the balloon payment
[code]


 if (paymt == paymentMonth)
      {newPrin = newPrin - balloonPayment;
                              }

[/code]

any help is appreciated.

JOE
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
 
public class LoanProjectUI7 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 exit;  //exit 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;
	 JLabel mlabel; //Month of Balloon Payment
	 JTextField mtextfield;
	 JLabel blabel; //Amount of Balloon Payment
	 JTextField btextfield;
 
 
 
 
   
    public LoanProjectUI7 ()
      {
       
		 //title
		  super("Loan Project 7");
        setDefaultLookAndFeelDecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        load();
        init();
        pack();
        setVisible(true);
      }
 
     //read from sequential file
     public void load()
     {
          Reader fis;
               try
               {
                    fis = new InputStreamReader(getClass().getResourceAsStream("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()
	 
     {
          LoanProjectUILayout customLayout = new LoanProjectUILayout();
 
          Container con = getContentPane();
          con.setLayout(customLayout);
 
          con.setFont(new Font("Tahoma", Font.PLAIN, 12));
          con.setLayout(customLayout);
 
          Llabel = new JLabel("Mortgage Loan Amount");//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();//option combobox
          con.add(options);
 
          MessageFormat mf = new MessageFormat("{0} years at {1,number,#.##}%");
          options.addItem(" (choose rate)");
          for (int i = 0; i < trmArray.length; i++)
          {
               options.addItem(mf.format(new Object[] { new Integer(trmArray[i]),
                                                            new Double(intrstArray[i])}));
          }
 
          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);
 
          reset = new JButton("Reset");//reset button
          con.add(reset);
 
          exit = new JButton ("Exit");//exit button
          con.add(exit);
 
          //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 ("Show Graph");//Display Graph button
          con.add(graph);
			 			 
			 mlabel = new JLabel("Month of Payment");//Month label
          con.add(mlabel);
 
          mtextfield = new JTextField("");//Month textfield
          con.add(mtextfield);
 
          blabel = new JLabel("Balloon Payment Amount");//Balloon label
          con.add(blabel);
 
          btextfield = new JTextField("");//Ballon Amount textfield
          con.add(btextfield);
 
 
          //action listeners
          Ltextfield.addActionListener(this); //loan amount field
          options.addActionListener(this); //interest rate field
          calculate.addActionListener(this);  //calculate Buttion
          reset.addActionListener(this); //reset Button
          exit.addActionListener(this);  //exit Button
          graph.addActionListener(this);  //graph Button
			 mtextfield.addActionListener(this); //month of balloon payment field
			 btextfield.addActionListener(this); //balloon payment amount field
     }
 
     private JFrame mFrame = null;
 
           //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 == exit)
                {
                     exit();
                }
 
                if (source == mnuExit)
                {
                  mFrame.dispose();
                mFrame = null;
                 }
                if (source == graph)
                {
                   //creates new JFrame for graph
                     mFrame = new JFrame("Loan Project Graph Output for Annual Principle and Interest");
                     mFrame.getContentPane().add(new GraphPanel(yearlyPrinciple, yearlyInterest));
                     mFrame.setSize(800,600);
                     mFrame.setLocation(200,100);
 
                   //creates menu bar
                   // 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 ("Exit", true);
                            mnuBar.add(mnuExitbar);
                          mnuExitbar.add(mnuExit);
 
                          mFrame.setVisible(true);
 
                     //exit listener
                      mnuExit.addActionListener(this);  //exit dropdown
                }
     }
 
          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!",
                                                            "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!",
                                                       "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    Rtextfield.setText(null);
               }
          }
     }
 
     //variables declarations
     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;
 			 double paymentMonth = 0;
			 double balloonPayment = 0;
			 
			 
          //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);
					mtextfield.setText(null);
					btextfield.setText(null);
               options.setSelectedIndex(0);
          }
 
          try
          {
               trm = Double.parseDouble(Ttextfield.getText());
 
          }
 
          catch (NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term!",
                                                  "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!",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Rtextfield.setText(null);
          }
 
          if (amt > 0)
          {
               amt = Double.parseDouble(Ltextfield.getText());
               moIn = (intrst / 1200);
               moTrm = trm * 12;
               paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));
                  yearlyPrinciple = new float[(int)trm]; 
                  yearlyInterest = new float[(int)trm];
 
               $label.setText("" + currency.format(paymt));
 
               double newPrin = amt;
					
 				 if (paymt == paymentMonth)
					{newPrin = newPrin - balloonPayment;
					}
		
               for (int i = 0; i < trm; i++)
               
										
					{
                       yearlyInterest[i] = 0.0f; 
                         yearlyPrinciple[i] = 0.0f;
                       for(int j = 1; j <=12; j++)
                         {
                    double newIn = moIn * newPrin;
                    double reduct = paymt - newIn;
                         yearlyInterest[i] += newIn;
                         yearlyPrinciple[i] += reduct; 
                    newPrin = newPrin - reduct;
						  
						 
						
                	         if (newPrin < 0)
                 				        newPrin = 0;
                    			else
                       			  newPrin = newPrin;
 
                    //populates the table
                    model.addRow(new Object[] { Integer.toString((i*12) + j), currency.format(paymt),
                    currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
                    }
 
               }
          }
 
          //less then 0 error check
          if (amt < 0)
          {
               JOptionPane.showMessageDialog(null, "Positive Numbers Only.",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Ltextfield.setText(null);
          }
 
     }
 
     //resets fields
     void reset()
     {
          Ltextfield.setText(null);
          Ttextfield.setText(null);
          Rtextfield.setText(null);
			 mtextfield.setText(null);
			 btextfield.setText(null);
          options.setSelectedIndex(0);
          $label.setText(null);
          model.setRowCount(0);
     }
 
     //exits the program
     void exit()
     {
 
          System.exit(0);
     }
 
     public static void main(String args[])
     {
          java.awt.EventQueue.invokeLater(new Runnable()
          {
               public void run()
               {
                    new LoanProjectUI7().setVisible(true);
               }
          });
     }
}
 
	
 
//layout and placement of GUI
class LoanProjectUILayout implements LayoutManager
{
     public LoanProjectUILayout() {}
 
     public void addLayoutComponent(String name, Component comp) {}
 
     public void removeLayoutComponent(Component comp) {}
 //sets up the dimensions of the panel
     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;
     }
 //sets up the locations of the components in the container
 
     public void layoutContainer(Container parent)
     {
          Insets insets = parent.getInsets();
 
          Component c;
          c = parent.getComponent(0); //L label (Mortgage Loan Amount)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);}
          c = parent.getComponent(1); // L textfield (Mortgage Loan Input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);}
          c = parent.getComponent(2); // O Label(Term and Interest Rate Options)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);}
          c = parent.getComponent(3); // JComboBox options (Term and Interest Options Box)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);}
          c = parent.getComponent(4); //T Label (Term)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);}
          c = parent.getComponent(5); // T textfield (term input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);}
          c = parent.getComponent(6); // R label (interest rate)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+96,250,24);}
          c = parent.getComponent(7); // R textfield (interest rate input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+96,112,24);}
          c = parent.getComponent(8); // P label (Monthly Payment Amount)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+120,250,24);}
          c = parent.getComponent(9); // $ label (label for monthly payment)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+120,112,24);}
          c = parent.getComponent(10); // JButton calculate 
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+375,96,24);}
          c = parent.getComponent(11); // JButton reset
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+375,112,24);}
          c = parent.getComponent(12); // JButton exit
               if (c.isVisible()) {c.setBounds(insets.left+450,insets.top+375,96,24);}
          c = parent.getComponent(13); // JTable table
         		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);}
          c = parent.getComponent(14); // JButton graph
         		if (c.isVisible()) {c.setBounds(insets.left+150,insets.top+375,112,24);}
			 c = parent.getComponent(15); //M Label (month of balloon payment)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+143,250,24);}
          c = parent.getComponent(16); // M textfield (month input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+143,96,24);}
          c = parent.getComponent(17); // B label (balloon payment)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+170,250,24);}
          c = parent.getComponent(18); // B textfield (Balloon Payment input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+170,112,24);}
			
			
 
     }
}
 
//coding for creation of Graph output Panel and the two charecteristics being graphed
class GraphPanel extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
    Font font;
     float[] principleData;
     float[] interestData;
 
 //the floats for principal and interest
    public GraphPanel(float[] p, float[] i)
    {
 
     principleData = p;
     interestData = i;
 
        font = new Font("Tahoma", Font.PLAIN, 16);
        setBackground(Color.gray);
    }
 
    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 for graph
        float xInc = (w - HPAD - VPAD) / (interestData.length - 1);  
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();       
        float yScale = dataVals[2] / 10f;
 
        //y 
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        
        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;
        }
        //  populates labels based upon user inputs or selections
        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);
        }
 
        //x
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        
        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;
        }
        // populates labels based upon user inputs or selections
        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);
        }
 
        // plots the data and sets the characteristics of the variables output
          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.black);
          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.orange);
          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 };
    }
}

Open in new window

0
 

Author Comment

by:UniversalFrost
ID: 23707274
basically just trying to add in a balloon payment for a given month specified and then subtract from the principal and continue on from there.

the flu is really kicking my rear and i am just in a daze right now.

Thanks in advance for all the help guys and gals.
JOE
0
 
LVL 92

Expert Comment

by:objects
ID: 23707345
you never calculate paymentMonth
0
 

Author Comment

by:UniversalFrost
ID: 23707711
ok, i added that, but i can't get it to work properly and I have to add 0's now for the month and ballon amount to get it to display the other amoutns correctly

sorry i am draggin this out, but assignment due soon and i am fryed with the flu and medications right now.
/* Joseph Roby
 * LoanProjectUI.java
 * Change Request #7
 * IT-307
 * Created on Feb 21, 2009
 */
 
 
 
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
 
public class LoanProjectUI7 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 exit;  //exit 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;
	 JLabel mlabel; //Month of Balloon Payment
	 JTextField mtextfield;
	 JLabel blabel; //Amount of Balloon Payment
	 JTextField btextfield;
 
 
 
 
   
    public LoanProjectUI7 ()
      {
       
		 //title
		  super("Loan Project 7");
        setDefaultLookAndFeelDecorated(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        load();
        init();
        pack();
        setVisible(true);
      }
 
     //read from sequential file
     public void load()
     {
          Reader fis;
               try
               {
                    fis = new InputStreamReader(getClass().getResourceAsStream("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()
	 
     {
          LoanProjectUILayout customLayout = new LoanProjectUILayout();
 
          Container con = getContentPane();
          con.setLayout(customLayout);
 
          con.setFont(new Font("Tahoma", Font.PLAIN, 12));
          con.setLayout(customLayout);
 
          Llabel = new JLabel("Mortgage Loan Amount");//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();//option combobox
          con.add(options);
 
          MessageFormat mf = new MessageFormat("{0} years at {1,number,#.##}%");
          options.addItem(" (choose rate)");
          for (int i = 0; i < trmArray.length; i++)
          {
               options.addItem(mf.format(new Object[] { new Integer(trmArray[i]),
                                                            new Double(intrstArray[i])}));
          }
 
          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);
 
          reset = new JButton("Reset");//reset button
          con.add(reset);
 
          exit = new JButton ("Exit");//exit button
          con.add(exit);
 
          //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 ("Show Graph");//Display Graph button
          con.add(graph);
			 			 
			 mlabel = new JLabel("Month of Payment");//Month label
          con.add(mlabel);
 
          mtextfield = new JTextField("");//Month textfield
          con.add(mtextfield);
 
          blabel = new JLabel("Balloon Payment Amount");//Balloon label
          con.add(blabel);
 
          btextfield = new JTextField("");//Ballon Amount textfield
          con.add(btextfield);
 
 
          //action listeners
          Ltextfield.addActionListener(this); //loan amount field
          options.addActionListener(this); //interest rate field
          calculate.addActionListener(this);  //calculate Buttion
          reset.addActionListener(this); //reset Button
          exit.addActionListener(this);  //exit Button
          graph.addActionListener(this);  //graph Button
			 mtextfield.addActionListener(this); //month of balloon payment field
			 btextfield.addActionListener(this); //balloon payment amount field
     }
 
     private JFrame mFrame = null;
 
           //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 == exit)
                {
                     exit();
                }
 
                if (source == mnuExit)
                {
                  mFrame.dispose();
                mFrame = null;
                 }
                if (source == graph)
                {
                   //creates new JFrame for graph
                     mFrame = new JFrame("Loan Project Graph Output for Annual Principle and Interest");
                     mFrame.getContentPane().add(new GraphPanel(yearlyPrinciple, yearlyInterest));
                     mFrame.setSize(800,600);
                     mFrame.setLocation(200,100);
 
                   //creates menu bar
                   // 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 ("Exit", true);
                            mnuBar.add(mnuExitbar);
                          mnuExitbar.add(mnuExit);
 
                          mFrame.setVisible(true);
 
                     //exit listener
                      mnuExit.addActionListener(this);  //exit dropdown
                }
     }
 
          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!",
                                                            "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!",
                                                       "Message Dialog", JOptionPane.PLAIN_MESSAGE);
                    Rtextfield.setText(null);
               }
          }
     }
 
     //variables declarations
     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;
 			 double paymentMonth = 0;
			 double balloonPayment = 0;
			 
			 
          //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);
					mtextfield.setText(null);
					btextfield.setText(null);
               options.setSelectedIndex(0);
          }
 
          try
          {
               trm = Double.parseDouble(Ttextfield.getText());
 
          }
 
          catch (NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Invalid or missing Loan Term!",
                                                  "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!",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Rtextfield.setText(null);
          }
 
          if (amt > 0)
          {
               amt = Double.parseDouble(Ltextfield.getText());
               moIn = (intrst / 1200);
               moTrm = trm * 12;
               paymt = (amt * moIn) / (1-Math.pow((1+moIn), -moTrm));
                  yearlyPrinciple = new float[(int)trm]; 
                  yearlyInterest = new float[(int)trm];
 
               $label.setText("" + currency.format(paymt));
 
               double newPrin = amt;
					paymentMonth = Double.parseDouble(mtextfield.getText());
					balloonPayment = Double.parseDouble(btextfield.getText());
					
 				 if (paymt == paymentMonth)
					{newPrin = newPrin - balloonPayment;
					}
		
               for (int i = 0; i < trm; i++)
               
										
					{
                       yearlyInterest[i] = 0.0f; 
                         yearlyPrinciple[i] = 0.0f;
                       for(int j = 1; j <=12; j++)
                         {
                    double newIn = moIn * newPrin;
                    double reduct = paymt - newIn;
                         yearlyInterest[i] += newIn;
                         yearlyPrinciple[i] += reduct; 
                    newPrin = newPrin - reduct;
						  
						 
						
                	         if (newPrin < 0)
                 				        newPrin = 0;
                    			else
                       			  newPrin = newPrin;
 
                    //populates the table
                    model.addRow(new Object[] { Integer.toString((i*12) + j), currency.format(paymt),
                    currency.format(newIn), currency.format(reduct), currency.format(newPrin) });
                    }
 
               }
          }
 
          //less then 0 error check
          if (amt < 0)
          {
               JOptionPane.showMessageDialog(null, "Positive Numbers Only.",
                                             "Message Dialog", JOptionPane.PLAIN_MESSAGE);
               Ltextfield.setText(null);
          }
 
     }
 
     //resets fields
     void reset()
     {
          Ltextfield.setText(null);
          Ttextfield.setText(null);
          Rtextfield.setText(null);
			 mtextfield.setText(null);
			 btextfield.setText(null);
          options.setSelectedIndex(0);
          $label.setText(null);
          model.setRowCount(0);
     }
 
     //exits the program
     void exit()
     {
 
          System.exit(0);
     }
 
     public static void main(String args[])
     {
          java.awt.EventQueue.invokeLater(new Runnable()
          {
               public void run()
               {
                    new LoanProjectUI7().setVisible(true);
               }
          });
     }
}
 
	
 
//layout and placement of GUI
class LoanProjectUILayout implements LayoutManager
{
     public LoanProjectUILayout() {}
 
     public void addLayoutComponent(String name, Component comp) {}
 
     public void removeLayoutComponent(Component comp) {}
 //sets up the dimensions of the panel
     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;
     }
 //sets up the locations of the components in the container
 
     public void layoutContainer(Container parent)
     {
          Insets insets = parent.getInsets();
 
          Component c;
          c = parent.getComponent(0); //L label (Mortgage Loan Amount)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,250,24);}
          c = parent.getComponent(1); // L textfield (Mortgage Loan Input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+8,175,24);}
          c = parent.getComponent(2); // O Label(Term and Interest Rate Options)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,250,24);}
          c = parent.getComponent(3); // JComboBox options (Term and Interest Options Box)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+40,150,24);}
          c = parent.getComponent(4); //T Label (Term)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,250,24);}
          c = parent.getComponent(5); // T textfield (term input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+72,96,24);}
          c = parent.getComponent(6); // R label (interest rate)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+96,250,24);}
          c = parent.getComponent(7); // R textfield (interest rate input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+96,112,24);}
          c = parent.getComponent(8); // P label (Monthly Payment Amount)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+120,250,24);}
          c = parent.getComponent(9); // $ label (label for monthly payment)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+120,112,24);}
          c = parent.getComponent(10); // JButton calculate 
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+375,96,24);}
          c = parent.getComponent(11); // JButton reset
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+375,112,24);}
          c = parent.getComponent(12); // JButton exit
               if (c.isVisible()) {c.setBounds(insets.left+450,insets.top+375,96,24);}
          c = parent.getComponent(13); // JTable table
         		if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,575,160);}
          c = parent.getComponent(14); // JButton graph
         		if (c.isVisible()) {c.setBounds(insets.left+150,insets.top+375,112,24);}
			 c = parent.getComponent(15); //M Label (month of balloon payment)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+143,250,24);}
          c = parent.getComponent(16); // M textfield (month input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+143,96,24);}
          c = parent.getComponent(17); // B label (balloon payment)
               if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+170,250,24);}
          c = parent.getComponent(18); // B textfield (Balloon Payment input)
               if (c.isVisible()) {c.setBounds(insets.left+300,insets.top+170,112,24);}
			
			
 
     }
}
 
//coding for creation of Graph output Panel and the two charecteristics being graphed
class GraphPanel extends JPanel
{
    final int
        HPAD = 60,
        VPAD = 40;
    int[] data;
    Font font;
     float[] principleData;
     float[] interestData;
 
 //the floats for principal and interest
    public GraphPanel(float[] p, float[] i)
    {
 
     principleData = p;
     interestData = i;
 
        font = new Font("Tahoma", Font.PLAIN, 16);
        setBackground(Color.gray);
    }
 
    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 for graph
        float xInc = (w - HPAD - VPAD) / (interestData.length - 1);  
        float yInc = (h - 2*VPAD) / 10f;
        int[] dataVals = getDataVals();       
        float yScale = dataVals[2] / 10f;
 
        //y 
        g2.draw(new Line2D.Double(HPAD, VPAD, HPAD, h - VPAD));
        
        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;
        }
        //  populates labels based upon user inputs or selections
        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);
        }
 
        //x
        g2.draw(new Line2D.Double(HPAD, h - VPAD, w - VPAD, h - VPAD));
        
        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;
        }
        // populates labels based upon user inputs or selections
        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);
        }
 
        // plots the data and sets the characteristics of the variables output
          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.black);
          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.orange);
          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 };
    }
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 23708837
Looks like you have some layout issues, unfortunately I'm in the middle of guiding a few of my students with there assignments so don't have time to look t the code in detail at the moment. Will try and find some time tomorrow to have a look at your.

get some rest and get that flu better :)


0
 

Author Comment

by:UniversalFrost
ID: 23717550
thanks for the help.

i figured it out with the help of my prof.

JOE
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month4 days, 5 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question