Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Havin Trouble with Applets...Please Help Me...



i have started learning java a month back.....i ws tryin to develop a program which will print 5 random questions and there 4 options in a radio button one by one.....get there answers on button click or wait for 10 seconds to move to next que................

i cant figure our wt hs gone wrong wid this program.....compiles with no error...bt the appletviewer says
start: applet not initialised and nothing gets displayed..................

just help me why isnt the applet showing anything.....i ll figure out the rest..

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

/*

*/

public class OnExam extends Applet implements Runnable,ItemListener,ActionListener
{

private String str[]={"Where is Otawa?","Who is the PM of Australia?",
"What is a Oasis?","When is Rose Day?",
"Which city is called called pink city"};
private String anslist[][]={
{"Canada","Russia","Australia","USA"},
{"John Howard","Tony Blair","Matthew Bell","Kevin Rudd"},
{"Desert Storm","Isolated area of vegetation in a desert","Name of an island","Website"},
{"13 Feb","11 Feb","8 Feb","7 Feb"},
{"Jodhpur","Bikaner","Ajmer","Jaipur"}
};
private int validresponse[]={0,3,1,0,3};

private String str1=str[0];
private String ans[]=anslist[0];

private final int size=5;
private TextField tx;
private Label lbl;
private CheckboxGroup cbg;
private Checkbox c1;
private Checkbox c2;
private Checkbox c3;
private Checkbox c4;
private Button b1;
public int tl;
public int noq=1;
private int currentindex=-1;
private int score=0;
protected Thread timerThread;
protected Thread queThread;

public void init()
{
lbl=new Label("*************************************************************************************************************************************************************************************************");
cbg=new CheckboxGroup();
c1=new Checkbox("*********************************************************",cbg,false);
c2=new Checkbox("*********************************************************",cbg,false);
c3=new Checkbox("*********************************************************",cbg,false);
b1=new Button("Submit");
tx=new TextField(50);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
b1.addActionListener(this);
add(lbl);
add(c1);
add(c2);
add(c3);
add(c4);
add(b1);
add(tx);
}

public void start()
{
timerThread = new Thread(this, "Clock");
queThread = new Thread(this);
timerThread.start();
}

public void run()
{
if (tl!=0)
{
while(noq<=size + 1)
{

queThread.start();
tl=10;
while ( tl!=0)
{
repaint(); // request a redraw
try
{
timerThread.sleep(1000);
} catch (InterruptedException e){ /* do nothing*/ }
tl--;
}
}
if (currentindex!=-1)
{
if (cbg.getSelectedCheckbox().getLabel()==anslist[currentindex][validresponse[currentindex]])
{
score+=1;
currentindex=-1;
}
}
}
else if(tl==0)
{
if(noq<=size)
{
Random r=new Random();
currentindex=r.nextInt(size-1);
str1=str[currentindex];
ans=anslist[currentindex];
lbl.setText(str1);
c1.setLabel(ans[0]);
c2.setLabel(ans[1]);
c3.setLabel(ans[2]);
c4.setLabel(ans[3]);
tx.setText("");
noq++;
}
else if(noq==size+1)
{
lbl.setText("exam completed...");
c1.setVisible(false);
c2.setVisible(false);
c3.setVisible(false);
c4.setVisible(false);
b1.setVisible(false);
tx.setText("You answered "+ score +" correct answers!!");
}
}

}

public void itemStateChanged (ItemEvent ie)
{
tx.setText("You have Selected " + cbg.getSelectedCheckbox().getLabel());
}

public void actionPerformed (ActionEvent ae)
{
if (ae.getSource()==b1)
{
tl=0;
try
{
timerThread.join();
}
catch (Exception e){}
}
}

public void stop()
{
}

public void paint(Graphics g)
{
g.drawString("Time Left: "+ tl,200,200);
}

}