• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

application to applet

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to convert my numerology application to an applet. (the code for the application is at the end of the post). The applet has to include two textfields, for inputting the name and the birthdate, a text area to display the results, and three buttons -- one for doing the calculations, one to refresh the screen, and one to close down the applet.
This is what I have so far for the applet:
<code>
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class NumerologyApplet extends Applet {
private TextField dateInput = new TextField("");
private TextField nameInput = new TextField("");
private Button closeBtn = new Button("Close");
private Button compBtn = new Button("Compute");
private Button refBtn = new Button("Refresh");
}
</code>
My question is, can I use any of the already existing application? My teacher said all we had to do was attach a GUI front end to it, but I am somewhat confused on how to do that, especially how to go from getting the input as command line arguments to getting it from the textfield input by the user.

<code>
import java.text.NumberFormat;
public class Calculation {
private int reduce_num(int num) {
int new_num = 0;
String s = ("" + num);
if (num < 10)<br /> return num;<br /> else<br /> new_num = Integer.parseInt(s.substring(0,1));<br /> for (int i = 1; i < s.length(); i++) {<br /> new_num += Integer.parseInt(s.charAt(i) + "");<br /> }<br /> if (new_num > 9)<br /> new_num = reduce_num(new_num);<br /> return new_num;<br /> }<br /> <br /> private int char_convert (String s) {<br /> int sum = 0;<br /> for (int i = 0; i < s.length(); i++) {<br /> sum += ((s.charAt(i) - 'a') % 9) + 1;<br /> }<br /> if (sum > 9)<br /> sum = reduce_num(sum);<br /> return sum;<br /> }<br /> private void a_string (String birthname) {<br /> //System.out.print(name);<br /> String name = (birthname.toLowerCase());<br /> int soul_num = 0;<br /> int person_num = 0;<br /> int power_num = 0;<br /> String vowels = "";<br /> String consonants = ""; <br /> for (int i = 0; i < name.length(); i++){<br /> if (name.charAt(i) == ' ')<br /> continue;<br /> else <br /> if (name.charAt(i) == 'a' || name.charAt(i) == 'e' || name.charAt(i) == 'i'<br /> || name.charAt(i) == 'o' || name.charAt(i) == 'u' || name.charAt(i) == 'y')<br /> vowels += name.charAt(i);//<br /> else<br /> consonants += name.charAt(i);//<br /> <br /> }<br /> soul_num = char_convert(vowels);<br /> person_num = char_convert(consonants);<br /> <br /> power_num = soul_num + person_num;<br /> power_num = reduce_num(power_num);<br /> System.out.println("The soul number is " + soul_num);<br /> System.out.println("The personality number is " + person_num);<br /> System.out.println("The power number is " + power_num);<br /> }<br /> private void birth_date (String date) {<br /> int att_num;<br /> int path_num;<br /> int birth_num;<br /> int mm_num;<br /> int dd_num;<br /> int yy_num;<br /> <br /> String month = (date.substring(0,2));<br /> String day = (date.substring(3,5));<br /> String year = (date.substring(6,10));<br /> <br /> mm_num = Integer.parseInt(month);<br /> dd_num = Integer.parseInt(day);<br /> yy_num = Integer.parseInt(year);<br /> if (mm_num > 9) <br /> mm_num = reduce_num(mm_num);<br /> if (dd_num > 9)
dd_num = reduce_num(dd_num);
if (yy_num > 9)
yy_num = reduce_num(yy_num);
path_num = mm_num + dd_num + yy_num;
if (path_num > 9)
path_num = reduce_num(path_num);
birth_num = dd_num;
if (birth_num > 9)
birth_num = reduce_num(birth_num);

att_num = mm_num + dd_num;
if (att_num > 9)
att_num = reduce_num(att_num);

System.out.println("The life path number is " + path_num);
System.out.println("The birthday number is " + birth_num);
System.out.println("The attitude number is " + att_num);
}
public Calculation (String name, String date) {
String a_string = new String(name);
String another_string = new String(date);
a_string(name);
birth_date(date);
}
}
public class Numerology {
public static void main(String [] args) {
if (args.length == 2) {
Calculation calc = new Calculation(args[0],args[1]);
}
}
}
</code>
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In an application, the JVM uses the main() method as the entry point your code. For an applet, the VM invoked by the browser is already running, so it's just a question of "slotting" the applet into it.
The Applet's methods serve as entry points for the browser's VM. The browser expects to be able to call init(), start(), stop(), paint() and a few others on your applet. When paint() is called on your applet, for example, it's nothing more than the VM asking for the proper directions to render the viewing space of your applet.
Roughly speaking, the code from your main() method and constructor could probably go into your applet init() method with almost no modifications.
Think of your Applet as a Panel (because it is one, after all) and the browser as an AWT Frame. Anything a Panel object normally has to handle to draw itself properly should go in your paint() method.
You usually don't need to worry about stop() and start() unless there is an animation, sound loop, or other ongoing calculation in your applet that you feel should stop when the browser is minimized, and start when it's opened back up.
The big don't: when you want the Applet area to be re-drawn, don't call paint(); call repaint() instead. This allows the system to accept your request on its screen-painting queue on its own terms.
The rest of your code can virtually remain untouched, because the browser will never call them directly. You can factor out whatever code you like into the methods you name; just make sure they're accessed as needed when the applet is being managed by the browser.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
reply
    Bookmark Topic Watch Topic
  • New Topic