PDA

View Full Version : Need help with coding Applet (Advanced coders suggested)*warning, very long*


MNov
07-23-2007, 01:04 AM
Okay, I have had it with this stupid thing I'm trying to code. What I'm trying to do is make an Applet that is like a study game. If you get it right, then the question is removed, else it stays and you randomly get another question. I coded it before but it had to access txt files. Now I want to stick the questions into the code itself but ArrayList and Arrays don't seem to want to run with the main method which I can't use because it's an Applet. here's the code, hopefully someone smart with a lot of time and find the error and fix it (or provide an alternative).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;

public class Quiz extends JApplet
{
private int WIDTH = 400;
private int HEIGHT = 200;

private JFrame frame;
private JPanel panel;
private JLabel inputLabel, outputLabel, resultLabel;
private JButton nextQ, Ans, WQues;
private JTextField question;
ArrayList questionA = new ArrayList();
ArrayList answerA = new ArrayList();

questionA.add(Abhor);
questionA.add(Bigot);
questionA.add(Counterfeit);
questionA.add(Enfranchise);
questionA.add(Hamper);
questionA.add(Kindle);
questionA.add(Noxious);
questionA.add(Placid);
questionA.add(Remuneration);
questionA.add(Talisman);
questionA.add(Abrasive);
questionA.add(Bilk);
questionA.add(Covert);
questionA.add(Engender);
questionA.add(Hangar);
questionA.add(Knotty);
questionA.add(Nuance);
questionA.add(Plagiarism);
questionA.add(Renown);
questionA.add(Tangent);
questionA.add(Abrasiveness);
questionA.add(Billowing);
questionA.add(Cower);
questionA.add(Enhance);
questionA.add(Harangue);
questionA.add(Labyrinth);
questionA.add(Nullify);
questionA.add(Plaintiff);
questionA.add(Replete);
questionA.add(Tangible);
questionA.add(Abrogate);
questionA.add(Blasphemy);
questionA.add(Credible);
questionA.add(Enigma);
questionA.add(Harbingers);
questionA.add(Labyrinthine);
questionA.add(Nuzzle);
questionA.add(Plaudit);
questionA.add(Reprehensible);
questionA.add(Tardy);
questionA.add(Absolution);
questionA.add(Blatant);
questionA.add(Creditable);
questionA.add(Ensconce);
questionA.add(Hasten);
questionA.add(Laceration);
questionA.add(Obdurate);
questionA.add(Plausible);
questionA.add(Reprieve);
questionA.add(Tawdry);
questionA.add(Abstain);
questionA.add(Blighted);
questionA.add(Credulous);
questionA.add(Enshroud);
questionA.add(Haughtiness);
questionA.add(Lachrymose);
questionA.add(Obfuscate);
questionA.add(Plethora);
questionA.add(Repudiate);
questionA.add(Tedium);
questionA.add(Abstemious);
questionA.add(Blithe);
questionA.add(Crepuscular);
questionA.add(Enunciation);
questionA.add(Headstrong);
questionA.add(Lackluster);
questionA.add(Objective);
questionA.add(Pliable);
questionA.add(Rescind);
questionA.add(Temper);
questionA.add(Abstruse);
questionA.add(Blunderbuss);
questionA.add(Cringe);
questionA.add(Envenom);
questionA.add(Hedonism);
questionA.add(Laconic);
questionA.add(Oblique);
questionA.add(Plumage);
questionA.add(Resignation);
questionA.add(Tenacious);
questionA.add(Accolade);
questionA.add(Bolster);
questionA.add(Cryptic);
questionA.add(Ephemeral);
questionA.add(Lamentation);
questionA.add(Obliterate);
questionA.add(Plummet);
questionA.add(Resolution);
questionA.add(Tentative);
questionA.add(Acquiesce);
questionA.add(Bombast);
questionA.add(Curtail);
questionA.add(Epicure);
questionA.add(Heed);
questionA.add(Lampoon);
questionA.add(Oblivious);
questionA.add(Podium);
questionA.add(Resonant);
questionA.add(Tenuous);
answerA.add(hate);
answerA.add(narrow);
answerA.add(fake, false);
answerA.add(giving voting rights);
answerA.add(hinder, obstruct);
answerA.add(to start a fire);
answerA.add(harmful, poisonous, lethal);
answerA.add(calm, peaceful);
answerA.add(payment for work done);
answerA.add(lucky charm);
answerA.add(rough, coarse, harsh);
answerA.add(cheat, defraud);
answerA.add(hidden, undercover);
answerA.add(cause);
answerA.add(storage area (like garage) for a plane);
answerA.add(complex, difficult to solve);
answerA.add(something subtle, a fine shade of meaning);
answerA.add(taking credit for someone else?s writing or ideas);
answerA.add(fame);
answerA.add(going off the main subject);
answerA.add(harshness, coarseness);
answerA.add(swelling, fluttering, waving);
answerA.add(recoil in fear or servility, shrink away from);
answerA.add(improve, make better or clearer);
answerA.add(noisy attacking speech);
answerA.add(a maze);
answerA.add(to counter, make unimportant);
answerA.add(applicant, petitioner);
answerA.add(full);
answerA.add(can be touched);
answerA.add(cancel, deny, repeal);
answerA.add(speech which offends religious sentiments);
answerA.add(believable);
answerA.add(puzzle, mystery);
answerA.add(indicators, bringers of warnings);
answerA.add(complicated, highly convoluted);
answerA.add(cuddle, snuggle);
answerA.add(statement giving strong praise);
answerA.add(shameful, very bad);
answerA.add(slow, late, overdue, delayed);
answerA.add(forgiveness, pardon, release);
answerA.add(obvious);
answerA.add(praiseworthy);
answerA.add(establish firmly in a position);
answerA.add(hurry, accelerate, rush);
answerA.add(a cut);
answerA.add(stubborn);
answerA.add(can be believed, reasonable);
answerA.add(a respite: postponement of a sentence);
answerA.add(a little value, gaudy);
answerA.add(desist, go without, withdraw);
answerA.add(damaged, destroyed, ruined);
answerA.add(gullible, ready to believe anything);
answerA.add(cover);
answerA.add(arrogance, pride);
answerA.add(tearful, sad);
answerA.add(deliberately make something difficult to understand);
answerA.add(an excess);
answerA.add(shun, eschew);
answerA.add(boredom);
answerA.add(self denying, refraining from indulging );
answerA.add(free);
answerA.add(active at dawn and dusk);
answerA.add(clear pronunciation, accent, articulation);
answerA.add(stubborn, willful);
answerA.add(dull, monotonous, bland);
answerA.add(unbiased, not subjective);
answerA.add(flexible, not stubborn);
answerA.add(retract, repeal);
answerA.add(to moderate, soften );
answerA.add(difficult to understand, obscure );
answerA.add(ancient weapon (type of gun); a clumsy person);
answerA.add(recoil, flinch, shy away);
answerA.add(to cause bitterness and bad feeling);
answerA.add(self indulgence, pleasure);
answerA.add(using few words, brief, short to the point);
answerA.add(indirect, slanting);
answerA.add(feathers of a bird);
answerA.add(acceptance of fate);
answerA.add(stubborn, resolute, holding firm to a purpose);
answerA.add(tribute, honor, praise);
answerA.add(support, prop up);
answerA.add(puzzling, enigmatic);
answerA.add(short);
answerA.add(expression of regret or sorrow);
answerA.add(destroy, demolish, eradicate);
answerA.add(fall suddenly and steeply);
answerA.add(determination);
answerA.add(not certain);
answerA.add(to agree to, give in to);
answerA.add(arrogant, pompous language);
answerA.add(cut short);
answerA.add(someone who appreciates good food and drink);
answerA.add(listens to);
answerA.add(ridicule, spoof);
answerA.add(totally unaware);
answerA.add(raised platform);
answerA.add(echoing);
answerA.add(flimsy, not solid);

Random gen = new Random();
int qnum=gen.nextInt(questionA.size());

public void init()
{

frame = new JFrame ("Quizzer");
Container contentPane = frame.getContentPane();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

inputLabel = new JLabel ("");
inputLabel.setMaximumSize(new Dimension(500, 130));
inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
Font f =new Font("Serif", Font.ITALIC, 20);
inputLabel.setFont(f);
inputLabel.setText("Question: "+questionA.get(qnum).toString());
resultLabel = new JLabel ("Answer: ");

nextQ = new JButton ("I got it Right");

nextQ.addActionListener (new TempListener2());
Ans = new JButton ("Answer");
Ans.addActionListener (new TempListener());
resultLabel.setMaximumSize(new Dimension(500, 200));
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
resultLabel.setFont(f);
WQues = new JButton ("I got it Wrong");
WQues.addActionListener (new TempListener3());

panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.yellow);
SpringLayout layout = new SpringLayout();
panel.setLayout( layout);
panel.add (inputLabel);
panel.add (Ans);
panel.add (resultLabel);
panel.add (nextQ);
panel.add (WQues);

layout.putConstraint(SpringLayout.WEST, inputLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.EAST, inputLabel, 5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, inputLabel, 15, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.NORTH, Ans, 30, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, Ans, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, resultLabel, 30, SpringLayout.SOUTH, inputLabel);
layout.putConstraint(SpringLayout.WEST, resultLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, nextQ, 10, SpringLayout.SOUTH, Ans);
layout.putConstraint(SpringLayout.EAST, nextQ, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, WQues, 10, SpringLayout.SOUTH, nextQ);
layout.putConstraint(SpringLayout.EAST, WQues, -5, SpringLayout.EAST, contentPane);


frame.getContentPane().add (panel);
}

