![]() |
| ||
| Homework Help Hello everyone, I'm at a loss. I've been trying to add a couple of images to my mortgage loan program for class. Everything I try either doesn't work or blanks out the screen. Any help would be appreciated: :-| /* * Author: Diana Salisbury * Week: 4 * Date: March 20, 2006 * Filename: MortPayCal.java * Purpose: This project will display a graphical user interface that calculates a mortgage payment amount and amortization schedule from user input amounts, terms, and interest rates. The three combinations of terms and interest rates for the loan are loaded from a file. */ import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; import java.text.DecimalFormat; import java.io.*; import javax.swing.JOptionPane; import java.lang.*; import java.util.Arrays; public class MortCalAmtDLSWK5Image extends JFrame implements ActionListener { //declare layout components JPanel contentPane = new JPanel(); JLabel instructionLabel = new JLabel(); JLabel instructionLabel2 = new JLabel(); JLabel instructionLabel3 = new JLabel(); JLabel termLabel = new JLabel(); JLabel rateLabel = new JLabel(); JLabel paymentLabel = new JLabel(); JLabel salaryLabel = new JLabel(); JLabel salaryFormatLabel = new JLabel(); JLabel qualifyLabel = new JLabel(); JLabel amountLabel = new JLabel(); JLabel amountFormatLabel = new JLabel(); JLabel calcLabel = new JLabel(); JLabel tableLabel = new JLabel(); //declare text fields JTextField termField = new JTextField(20); JTextField rateField = new JTextField(20); JTextField amountField = new JTextField(20); JTextField salaryField = new JTextField(20); JTextField paymentField= new JTextField(20); JTextField qualifyField = new JTextField(20); //declare variable for images and images int qualify = 0; Image homeImage = Toolkit.getDefaultToolkit().getImage("home.jpg"); Image noImage = Toolkit.getDefaultToolkit().getImage("no.jpg"); //Icon icon = new ImageIcon("home.jpg"); // JButton homeButton = new JButton(); //Icon icon2 = new ImageIcon("no.jpg"); //JButton noButton = new JButton(); //declare variables and array String[] FileArray = new String[6]; //declare button objects JButton clearButton = new JButton(); JButton calcButton = new JButton(); JButton quitButton = new JButton(); //declare text area for amortization JTextArea amortTextArea = new JTextArea(); JTextArea testTextArea = new JTextArea(); //declare scroll bar for amortization table JScrollPane scrollPane = new JScrollPane(amortTextArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); public MortCalAmtDLSWK5Image() { //Set up the frame features super("Mortgage Payment Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); try { //Create the file reading object BufferedReader reader = new BufferedReader(new FileReader("e:\\LoanChoices.txt")); for (int i = 0; i<6; i++) { // Read from the file FileArray[i] = reader.readLine( ); } //close file reader.close(); } catch (IOException e) { System.out.println("There was an input/output error."); } int intTerm1=Integer.parseInt(FileArray[0]); double dblInterest1=Double.parseDouble(FileArray[1]); int intTerm2=Integer.parseInt(FileArray[2]); double dblInterest2=Double.parseDouble(FileArray[3]); int intTerm3=Integer.parseInt(FileArray[4]); double dblInterest3=Double.parseDouble(FileArray[5]); // Create Menu MenuBar mnuBar=new MenuBar(); setMenuBar(mnuBar); Menu mnuFile = new Menu("File",true); mnuBar.add(mnuFile); MenuItem mnuExit = new MenuItem("Exit"); mnuFile.add(mnuExit); Menu mnuRateTerm = new Menu("Rate and Term",true); mnuBar.add(mnuRateTerm); MenuItem mnuChoice1 = new MenuItem(FileArray[0] + "% and " + FileArray[1] + " Years"); mnuRateTerm.add(mnuChoice1); MenuItem mnuChoice2 = new MenuItem(FileArray[2] + "% and " + FileArray[3] + " Years"); mnuRateTerm.add(mnuChoice2); MenuItem mnuChoice3 = new MenuItem(FileArray[4] + "% and " + FileArray[5] + " Years"); mnuRateTerm.add(mnuChoice3); // add the ActionListener to each menu mnuExit.addActionListener(this); mnuChoice1.addActionListener(this); mnuChoice2.addActionListener(this); mnuChoice3.addActionListener(this); // set command actions mnuExit.setActionCommand("Exit"); mnuChoice1.setActionCommand("Choice1"); mnuChoice2.setActionCommand("Choice2"); mnuChoice3.setActionCommand("Choice3"); instructionLabel.setText("Input the term, rate, and amount fields in the appropriate fields."); instructionLabel.setFont(new Font("Century",Font.ITALIC,14)); instructionLabel2.setText("OR"); instructionLabel2.setFont(new Font("Century",Font.BOLD + Font.ITALIC,18)); instructionLabel3.setText("Select a predefined term and rate from the Window option above & input amount."); instructionLabel3.setFont(new Font("Century",Font.ITALIC,14)); amountField.requestFocusInWindow(); //set focus on amount field termLabel.setText("Enter term length in years:"); rateLabel.setText("Enter interest rate:"); salaryLabel.setText("Enter your yearly salary:"); salaryFormatLabel.setText("Enter salary to nearest whole dollar."); salaryFormatLabel.setFont(new Font("Century",Font.ITALIC,12)); calcLabel.setText("Press Calculate to determine monthly payment or Clear to start all over."); calcLabel.setFont(new Font("Century",Font.ITALIC,14)); amountLabel.setText("Enter mortgage amount:"); amountFormatLabel.setText("Enter mortgage amountto the nearest whole dollar."); amountFormatLabel.setFont(new Font("Century",Font.ITALIC,12)); calcButton.setText("Calculate"); //define calculate button calcButton.addActionListener(this); //set action listener for calculate button paymentLabel.setText("Monthly payment:"); //define monthly payment label paymentField.setEditable(false); //set monthly payment field to not be editable qualifyField.setText(""); qualifyField.setEditable(false); //set qualify field to not be editable clearButton.setText("Clear"); //define clear button clearButton.addActionListener(this); //set action listener for clear button quitButton.setText("Quit"); //define quit button quitButton.addActionListener(this); //set action listener for quit button tableLabel.setText("Amoritization Table"); //add components to content getContentPane().add(contentPane); contentPane.setLayout(null); addComponent(contentPane, instructionLabel, 50,10,450,26); addComponent(contentPane, instructionLabel2, 250,30,450,26); addComponent(contentPane, instructionLabel3, 50,50,550,26); addComponent(contentPane, termLabel, 35,75,200,30); addComponent(contentPane, termField, 195,75,25,20); addComponent(contentPane, rateLabel, 35,100,200,30); addComponent(contentPane, rateField, 195,100,40,20); addComponent(contentPane, amountLabel, 35,125,220,25); addComponent(contentPane, amountField, 195,125,75,25); addComponent(contentPane, amountFormatLabel, 275,120,300,30); addComponent(contentPane, salaryLabel, 35,150,250,25); addComponent(contentPane, salaryField, 195,150,75,25); addComponent(contentPane, salaryFormatLabel, 275,150,300,25); addComponent(contentPane, calcLabel, 35,175,500,30); addComponent(contentPane, calcButton, 100,215,100,30); addComponent(contentPane, clearButton, 250,215,100,30); addComponent(contentPane, quitButton, 400,215,100,30); addComponent(contentPane, paymentLabel, 35,275,200,30); addComponent(contentPane, paymentField, 150,275,75,20); addComponent(contentPane, qualifyField, 300,260,125,40); addComponent(contentPane, tableLabel, 200,325,300,26); addComponent(contentPane, scrollPane, 50,325,500,300); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); pack(); } //method to add components private void addComponent(Container container, Component c, int x, int y, int width, int height) { //get the image from the file c.setBounds(x, y, width, height); container.add(c); } //action performed method public void actionPerformed(ActionEvent event) { String arg = event.getActionCommand(); if (arg == "Exit") { System.exit(0); } if (arg == "Choice1") { rateField.setText(FileArray[1]); termField.setText(FileArray[0]); } if (arg == "Choice2") { rateField.setText(FileArray[3]); termField.setText(FileArray[2]); } if (arg == "Choice3") { rateField.setText(FileArray[5]); termField.setText(FileArray[4]); } Object source = event.getSource(); if (source == calcButton) { Calculate(); } if (source == clearButton) { Clear(); } if (source == quitButton) { Exit(); } } void Calculate() { //calculation variables NumberFormat currency = NumberFormat.getCurrencyInstance(); double totalMonths = 0; //total months double Loan = 0.0; //amount of loan double MonthlyInterest = 0.0; //monthly interest rate double Payment = 0.0; //calculate payment double monthlyPayment = 0.0; //calculate monthly payment double Interest = 0.0; //variable for Interest Array input double Term = 0; //variable for Term Array input double NewMonthlyInterest = 0.0; //new interest amount double NewLoan = 0.0; //new loan amount double Reduction = 0.0; //principle reduction double salary = 0.0; //yearly salary double monthlySalary = 0.0; //monthly salary double perSal = 0.0; //28% of salary //validate input try { Interest = Double.parseDouble(rateField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null,"Invalid entry. Please enter valid rate (without percent symbol).","Error",JOptionPane.WARNING_MESSAGE); //clears fields after error message rateField.setText(""); rateField.requestFocusInWindow(); } try { Term = Double.parseDouble(termField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null,"Invalid Entry. Please enter valid term in years.", "Error", JOptionPane.WARNING_MESSAGE); //resets input fields after error message termField.setText(""); termField.requestFocusInWindow(); } try { Loan = Double.parseDouble(amountField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null,"Invalid Entry. Please enter valid loan amount.", "Error", JOptionPane.WARNING_MESSAGE); //resets input fields after error message amountField.setText(""); amountField.requestFocusInWindow(); } try { salary = Double.parseDouble(salaryField.getText()); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null,"Invalid Entry. Please enter valid yearly salary.", "Error", JOptionPane.WARNING_MESSAGE); //resets input fields after error message termField.setText(""); termField.requestFocusInWindow(); } //calculations if (Loan > 0) { MonthlyInterest = (Interest / 12)/100; totalMonths = Term * 12; monthlyPayment = Loan * MonthlyInterest *(Math.pow((1 + MonthlyInterest), totalMonths)/(Math.pow((1 + MonthlyInterest), totalMonths)-1)); paymentField.setText("" + currency.format(monthlyPayment)); monthlySalary = salary / 12; perSal = monthlySalary * .28; if(perSal >= monthlyPayment) { qualifyField.setText("You qualify!"); qualifyField.setFont(new Font("Century", Font.BOLD, 18)); qualifyField.setForeground(Color.red); qualify = 11; } else if(perSal < monthlyPayment) { qualifyField.setText("Sorry"); qualifyField.setFont(new Font("Century", Font.BOLD + Font.ITALIC, 30)); qualifyField.setForeground(Color.black); qualify = 22; } } //send information to amortization text area amortTextArea.append("Number\t"); amortTextArea.append(" Amount\t"); amortTextArea.append("Interest\t"); amortTextArea.append("Principle\t"); amortTextArea.append("Balance\n"); NewLoan = Loan; for (int i = 1; i <= totalMonths; i++) { NewMonthlyInterest = MonthlyInterest * NewLoan; Reduction = monthlyPayment - NewMonthlyInterest; NewLoan = NewLoan - Reduction; amortTextArea.append(" " + i +"\t"); amortTextArea.append(" " + currency.format(monthlyPayment) + "\t"); amortTextArea.append(" " + currency.format(NewMonthlyInterest)+ "\t"); amortTextArea.append(" " + currency.format(Reduction) + "\t"); amortTextArea.append(" " + currency.format(NewLoan) + "\n"); } //resets fields if loan amount is less than zero if((Loan <= 0 || Term <= 0 || Interest <= 0)) { paymentField.setText(""); amortTextArea.setText(""); setVisible(true); } } public void paint(Graphics g) { super.paint(g); if (qualify == 11) { homeImage = Toolkit.getDefaultToolkit().getImage("homeImage.gif"); g.drawImage(homeImage, 0, 0,null); } if (qualify == 22) { noImage = Toolkit.getDefaultToolkit().getImage("noImage.gif"); g.drawImage(noImage, 0, 0,null); } } void Clear() { amountField.setText(""); amountField.requestFocusInWindow(); termField.setText(""); rateField.setText(""); salaryField.setText(""); paymentField.setText(""); qualifyField.setText(""); amortTextArea.setText(""); } public static void main(String args[]) { MortCalAmtDLSWK5Image f = new MortCalAmtDLSWK5Image(); f.setTitle("Mortgage Calculator"); f.setBounds(200,50,600,700); f.setResizable(true); f.setVisible(true); } void Exit() { System.exit(0); } } |
| ||
| Re: Homework Help You should post that in the java forum and you should use code tags Or be forced into using code tags...cscgal are you listening? |
| All times are GMT -4. The time now is 6:06 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC