i started to make a applet for the game whack a mole i named that samckitt but this is giving me probs it compiles but it always give me -ve score i tried to debug this but of no use i think somthing wrong with the repaint method :(( i dont know what wrong so i m submitting it so guys plz help me :
Random r=new Random();
int val;
val=r.nextInt(24);
switch(val)
{//switch
case 0:
{
b0.setText("SmackIt");
}
break;
case 1:
{
b1.setText("SmackIt");
}
break;
case 2:
{
b2.setText("SmackIt");
}
break;
case 3:
{
b3.setText("SmackIt");
}
break;
case 4:
{
b4.setText("SmackIt");
}
break;
case 5:
{
b5.setText("SmackIt");
}
break;
case 6:
{
b6.setText("SmackIt");
}
break;
case 7:
{
b7.setText("SmackIt");
}
break;
case 8:
{
b8.setText("SmackIt");
}
break;
case 9:
{
b9.setText("SmackIt");
}
break;
case 10:
{
b10.setText("SmackIt");
}
break;
case 11:
{
b11.setText("SmackIt");
}
break;
case 12:
{
b12.setText("SmackIt");
}
break;
case 13:
{
b13.setText("SmackIt");
}
break;
case 14:
{
b14.setText("SmackIt");
}
break;
case 15:
{
b15.setText("SmackIt");
}
break;
case 16:
{
b16.setText("SmackIt");
}
break;
case 17:
{
b17.setText("SmackIt");
}
break;
case 18:
{
b18.setText("SmackIt");
}
break;
case 19:
{
b19.setText("SmackIt");
}
break;
case 20:
{
b20.setText("SmackIt");
}
break;
case 21:
{
b21.setText("SmackIt");
}
break;
case 22:
{
b22.setText("SmackIt");
}
break;
case 23:
{
b23.setText("SmackIt");
}
break;
case 24:
{
b24.setText("SmackIt");
}
break;
}//switch
}
public void init()
{
gen();
b=40;
}//init
public void start()
{}
public void stop()
{}
public void paint(Graphics g)
{
gen();
}
public void actionPerformed(ActionEvent ae)
{//actionperformed
Object obj;
obj=ae.getSource();
if (obj==b0) b=0;
else if (obj==b1) b=1;
else if (obj==b2) b=2;
else if (obj==b3) b=3;
else if (obj==b4) b=4;
else if (obj==b5) b=5;
else if (obj==b6) b=6;
else if (obj==b7) b=7;
else if (obj==b8) b=8;
else if (obj==b9) b=9;
else if (obj==b10) b=10;
else if (obj==b11) b=11;
else if (obj==b12) b=12;
else if (obj==b13) b=13;
else if (obj==b14) b=14;
else if (obj==b15) b=15;
else if (obj==b16) b=16;
else if (obj==b17) b=17;
else if (obj==b18) b=18;
else if (obj==b19) b=19;
else if (obj==b20) b=20;
else if (obj==b21) b=21;
else if (obj==b22) b=22;
else if (obj==b23) b=23;
else if (obj==b24) b=24;
BTW use [code] tags dude, makes your code much easier to read.. :)
12-15-2005, 05:32 PM
Apocalyp5e
thx i will try it out and i m new to java so tell me how to use that [code] tags .
by the way thanks mate.
12-15-2005, 05:46 PM
Code_Nerd
Just put your code between [code] and [ /code] without a space between [ and /
12-15-2005, 05:47 PM
Apocalyp5e
nah its not working evrything was working just fine b4 that repaint method was applied but after that all gone it screwd the code actully i m gentting +1 first time and after that all score in -.
????
12-15-2005, 06:17 PM
Joe Beam
repaint simply calls paint. Your paint method is reinstantiating everything each time which is wrong.
public void paint(Graphics g)
{
gen();
}
12-15-2005, 06:55 PM
Apocalyp5e
yeah i know that but tell me how i can do it at least tell me alternative i tried to use do while but the same prob here, it reassign the values but i get same display every time so i called the repaint method the main prob here is the how can i put that code in a loop so i can smackit on a ramdom button everytime its clicked the codes are okey to genrate that random button and the scoring goes well but after adding the repaint all screwedi tried mouseListeners too but its of no use.
by the way thanks everyone.
12-17-2005, 10:48 PM
srekcus
The main problem with your code is that the variable b is always equal to 40 (defined in the init() method)), it never changes. In other words the following code isn't working
Code:
Object obj;
obj=ae.getSource();
if (obj==b0) b=0;
else if (obj==b1) b=1;
else if (obj==b2) b=2;
else if (obj==b3) b=3;
else if (obj==b4) b=4;
else if (obj==b5) b=5;
else if (obj==b6) b=6;
else if (obj==b7) b=7;
else if (obj==b8) b=8;
else if (obj==b9) b=9;
else if (obj==b10) b=10;
else if (obj==b11) b=11;
else if (obj==b12) b=12;
else if (obj==b13) b=13;
else if (obj==b14) b=14;
else if (obj==b15) b=15;
else if (obj==b16) b=16;
else if (obj==b17) b=17;
else if (obj==b18) b=18;
else if (obj==b19) b=19;
else if (obj==b20) b=20;
else if (obj==b21) b=21;
else if (obj==b22) b=22;
else if (obj==b23) b=23;
else if (obj==b24) b=24;
Also, in the gen() method, you will need change
Code:
Random r=new Random();
int val;
val=r.nextInt(4);
to
Code:
Random r=new Random();
val=r.nextInt(4);
You already defined val as an int object above the gen() method. Adding int val; again removes the variable's global scope. In other words, it is always equals 0 inside the actionPerformed() method.
I would highly recommend this change to your code in order to debug it...
getAppletContext().showStatus("Your score is"+score+" b="+b+", val ="+val);
Good Luck!
12-20-2005, 04:08 PM
Apocalyp5e
thank ya mate for ur suggestions ,
but about the first suggestion i tried that too but thats of no use
the second one is good i m going to try that;
and about the third one i need +1 every time user click on smackitt button and -1 if he clicked any other button then smackitt button
but thx again
12-20-2005, 11:42 PM
srekcus
The main problem with your applet remains that the buttons are not changing the value of b. I got it working for you after changing little of the original code. I moved things around and added/deleted a few others.
public class Smackitt extends JApplet implements ActionListener,Serializable
{//smackitt
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24;
int val,score=0,b;
String str1;
Random r;
public void actionPerformed(ActionEvent ae)
{//actionperformed
Object obj;
obj=ae.getSource();
if (obj==b0) b=0;
else if (obj==b1) b=1;
else if (obj==b2) b=2;
else if (obj==b3) b=3;
else if (obj==b4) b=4;
else if (obj==b5) b=5;
else if (obj==b6) b=6;
else if (obj==b7) b=7;
else if (obj==b8) b=8;
else if (obj==b9) b=9;
else if (obj==b10) b=10;
else if (obj==b11) b=11;
else if (obj==b12) b=12;
else if (obj==b13) b=13;
else if (obj==b14) b=14;
else if (obj==b15) b=15;
else if (obj==b16) b=16;
else if (obj==b17) b=17;
else if (obj==b18) b=18;
else if (obj==b19) b=19;
else if (obj==b20) b=20;
else if (obj==b21) b=21;
else if (obj==b22) b=22;
else if (obj==b23) b=23;
else if (obj==b24) b=24;
I think the reason you are having problems is because you are creating a whole new set of JButtons everytime you run the gen() method, and it is screwing up which buttons actually hold the action listener, and when.
Instead I initialized every object in init() and the gen() method merely manipulates them.
On a side note, I rewrote your program (for fun) and reduced the number of lines from 303 to 50 ... let me know and I'll post it. Otherwise, I don't want to spoil your learning process.
I hope this helps! :WAVE:
12-22-2005, 07:43 PM
Apocalyp5e
wheeehaaaa at last thats working fine :) and yeah i want to see that optimized code 330 to 50 lines thats gr8 and now i m going to add a timer and a proper scoreboard to this incomplete game. I will post the codes if i have any probs :P and hey srekcus thx for helping me out and thx to all :P
i will post the codes if somehow i complete it. :D
public class Smackitt2 extends JApplet implements ActionListener {
private JButton[] jb = new JButton[25];
private int b=0,val=0,score=0;
public void init(){
setLayout(new GridLayout(5,5));
for(int i = 0; i < jb.length; i++){
jb[i] = new JButton("");
jb[i].addActionListener(this);
add(jb[i]);
}
gen();
}
public void actionPerformed(ActionEvent ae) {
for(int i = 0; i < jb.length; i++){
if(ae.getSource() == jb[i]){
b = i;
i = jb.length; // break loop
}
}
if(b == val){
score++;
} else {
score--;
}
getAppletContext().showStatus("Your score is "+score+", b = "+b+", val = "+val);
gen();
}
public void gen(){
val = (int)(Math.random()*25);
for(int i = 0; i < jb.length; i++){
if(i == val){
jb[i].setText("Smack!");
} else {
jb.setText("");
}
}
}
}
I create an array of 25 JButtons and simply use for loops to cycle between all of them during each operation. If you compile this code you'll see it works exactly the same as my previous posting. You'll also notice that I defined the Layout in the init() method so it is only executed once. All the gen() method does is change the text of each button.