Sunday, April 25, 2010
Turning One Out
I finally exported my first GUI (Graphic User Interface) based Java Applet. This one was basically the same as a 'magic 8 ball', but with a few other parameters. This one was a request from a friend of mine, and I tried to do it in flash first. I found I could get it to play sounds easily enough when you clicked for an answer, but I couldn't get it to randomly generate text on the screen from a list I created. Most specifically, I couldn't get it to output text to a dynamic text box at all. This was frustrating.
Of course, with Net Beans and java, absolutely everything you make is tagged as an instance you can then manipulate in programming. It took about 20 minutes of effort, but I could get it to spit out text to a dynamic text box. The next thing I had to do was to randomize the string of text it displayed. This was the part of programming that comes easy to me. I had to go through C+ and C++ in college before getting to java, so just writing code is nothing.
I declared my constant(s) first (I was taught constants were to be in all CAPS), and then the remaining variable that would be fed to the textbox.
private int SIZE = 11; //Dictates the size of an array
private String RandAns[] = new String[SIZE]; //Declaration of an array
//of String to be the bank
//for my random answers
Random generator = new Random(); //Initialized randomizer.
//Next I wrote a function to populate the String array RandAns[], only
//example text is shown, can be changed to whatever suits your needs.
private void Populate()
{
RandAns[0] = "Example text 1";
RandAns[1] = "Example text 2";
RandAns[2] = "Example text 3";
RandAns[3] = "Example text 4";
RandAns[4] = "Example text 5";
RandAns[5] = "Example text 6";
RandAns[6] = "Example text 7";
RandAns[7] = "Example text 8";
RandAns[8] = "Example text 9";
RandAns[9] = "Example text 10";
RandAns[10] = "Example text 11";
}
//upon initialization of the java applet, I call the function Populate, so
//that my array is full and ready.
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
setSize( 550,470 );
Populate ();
}
});
//when I open the button handler, I added the following code to output
//a randomized string from array RandAns[] to the dynamic textbox
//on the GUI.
//Dynamic Textbox instance is named "AnsTxtBox"
private void AnsKeyActionPerformed(java.awt.event.ActionEvent evt) {
AnsTxtBox.setText(RandAns[generator.nextInt(SIZE)]);
}
Hopefully this code can come in handy for anyone reading it. Of course...my professors from college are probably weeping at how poorly the program was written. But it works!
Of course, with Net Beans and java, absolutely everything you make is tagged as an instance you can then manipulate in programming. It took about 20 minutes of effort, but I could get it to spit out text to a dynamic text box. The next thing I had to do was to randomize the string of text it displayed. This was the part of programming that comes easy to me. I had to go through C+ and C++ in college before getting to java, so just writing code is nothing.
I declared my constant(s) first (I was taught constants were to be in all CAPS), and then the remaining variable that would be fed to the textbox.
private int SIZE = 11; //Dictates the size of an array
private String RandAns[] = new String[SIZE]; //Declaration of an array
//of String to be the bank
//for my random answers
Random generator = new Random(); //Initialized randomizer.
//Next I wrote a function to populate the String array RandAns[], only
//example text is shown, can be changed to whatever suits your needs.
private void Populate()
{
RandAns[0] = "Example text 1";
RandAns[1] = "Example text 2";
RandAns[2] = "Example text 3";
RandAns[3] = "Example text 4";
RandAns[4] = "Example text 5";
RandAns[5] = "Example text 6";
RandAns[6] = "Example text 7";
RandAns[7] = "Example text 8";
RandAns[8] = "Example text 9";
RandAns[9] = "Example text 10";
RandAns[10] = "Example text 11";
}
//upon initialization of the java applet, I call the function Populate, so
//that my array is full and ready.
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
setSize( 550,470 );
Populate ();
}
});
//when I open the button handler, I added the following code to output
//a randomized string from array RandAns[] to the dynamic textbox
//on the GUI.
//Dynamic Textbox instance is named "AnsTxtBox"
private void AnsKeyActionPerformed(java.awt.event.ActionEvent evt) {
AnsTxtBox.setText(RandAns[generator.nextInt(SIZE)]);
}
Hopefully this code can come in handy for anyone reading it. Of course...my professors from college are probably weeping at how poorly the program was written. But it works!
Labels:
computer programming,
Free Java Compiler,
java,
Net Beans,
Net Beans IDE
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment