Need help with code pleazzz
I'm still trying to getting the hang of applets, i know i might have missed some basics but could someone help me with this. I'm doing a mortgage payment calculator. User inputs principal, interest, term and textarea will display result.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.text.*;
publicclass MortgageAppletGUIextends Appletimplements ActionListener{
Button calculateButton;
TextField principalField;
TextField interestField;
TextField termField;
TextArea displayArea;
Label principal;
Label Term;
Label interest;
Button clearButton;
Image house;
Font title =new Font("TimesRoman", Font.BOLD, 16);
Font text =new Font("Courier", Font.BOLD, 12);
double monthlyInt;
double monthlyPayment;
int months;
double interestPayment;
double principalPayment;
double remainingBalance;
publicvoid init(){
MortgageAppletLayout customLayout =new MortgageAppletLayout();
setFont(new Font("Helvetica", Font.PLAIN, 12));
setLayout(customLayout);
Button calculateButton =new Button("Calculate");
add(calculateButton);
TextField principalField =new TextField(20);
add(principalField);
TextField interestField =new TextField(20);
add(interestField);
TextField termField =new TextField(20);
add(termField);
TextArea displayArea =new TextArea();
add(displayArea);
Label principal =new Label("Principal");
add(principal);
Label term =new Label("Term");
add(term);
Label interest =new Label("Interest");
add(interest);
Button clearButton =new Button("Clear");
add(clearButton);
setSize(getPreferredSize());
house = getImage(getCodeBase(),"house.jpg");
}
publicvoid actionPerformed(ActionEvent ev){
String userInputprincipal = principalField.getText();
String userInputinterest = interestField.getText();
String userInputterm = termField.getText();
//Display principal, interest, and loan balance of entire term
monthlyInt = userInputinterest / 12;
monthlyPayment = (userInputprincipal * monthlyInt)
/ (1 - Math.pow(1/ (1 + monthlyInt), userInputterm * 12));
for (int i = 1; i <= months; ++i )
{
interestPayment = userInputprincipal * monthlyInt;
principalPayment = monthlyPayment - interestPayment;
remainingBalance = userInputprincipal - principalPayment;
displayArea.setText("#" + i
+"\tP=" + (principalPayment)
+"\tI=" + (interestPayment)
+ (interestPayment <1 ?"\t\t\tB=" :"\t\t\tB=")//use tabs based on format of interest
+ (remainingBalance));
Thread.currentThread().sleep(200);//slows down the page
principal -= principalPayment;//calculate until payments are done
}
}
publicvoid paint(Graphics g){
//Display program info
g.drawImage(house, 10, 5,this);
g.setColor(Color.green);
g.drawLine(20, 4, 230, 4);
g.setColor(Color.red);
g.setFont(title);
g.drawString("\tMortgage Loan Calculator", 10, 17);
g.setColor(Color.black);
g.setFont(text);
g.drawString("\tCreated by Me", 10, 30);
}
publicstaticvoid main(String args[]){
MortgageApplet applet =new MortgageApplet();
Frame window =new Frame("MortgageApplet");
window.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
applet.init();
window.add("Center", applet);
window.pack();
window.setVisible(true);
}
}
class MortgageAppletLayoutimplements LayoutManager{
public MortgageAppletLayout(){
}
publicvoid addLayoutComponent(String name, Component comp){
}
publicvoid removeLayoutComponent(Component comp){
}
public Dimension preferredLayoutSize(Container parent){
Dimension dim =new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 469 + insets.left + insets.right;
dim.height = 311 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent){
Dimension dim =new Dimension(0, 0);
return dim;
}
publicvoid layoutContainer(Container parent){
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+280,72,24);}
c = parent.getComponent(1);
if (c.isVisible()){c.setBounds(insets.left+80,insets.top+104,72,24);}
c = parent.getComponent(2);
if (c.isVisible()){c.setBounds(insets.left+80,insets.top+128,72,24);}
c = parent.getComponent(3);
if (c.isVisible()){c.setBounds(insets.left+80,insets.top+152,72,24);}
c = parent.getComponent(4);
if (c.isVisible()){c.setBounds(insets.left+176,insets.top+88,280,216);}
c = parent.getComponent(5);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+104,72,24);}
c = parent.getComponent(6);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+152,72,24);}
c = parent.getComponent(7);
if (c.isVisible()){c.setBounds(insets.left+8,insets.top+128,72,24);}
c = parent.getComponent(8);
if (c.isVisible()){c.setBounds(insets.left+88,insets.top+280,72,24);}
}
}
[9802 byte] By [
JavaBean5a] at [2007-11-27 2:41:11]

