[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

ADDING ANY COLOR TO JAVA APPLET BACKGROUND, FIELDS AND LINES ???????? THANK YOU IN ADVANCE!!! :)

PLEASE SOMEONE HELP ME WITH THIS PROGRAMMING, IM NOT MUCH OF A PROGRAMMER, I'M HAVING SUCH A HARD TIME FIGURING THIS OUT; I EVEN BOUGHT A BOOK JAVA SAMS 4TH EDITION.  

HOW CAN I ADD COLORS TO THE APPLET BACKGROUND, FIELDS AND LINES????? I WOULD ALSO LIKE TO DISPLAY THIS JAVA APPLET IN HTML AND JAVA, IS THIS POSSIBLE?????  PLEASE PUT COMMENTS OR EXPLANATIONS FOR THE NEW JAVA LINES FOR THE BACKGROUND, FIELDS AND LINE COLORS SO HOPEFULLY I CAN UNDERSTAND IT BETTER.. :)

THANK YOU IN ADVANCE!


// newMortgage.java


import java.awt.*;
import java.applet.*;

public class Mortgage extends Applet {
  TextField textPrincipal,
    textYearlyInterest,
    textYears;
  Label textPayment;
  Label errorLine;

  float payment;
 
  public void init() {
    // Set Background and layout
     Color customColor = null;
     try {
          String r = getParameter("r");
          String g = getParameter("g");
          String b = getParameter("b");
          if ( ( r != null ) && ( g != null ) && ( b != null ) ) {
               customColor = new Color(
                         Integer.valueOf(r).intValue(),
                         Integer.valueOf(g).intValue(),
                         Integer.valueOf(b).intValue() );
          }
     }
     catch ( NumberFormatException e ) {
     }

     // Color customColor = new Color(255,204,0);
     if ( customColor != null )
         setBackground(customColor);
     else
         setBackground(Color.white);
    setLayout(new BorderLayout());
    add("North",new Label("Mortgage Calculator Copyright 1997 Mark Solomon"));

    // Three entry fields
    Panel p1 = new Panel();
    p1.setLayout(new GridLayout(0,2));

    p1.add(new Label("Loan amount (No commas):"));
    textPrincipal = new TextField(10);
    p1.add(textPrincipal);
   
    p1.add(new Label("Interest Rate in %:"));
    textYearlyInterest = new TextField(8);
    p1.add(textYearlyInterest);
   
    p1.add(new Label("Number of Years:"));
    textYears = new TextField(8);
    p1.add(textYears);

    // payment result
    p1.add(new Label("Monthly Payment: "));
    textPayment = new Label();
    p1.add(textPayment);

    Panel centerPanel = new Panel();
    centerPanel.add(p1);
    add("Center",centerPanel);

    errorLine = new Label();
    Panel p2 = new Panel();
    p2.setLayout(new GridLayout(0,1));
    p2.add(errorLine);
    p2.add(new Button("Calculate!"));
    add("South",p2);
  }

  public double getDouble (TextField t1) {
    try {
      Double tmpOne = Double.valueOf(t1.getText());
      double one = tmpOne.doubleValue();
      return one;
    }
    catch (NumberFormatException ex1) {
      errorLine.setText("Error: Please only enter numbers");
      return 0;
    }
  }

  public int getInt (TextField t1) {
    try {
      Integer tmpOne = Integer.valueOf(t1.getText());
      int one = tmpOne.intValue();
      return one;
    }
    catch (NumberFormatException ex1) {
      errorLine.setText("Error: Please only enter numbers");
      return 0;
    }
  }

  public String formatMoney (double number) {
    number = Math.floor(number * 100 + .5)/100; // round to hundreths
    double even = (number * 100) % 10;
    if ((even < 1) || (even > 9)) {
      return "$"+String.valueOf(number)+"0";
    }
    else {
      return "$"+String.valueOf(number);
    }
  }
 