public void display()
{
frame.pack();
frame.show();
}

private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{

resultLabel.setText ("Answer: "+answerA.get(qnum).toString());
}
}

private class TempListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
questionA.remove(qnum);
answerA.remove(qnum);
qnum=gen.nextInt(questionA.size());
inputLabel.setText ("Question: "+questionA.get(qnum).toString());
resultLabel.setText ("Answer: ");

}
}

private class TempListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
qnum=gen.nextInt(questionA.size());
inputLabel.setText ("Question: "+questionA.get(qnum).toString());
resultLabel.setText ("Answer: ");

}
}
}


This is how it's supposed to look like
http://img.photobucket.com/albums/v299/YRPNova/e41eaf8f.jpg

Aradon
07-23-2007, 05:19 PM
I'm presently out of town and unable to run your code freely in my common editor, however from just a moment's look You are doing this:


questionA.add(Abhor);
questionA.add(Bigot);
questionA.add(Counterfeit);
questionA.add(Enfranchise);


What type is Abhor? Is it a variable? Or do you mean the word Abhor?

If you mean the word then I think you're looking to use Strings, instead of just the actual word. What do I mean you ask?

Well Strings in java are any set of characters or numbers or symbols that appear between two quotation marks. For example


System.out.println("Hello World!");


Hello World! is a String that is being outputted to the system.

