plz help me n00b here


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: plz help me n00b here

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    6

    plz help me n00b here

    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 :


    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;
    import java.io.*;
    import java.awt.event.*;
    
    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;
    void gen()
    {
    GridLayout g1=new GridLayout(5,5);
    JPanel p1=new JPanel();
    getContentPane().add(p1);
    p1.setLayout(g1);
    
    JButton b0=new JButton("");
    JButton b1=new JButton("");
    JButton b2=new JButton("");
    JButton b3=new JButton("");
    JButton b4=new JButton("");
    JButton b5=new JButton("");
    JButton b6=new JButton("");
    JButton b7=new JButton("");
    JButton b8=new JButton("");
    JButton b9=new JButton("");
    JButton b10=new JButton("");
    JButton b11=new JButton("");
    JButton b12=new JButton("");
    JButton b13=new JButton("");
    JButton b14=new JButton("");
    JButton b15=new JButton("");
    JButton b16=new JButton("");
    JButton b17=new JButton("");
    JButton b18=new JButton("");
    JButton b19=new JButton("");
    JButton b20=new JButton("");
    JButton b21=new JButton("");
    JButton b22=new JButton("");
    JButton b23=new JButton("");
    JButton b24=new JButton("");
    
    p1.add(b0);
    p1.add(b1);
    p1.add(b2);
    p1.add(b3);
    p1.add(b4);
    p1.add(b5);
    p1.add(b6);
    p1.add(b7);
    p1.add(b8);
    p1.add(b9);
    p1.add(b10);
    p1.add(b11);
    p1.add(b12);
    p1.add(b13);
    p1.add(b14);
    p1.add(b15);
    p1.add(b16);
    p1.add(b17);
    p1.add(b18);
    p1.add(b19);
    p1.add(b20);
    p1.add(b21);
    p1.add(b22);
    p1.add(b23);
    p1.add(b24);
    
    b0.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    b10.addActionListener(this);
    b11.addActionListener(this);
    b12.addActionListener(this);
    b13.addActionListener(this);
    b14.addActionListener(this);
    b15.addActionListener(this);
    b16.addActionListener(this);
    b17.addActionListener(this);
    b18.addActionListener(this);
    b19.addActionListener(this);
    b20.addActionListener(this);
    b21.addActionListener(this);
    b22.addActionListener(this);
    b23.addActionListener(this);
    b24.addActionListener(this);
    
    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;
    
    try 
    {//try
    if(b==0 && val==0 || b==1 && val==1 || b==2 && val==2 || b==3 && val==3 || b==4 && val==4 
    || obj==b5 && val==5 || obj==b6 && val==6 || obj==b7 && val==7 || obj==b8 && val==8 || obj==b9 && val==9 
    || obj==b10 && val==10 || obj==b11 && val==11 || obj==b12 && val==13 || obj==b14 && val==14 || obj==b15 
    && val==15 || obj==b16 && val==16 || obj==b17 && val==17 || obj==b18 && val==18 || obj==b19 && val==19 
    || obj==b20 && val==20 || obj==b21 && val==21 || obj==b22 && val==22 || obj==b23 && val==23 || obj==b24 && val==24)
    {
    score=score+1;
    getAppletContext().showStatus("Your score is"+score);
    }
    else
    {
    score=score-1;	
    getAppletContext().showStatus("Your score is"+score);
    }
    }//try
    catch(Exception e)
    {
    System.out.println(""+e);
    }
    repaint();
    }//action performed
    }//smackitt

    plz plz help me (
    Last edited by Apocalyp5e; 12-20-2005 at 04:11 PM. Reason: using the code tag

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Shouldnt this be:
    Code:
    if((b==0 && val==0) || (b==1 && val==1) || ( b==2 && val==2) || (b==3 && val==3) || (b==4 && val==4) || (obj==b5 && val==5) || (obj==b6 && val==6) || (obj==b7 && val==7) || (obj==b8 && val==8) || (obj==b9 && val==9) || (obj==b10 && val==10) || (obj==b11 && val==11) || (obj==b12 && val==13) || (obj==b14 && val==14) || (obj==b15 && val==15) || (obj==b16 && val==16) || (obj==b17 && val==17) || (obj==b18 && val==18) || (obj==b19 && val==19)|| (obj==b20 && val==20) || (obj==b21 && val==21) || (obj==b22 && val==22) || (obj==b23 && val==23) || (obj==b24 && val==24))
    BTW use [code] tags dude, makes your code much easier to read..

  3. #3
    Join Date
    Dec 2005
    Posts
    6
    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.

  4. #4
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    Just put your code between [code] and [ /code] without a space between [ and /

  5. #5
    Join Date
    Dec 2005
    Posts
    6
    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 -.

    ????

  6. #6
    Join Date
    Oct 2005
    Posts
    107
    repaint simply calls paint. Your paint method is reinstantiating everything each time which is wrong.

    public void paint(Graphics g)
    {
    gen();
    }

  7. #7
    Join Date
    Dec 2005
    Posts
    6
    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.

  8. #8
    Join Date
    May 2005
    Posts
    75
    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...
    Code:
    getAppletContext().showStatus("Your score is"+score);
    Code:
    getAppletContext().showStatus("Your score is"+score+" b="+b+", val ="+val);
    Good Luck!

  9. #9
    Join Date
    Dec 2005
    Posts
    6
    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

  10. #10
    Join Date
    May 2005
    Posts
    75
    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.
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;
    import java.io.*;
    import java.awt.event.*;
    
    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 init()
    {
    
    r = new Random();
    
    b0=new JButton("");
    b1=new JButton("");
    b2=new JButton("");
    b3=new JButton("");
    b4=new JButton("");
    b5=new JButton("");
    b6=new JButton("");
    b7=new JButton("");
    b8=new JButton("");
    b9=new JButton("");
    b10=new JButton("");
    b11=new JButton("");
    b12=new JButton("");
    b13=new JButton("");
    b14=new JButton("");
    b15=new JButton("");
    b16=new JButton("");
    b17=new JButton("");
    b18=new JButton("");
    b19=new JButton("");
    b20=new JButton("");
    b21=new JButton("");
    b22=new JButton("");
    b23=new JButton("");
    b24=new JButton("");
    
    b0.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    b10.addActionListener(this);
    b11.addActionListener(this);
    b12.addActionListener(this);
    b13.addActionListener(this);
    b14.addActionListener(this);
    b15.addActionListener(this);
    b16.addActionListener(this);
    b17.addActionListener(this);
    b18.addActionListener(this);
    b19.addActionListener(this);
    b20.addActionListener(this);
    b21.addActionListener(this);
    b22.addActionListener(this);
    b23.addActionListener(this);
    b24.addActionListener(this);
    
    gen();
    }//init
    
    public void gen()
    {
    b0.setText("");
    b1.setText("");
    b2.setText("");
    b3.setText("");
    b4.setText("");
    b5.setText("");
    b6.setText("");
    b7.setText("");
    b8.setText("");
    b9.setText("");
    b10.setText("");
    b11.setText("");
    b12.setText("");
    b13.setText("");
    b14.setText("");
    b15.setText("");
    b16.setText("");
    b17.setText("");
    b18.setText("");
    b19.setText("");
    b20.setText("");
    b21.setText("");
    b22.setText("");
    b23.setText("");
    b24.setText("");
    
    val = r.nextInt(25);
    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
    
    GridLayout g1=new GridLayout(5,5);
    JPanel p1=new JPanel();
    getContentPane().add(p1);
    p1.setLayout(g1);
    
    p1.add(b0);
    p1.add(b1);
    p1.add(b2);
    p1.add(b3);
    p1.add(b4);
    p1.add(b5);
    p1.add(b6);
    p1.add(b7);
    p1.add(b8);
    p1.add(b9);
    p1.add(b10);
    p1.add(b11);
    p1.add(b12);
    p1.add(b13);
    p1.add(b14);
    p1.add(b15);
    p1.add(b16);
    p1.add(b17);
    p1.add(b18);
    p1.add(b19);
    p1.add(b20);
    p1.add(b21);
    p1.add(b22);
    p1.add(b23);
    p1.add(b24);
    
    }
    
    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;
    
    try
    {//try
    if(b==0 && val==0 || b==1 && val==1 || b==2 && val==2 || b==3 && val==3 || b==4 && val==4
    || obj==b5 && val==5 || obj==b6 && val==6 || obj==b7 && val==7 || obj==b8 && val==8 || obj==b9 && val==9
    || obj==b10 && val==10 || obj==b11 && val==11 || obj==b12 && val==13 || obj==b14 && val==14 || obj==b15
    && val==15 || obj==b16 && val==16 || obj==b17 && val==17 || obj==b18 && val==18 || obj==b19 && val==19
    || obj==b20 && val==20 || obj==b21 && val==21 || obj==b22 && val==22 || obj==b23 && val==23 || obj==b24 && val==24)
    {
    score=score+1;
    }
    else
    {
    score=score-1;
    }
    }//try
    catch(Exception e)
    {
    System.out.println(""+e);
    }
    getAppletContext().showStatus("Your score is "+score+" b = "+b+", val = "+val);
    gen();
    }//action performed
    }//smackitt
    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!
    Last edited by srekcus; 12-20-2005 at 11:56 PM.

  11. #11
    Join Date
    Dec 2005
    Posts
    6
    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.
    Last edited by Apocalyp5e; 12-22-2005 at 07:58 PM.

  12. #12
    Join Date
    May 2005
    Posts
    75
    I'm glad it's working for you

    The code actually 46 lines
    [I]
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    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.

Similar Threads

  1. plz help me to solve this problem
    By ankushbindra in forum ASP.NET
    Replies: 1
    Last Post: 11-30-2005, 06:13 PM
  2. plz help Newbie:object library not registered........
    By cooldude.i2k in forum VB Classic
    Replies: 1
    Last Post: 08-11-2005, 03:16 PM
  3. plz plz plz help me(java)
    By kamon in forum Java
    Replies: 4
    Last Post: 08-10-2005, 10:30 AM
  4. java applet... i'm in a fix, plz help
    By nsbscool in forum Java
    Replies: 1
    Last Post: 05-02-2005, 08:01 AM
  5. Plz Help!!!!
    By Kyle in forum Java
    Replies: 0
    Last Post: 05-14-2003, 03:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center