  public boolean action(Event e,Object o) {

    double principal = getDouble(textPrincipal);
    if ( principal > 0 ) {

      // a valid principal was entered
      double yearlyInterest = getDouble(textYearlyInterest);
      if ( yearlyInterest > 0 ) {

     // A valid interest was entered
     int years = getInt(textYears);
     if ( years > 0 ) {

       // A valid year was entered
       // Calculate payment
             double monthlyInterest = (yearlyInterest / 100) / 12;
       double payment  = principal * monthlyInterest /
         (1 - (Math.pow(1/(1+monthlyInterest),years * 12)));
       textPayment.setText(formatMoney(payment));
     }
     errorLine.setText("");
      }
    }
    return true;
  }
}

0
ZACKYJONESKI
Asked:
ZACKYJONESKI
  • 8
  • 5
1 Solution
 
objectsCommented:
This tutorial should help you with running your applet

http://java.sun.com/docs/books/tutorial/applet/index.html
0
 
objectsCommented:
the applet tag in your html would look something like:

<APPLET CODE=Mortgage.class WIDTH=300 HEIGHT=300>
    <PARAM NAME=r VALUE=255>
    <PARAM NAME=g VALUE=0>
    <PARAM NAME=b VALUE=0>
</APPLET>

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
// newMortgage.java

the file name should be the same as the class name ie. Mortgage.java
0
 
CEHJCommented:
You can simplify your coding by only using one parameter, since the rgb values are read in order:

Color customColor = Color.decode(getParameter("backgroundColor"));

....


<APPLET CODE="Mortgage.class" WIDTH="300" HEIGHT="300">
      <PARAM NAME="backgroundColor" VALUE="0xA0B0C0">
</APPLET>
0
 
ZACKYJONESKIAuthor Commented:
WHERE DO I INSERT THESE LINES IN MY MORTGAGE JAVA PROGRAM???????






<APPLET CODE=Mortgage.class WIDTH=300 HEIGHT=300>
    <PARAM NAME=r VALUE=255>
    <PARAM NAME=g VALUE=0>
    <PARAM NAME=b VALUE=0>
</APPLET>





<APPLET CODE="Mortgage.class" WIDTH="300" HEIGHT="300">
     <PARAM NAME="backgroundColor" VALUE="0xA0B0C0">
</APPLET>
0
 
objectsCommented:
You add them to the html file you want to display your applet in

<html>

<head>
<title>My Mortgage Applet</title>
</head>

<body>

<APPLET CODE=Mortgage.class WIDTH=300 HEIGHT=300>
    <PARAM NAME=r VALUE=255>
    <PARAM NAME=g VALUE=0>
    <PARAM NAME=b VALUE=0>
</APPLET>

</body>
</html>
0
 
ZACKYJONESKIAuthor Commented:
IM SOOOOO SORRY OBJECTS IM SO STUPID WHEN IT COMES TO THIS PROGRAMMING STUFF!  THANK YOU FOR YOUR TIME.

CAN YOU PLEASE JUST SHOW ME AND EDIT THIS MORTGAGE PROGRAM.  I NEED THE BACKGROUND, LINES AND FIELD COLORS CHANGED, DOESNT MATTER WHAT COLORS, THEN ILL COMPILE AND RUN IT, AND HOPEFULLY I CAN UNDERSTAND IT BETTER THEN. WELL IT WILL BE GREAT IF I CAN VIEW THIS MORTGAGE IN HTML AS WELL, IF NOT ITS OKAY, I JUST WANT TO SEE HOW YOU CHANGE THE BACKGROUND, LINES AND FIELD COLORS.  

SORRY :)  ILL TRY TO LEAVE YOU ALONE THEN, AND OFCOURSE WILL GIVE YOU YOUR WELL EARNED POINTS, TY IN ADVANCE AGAIN...  I WISH I HAD YOUR PROGRAMMING INTELLIGENCE!


// newMortgage.java


import java.awt.*;
import java.applet.*;

public class Mortgage extends Applet {
  TextField textPrincipal,
    textYearlyInterest,
    textYears;
  Label textPayment;
  Label errorLine;

  float payment;
 