So if we wanted to add Strings to the ArrayList we could do..



questionA.add("Abhor");
questionA.add("Bigot");
questionA.add("Counterfeit");
questionA.add("Enfranchise");




Try that out and see if it works. If not, give up a log of your compilation error.

MNov
07-25-2007, 10:41 PM
Oh right, the quotation marks. :p

Anyways, after I put in the quotation marks I get these errors. I tried making a 'new String' too but it doesn't work either.

Quiz.java:20: <identifier> expected
questionA.add(new String("Abhor"));
^
Quiz.java:20: illegal start of type
questionA.add(new String("Abhor"));
^
Quiz.java:21: <identifier> expected
questionA.add("Bigot");
^
Quiz.java:21: illegal start of type
questionA.add("Bigot");

Aradon
07-26-2007, 07:32 AM
Okay, second thing.

When we are writing a java program we can put our private and public variable declerations outside of the scope of a method (such as the decleration of questionA, but we can not add to it unless it's in a method.

As I see it you want these questions added at init, so placing them inside the init method would be best..

So you would have something like..


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;

public class Quiz extends JApplet
{
private int WIDTH = 400;
private int HEIGHT = 200;

private JFrame frame;
private JPanel panel;
private JLabel inputLabel, outputLabel, resultLabel;
private JButton nextQ, Ans, WQues;
private JTextField question;
ArrayList questionA = new ArrayList();
ArrayList answerA = new ArrayList();

public void init()
{

questionA.add("Abhor");
questionA.add("Bigot");
/// Other Questions and Answers Here

//***************
Random gen = new Random();
int qnum=gen.nextInt(questionA.size());

frame = new JFrame ("Quizzer");
Container contentPane = frame.getContentPane();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

inputLabel = new JLabel ("");
inputLabel.setMaximumSize(new Dimension(500, 130));
inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
Font f =new Font("Serif", Font.ITALIC, 20);
inputLabel.setFont(f);
inputLabel.setText("Question: "+questionA.get(qnum).toString());
resultLabel = new JLabel ("Answer: ");

nextQ = new JButton ("I got it Right");

nextQ.addActionListener (new TempListener2());
Ans = new JButton ("Answer");
Ans.addActionListener (new TempListener());
resultLabel.setMaximumSize(new Dimension(500, 200));
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
resultLabel.setFont(f);
WQues = new JButton ("I got it Wrong");
WQues.addActionListener (new TempListener3());

panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.yellow);
SpringLayout layout = new SpringLayout();
panel.setLayout( layout);
panel.add (inputLabel);
panel.add (Ans);
panel.add (resultLabel);
panel.add (nextQ);
panel.add (WQues);

layout.putConstraint(SpringLayout.WEST, inputLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.EAST, inputLabel, 5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, inputLabel, 15, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.NORTH, Ans, 30, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, Ans, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, resultLabel, 30, SpringLayout.SOUTH, inputLabel);
layout.putConstraint(SpringLayout.WEST, resultLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, nextQ, 10, SpringLayout.SOUTH, Ans);
layout.putConstraint(SpringLayout.EAST, nextQ, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, WQues, 10, SpringLayout.SOUTH, nextQ);
layout.putConstraint(SpringLayout.EAST, WQues, -5, SpringLayout.EAST, contentPane);


frame.getContentPane().add (panel);
}

/// etc. etc.


Or something to that fact..

MNov
07-30-2007, 06:38 PM
Ohhh.. cool. I didn't know about that rule XD. Okay so now there's no errors during compiling but the applet won't run.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;
import java.applet.Applet;


public class Quiz extends JApplet
{
private int WIDTH = 400;
private int HEIGHT = 200;

private JFrame frame;
private JPanel panel;
private JLabel inputLabel, outputLabel, resultLabel;
private JButton nextQ, Ans, WQues;
private JTextField question;
ArrayList questionA = new ArrayList();
ArrayList answerA = new ArrayList();

SpringLayout layout = new SpringLayout();
Container contentPane;
Font f;


Random gen = new Random();
int qnum;

public void init()
{
setQuestions();
qnum=gen.nextInt(questionA.size());

frame = new JFrame ("Quizzer");
contentPane = frame.getContentPane();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

inputLabel = new JLabel ("");
inputLabel.setMaximumSize(new Dimension(500, 130));
inputLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
f =new Font("Serif", Font.ITALIC, 20);
inputLabel.setFont(f);
inputLabel.setText("Question: "+questionA.get(qnum).toString());
resultLabel = new JLabel ("Answer: ");

nextQ = new JButton ("I got it Right");

nextQ.addActionListener (new TempListener2());
Ans = new JButton ("Answer");
Ans.addActionListener (new TempListener());
resultLabel.setMaximumSize(new Dimension(500, 200));
resultLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
resultLabel.setFont(f);
WQues = new JButton ("I got it Wrong");
WQues.addActionListener (new TempListener3());

panel = new JPanel();
panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
panel.setBackground (Color.yellow);

panel.setLayout( layout);
panel.add (inputLabel);
panel.add (Ans);
panel.add (resultLabel);
panel.add (nextQ);
panel.add (WQues);

layout.putConstraint(SpringLayout.WEST, inputLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.EAST, inputLabel, 5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, inputLabel, 15, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.NORTH, Ans, 30, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, Ans, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, resultLabel, 30, SpringLayout.SOUTH, inputLabel);
layout.putConstraint(SpringLayout.WEST, resultLabel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, nextQ, 10, SpringLayout.SOUTH, Ans);
layout.putConstraint(SpringLayout.EAST, nextQ, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, WQues, 10, SpringLayout.SOUTH, nextQ);
layout.putConstraint(SpringLayout.EAST, WQues, -5, SpringLayout.EAST, contentPane);


frame.getContentPane().add (panel);
}

public void start()
{
frame.pack();
frame.show();
}

private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{

resultLabel.setText ("Answer: "+answerA.get(qnum).toString());
}
}

private class TempListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
questionA.remove(qnum);
answerA.remove(qnum);
qnum=gen.nextInt(questionA.size());
inputLabel.setText ("Question: "+questionA.get(qnum).toString());
resultLabel.setText ("Answer: ");

}
}

private class TempListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
qnum=gen.nextInt(questionA.size());
inputLabel.setText ("Question: "+questionA.get(qnum).toString());
resultLabel.setText ("Answer: ");

}
}
private void setQuestions()
{
questionA.add((new String("Abhor")));
/....
answerA.add("hate");
//...

}
}

This is what I get when I choose 'Run as Applet'
----jGRASP exec: appletviewer jgrasphta.htm

java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkExit(SecurityManager.java:744)
at javax.swing.JFrame.setDefaultCloseOperation(JFrame.java:377)
at Quiz.init(Quiz.java:37)
at sun.applet.AppletPanel.run(AppletPanel.java:417)
at java.lang.Thread.run(Thread.java:619)

----jGRASP: operation complete.
It says
Start: applet not initialized.
in the applet viewer