Hi,
Please read this code carefully and especially the comments I added :
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.LayoutManager;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
public class MortgageAppletGUI extends Applet{
Button calculateButton;
TextField principalField;
TextField interestField;
TextField termField;
TextArea displayArea;
Label principal;
Label term;
Label interest;
Button clearButton;
Image house;
Font title = new Font("TimesRoman", Font.BOLD, 16);
Font text = new Font("Courier", Font.BOLD, 12);
double monthlyInt;
double monthlyPayment;
int months=12;
double interestPayment;
double principalPayment;
double remainingBalance;
public void init() {
MortgageAppletLayout customLayout = new MortgageAppletLayout();
setFont(new Font("Helvetica", Font.PLAIN, 12));
setLayout(customLayout);
calculateButton = new Button("Calculate");
calculateButton.addActionListener(new CalculateActionListener());/////////////////////change here : code added
add(calculateButton);
principalField = new TextField(20);//change here
add(principalField);
interestField = new TextField(20);
add(interestField);
termField = new TextField(20);
add(termField);
displayArea = new TextArea();
add(displayArea);
principal = new Label("Principal");
add(principal);
term = new Label("Term");
add(term);
interest = new Label("Interest");
add(interest);
clearButton = new Button("Clear");
clearButton.addActionListener(new CancelActionListener());////////change here : code added
add(clearButton);
setSize(getPreferredSize());
house = getImage(getCodeBase(), "house.jpg");
}
class CalculateActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String userInputprincipal = principalField.getText();
String userInputinterest = interestField.getText();
String userInputterm = termField.getText();
double userInputprincipalD=0;
double userInputinterestD=0;
double userInputtermD=0;
try {
userInputprincipalD = Double.parseDouble(userInputprincipal);///////////
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error principalField", "title", JOptionPane.ERROR_MESSAGE);
principalField.requestFocus();
return;
}
try {
userInputinterestD = Double.parseDouble(userInputinterest);/////////
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error interestField", "title", JOptionPane.ERROR_MESSAGE);
interestField.requestFocus();
return;
}
try {
userInputtermD = Double.parseDouble(userInputterm);///////////
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error termField", "title", JOptionPane.ERROR_MESSAGE);
termField.requestFocus();
return;
}
// Display principal, interest, and loan balance of entire term
monthlyInt = userInputinterestD / 12;
monthlyPayment = (userInputprincipalD * monthlyInt) / (1 - Math.pow(1 / (1 + monthlyInt), userInputtermD * 12));
for (int i = 1; i <= months; ++i) {
interestPayment = userInputprincipalD * monthlyInt;
principalPayment = monthlyPayment - interestPayment;
remainingBalance = userInputprincipalD - principalPayment;
displayArea.setText("#" + i + "\tP=" + (principalPayment) + "\tI=" + (interestPayment)
+ (interestPayment < 1 ? "\t\t\tB=" : "\t\t\tB=") // use tabs based on format of interest
+ (remainingBalance));
try {
Thread.currentThread().sleep(200);
} catch (InterruptedException ie) {
// TODO Auto-generated catch block
ie.printStackTrace();
} // slows down the page
//principal -= principalPayment; // calculate until payments are done ############## HERE A PROBLEM :
}
}
}
class CancelActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
principalField.setText("");
interestField.setText("");
termField.setText("");
displayArea.setText("");
}
}
public void paint(Graphics g) {
// Display program info
g.drawImage(house, 10, 5, this);
g.setColor(Color.green);
g.drawLine(20, 4, 230, 4);
g.setColor(Color.red);
g.setFont(title);
g.drawString("\tMortgage Loan Calculator", 10, 17);
g.setColor(Color.black);
g.setFont(text);
g.drawString("\tCreated by Me", 10, 30);
}
public static void main(String args[]) {
MortgageAppletGUI applet = new MortgageAppletGUI();
Frame window = new Frame("MortgageApplet");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
applet.init();
window.add("Center", applet);
window.pack();
window.setVisible(true);
}
}
class MortgageAppletLayout implements LayoutManager {
public MortgageAppletLayout() {
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 469 + insets.left + insets.right;
dim.height = 311 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {
c.setBounds(insets.left + 8, insets.top + 280, 72, 24);
}
c = parent.getComponent(1);
if (c.isVisible()) {
c.setBounds(insets.left + 80, insets.top + 104, 72, 24);
}
c = parent.getComponent(2);
if (c.isVisible()) {
c.setBounds(insets.left + 80, insets.top + 128, 72, 24);
}
c = parent.getComponent(3);
if (c.isVisible()) {
c.setBounds(insets.left + 80, insets.top + 152, 72, 24);
}
c = parent.getComponent(4);
if (c.isVisible()) {
c.setBounds(insets.left + 176, insets.top + 88, 280, 216);
}
c = parent.getComponent(5);
if (c.isVisible()) {
c.setBounds(insets.left + 8, insets.top + 104, 72, 24);
}
c = parent.getComponent(6);
if (c.isVisible()) {
c.setBounds(insets.left + 8, insets.top + 152, 72, 24);
}
c = parent.getComponent(7);
if (c.isVisible()) {
c.setBounds(insets.left + 8, insets.top + 128, 72, 24);
}
c = parent.getComponent(8);
if (c.isVisible()) {
c.setBounds(insets.left + 88, insets.top + 280, 72, 24);
}
}
}
Hope That Helps