  public void init() {
    // Set Background and layout
     Color customColor = null;
     try {
          String r = getParameter("r");
          String g = getParameter("g");
          String b = getParameter("b");
          if ( ( r != null ) && ( g != null ) && ( b != null ) ) {
               customColor = new Color(
                         Integer.valueOf(r).intValue(),
                         Integer.valueOf(g).intValue(),
                         Integer.valueOf(b).intValue() );
          }
     }
     catch ( NumberFormatException e ) {
     }

     // Color customColor = new Color(255,204,0);
     if ( customColor != null )
         setBackground(customColor);
     else
         setBackground(Color.white);
    setLayout(new BorderLayout());
    add("North",new Label("Mortgage Calculator Copyright 1997 Mark Solomon"));

    // Three entry fields
    Panel p1 = new Panel();
    p1.setLayout(new GridLayout(0,2));

    p1.add(new Label("Loan amount (No commas):"));
    textPrincipal = new TextField(10);
    p1.add(textPrincipal);
   
    p1.add(new Label("Interest Rate in %:"));
    textYearlyInterest = new TextField(8);
    p1.add(textYearlyInterest);
   
    p1.add(new Label("Number of Years:"));
    textYears = new TextField(8);
    p1.add(textYears);

    // payment result
    p1.add(new Label("Monthly Payment: "));
    textPayment = new Label();
    p1.add(textPayment);

    Panel centerPanel = new Panel();
    centerPanel.add(p1);
    add("Center",centerPanel);

    errorLine = new Label();
    Panel p2 = new Panel();
    p2.setLayout(new GridLayout(0,1));
    p2.add(errorLine);
    p2.add(new Button("Calculate!"));
    add("South",p2);
  }

  public double getDouble (TextField t1) {
    try {
      Double tmpOne = Double.valueOf(t1.getText());
      double one = tmpOne.doubleValue();
      return one;
    }
    catch (NumberFormatException ex1) {
      errorLine.setText("Error: Please only enter numbers");
      return 0;
    }
  }

  public int getInt (TextField t1) {
    try {
      Integer tmpOne = Integer.valueOf(t1.getText());
      int one = tmpOne.intValue();
      return one;
    }
    catch (NumberFormatException ex1) {
      errorLine.setText("Error: Please only enter numbers");
      return 0;
    }
  }

  public String formatMoney (double number) {
    number = Math.floor(number * 100 + .5)/100; // round to hundreths
    double even = (number * 100) % 10;
    if ((even < 1) || (even > 9)) {
      return "$"+String.valueOf(number)+"0";
    }
    else {
      return "$"+String.valueOf(number);
    }
  }
 
  public boolean action(Event e,Object o) {

    double principal = getDouble(textPrincipal);
    if ( principal > 0 ) {

      // a valid principal was entered
      double yearlyInterest = getDouble(textYearlyInterest);
      if ( yearlyInterest > 0 ) {

     // A valid interest was entered
     int years = getInt(textYears);
     if ( years > 0 ) {

       // A valid year was entered
       // Calculate payment
             double monthlyInterest = (yearlyInterest / 100) / 12;
       double payment  = principal * monthlyInterest /
         (1 - (Math.pow(1/(1+monthlyInterest),years * 12)));
       textPayment.setText(formatMoney(payment));
     }
     errorLine.setText("");
      }
    }
    return true;
  }
}


0
 
objectsCommented:
>  IT WILL BE GREAT IF I CAN VIEW THIS MORTGAGE IN HTML AS WELL

Save the html I posted in my comment above to display your applet in html
you can change the colour by changing the r,g,b values
0
 
ZACKYJONESKIAuthor Commented:
IM USING A TEXTPAD TO COMPILE IT, BUT I WOULD A JDK RIGHT???  WHERE CAN I GET JDK?
0
 
ZACKYJONESKIAuthor Commented:
THANK YOU!
0
 
ZACKYJONESKIAuthor Commented:
YOU'RE SOOOOO COOOOOL!  THANK YOU!  THANK YOU!!!! :)
0
 
objectsCommented:
no worries :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now