Results 1 to 11 of 11
  1. #1
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default how to insert new button in the menu as "Credits"

    i need help..me and my friend have create a java snake game..we need help to insert/create a button named "Credits" on the menu which when we click it,our name will appear as it is 'credits' to us for creating the game..here i attached our coding for the game..hopefully there is someone who can help us,this is our final project program..thank you


    import java.awt.*;
    import java.applet.*;


    public class snake extends Applet implements Runnable
    {
    Image dot[]=new Image[600]; //declare image dot
    Image back;
    Image offI;
    Graphics offG;

    int x[]= new int[600];
    int y[]= new int[600];
    int rtemp=1;
    int game=1;
    int level; //declare level
    int z;
    int n;
    int count=0; //declare count
    int score=0; //declare score
    int add=1;

    Button u= new Button("Insert your name");

    Button b= new Button("Budak Baru");
    Button i= new Button("Cekap Sikit");
    Button p= new Button("Cekap Banyak");
    Button X= new Button("Melampau");






    String stemp;
    String s;
    String t;

    boolean go[]=new boolean[600];
    boolean left=false;
    boolean right=false;
    boolean up=false;
    boolean down=false;
    boolean started=false ;
    boolean me=false;

    Thread setTime;

    public void init()
    {
    add(u);
    add(b);
    add(i);
    add(p);
    add(X);
    setBackground(Color.green); //background color
    back = getImage(getCodeBase(), "");

    for (z=0 ; z < 600 ; z++){dot[z] = getImage(getCodeBase(), "dot.gif"); }
    }



    public void update(Graphics g)
    {
    Dimension d = this.size();
    if(offI == null)
    {
    offI = createImage(d.width, d.height);
    offG = offI.getGraphics();
    }
    offG.clearRect(0, 0, d.width, d.height);
    paint(offG);
    g.drawImage(offI, 0, 0, null);
    }

    public void paint(Graphics g)
    {
    g.drawImage(back,0,0, this);
    g.setColor(Color.black);

    if(started)
    {
    g.setFont(new Font("Verdana", 1, 12));
    t = "Score "+score+"";
    g.drawString(t, 200, 490);
    }

    if(game==1)
    {
    g.setFont(new Font("Verdana", 1, 13));
    s = "Tahap Anda";
    g.drawString(s, 200, 30);

    u.move(190,230);


    b.move(200, 50); //kedudukan button level
    i.move(200, 90);
    p.move(195, 130);
    X.move(200, 170);
    }

    if((game==2)||(game==3))
    {
    if(!started)
    {
    g.setFont(new Font("Verdana", 1, 11));
    t = "Use the key board arrows to move!";
    g.drawString(t, 150,300);
    }
    for (z=0 ; z <= n ; z++){ g.drawImage(dot[z],x[z],y[z],this); }
    me=true;
    }

    if(!me)
    {
    g.setFont(new Font("Verdana", 1, 11));
    t = "by NABIL & FADHLI";
    g.drawString(t, 300, 495);
    }

    if(game==3)
    {
    g.setFont(new Font("Verdana", 1, 13));
    s="ANDA KALAH..CUBA LAGI";
    g.drawString(s, 200, 60);
    }

    }

    public void run()
    {
    for(z=4 ;z <600 ; z++) { go[z]=false;}
    for(z=0 ; z<4 ; z++) { go[z]=true;x[z]=91;y[z]=91;}
    n=3;
    game=2;
    score=0;

    u.move(70,-100);

    b.move(70, -100); //kedudukan button selepas click
    i.move(70, -100);
    p.move(70, -100);
    X.move(70, -100);
    left=false;
    right=false;
    up=false;
    down=false;
    locateRandom(4);

    while(true)
    {
    if (game==2)
    {
    if ((x[0]==x[n])&&(y[0]==y[n])){go[n]=true;locateRandom((n+1));score+=add; }
    for(z = 599 ; z > 0 ; z--)
    {
    if (go[z])
    {
    x[z] = x[(z-1)]; y[z] = y[(z-1)];
    if ((z>4)&&(x[0]==x[z])&&(y[0]==y[z])){ game=3; }
    }
    }
    if(left){ x[0]-=10; }
    if(right){ x[0]+=10; }
    if(up){ y[0]-=10; }
    if(down){ y[0]+=10; }
    }

    //saiz border
    if(y[0]>500){y[0]=500;game=3;}
    if(y[0]<1){y[0]=1;game=3;}
    if(x[0]>500){x[0]=500;game=3;}
    if(x[0]<1){x[0]=1;game=3;}

    if (game==3)
    {
    if (count <(1500/level)) { count++; } else { count=0;game=1;repaint();setTime.stop(); }
    }

    repaint();
    try{setTime.sleep(level);}
    catch(InterruptedException e){}
    }
    }

    public void locateRandom(int turn)
    {
    rtemp=(int)(Math.random()*50); //locate for dot
    x[turn]=((rtemp*10)+1) ;
    rtemp=(int)(Math.random()*50);
    y[turn]=((rtemp*10)+1);
    n++;
    }

    public boolean keyDown(Event e, int key)
    {
    if ((key == Event.LEFT) &&(!right)){left = true; up = false; down = false;if(!started)started=true;}
    if ((key == Event.RIGHT) && (!left)){right = true; up = false; down = false;if(!started)started=true;}
    if ((key == Event.UP) && (!down)){ up = true; right = false; left = false;if(!started)started=true;}
    if ((key == Event.DOWN) && (!up)){down = true; right = false; left = false;if(!started)started=true;}
    return true;
    }

    public boolean action(Event event, Object obj)
    {
    stemp = (String) obj;

    if(stemp.equals("Budak Baru"))
    {
    add=2;
    level=100;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }

    if(stemp.equals("Cekap Sikit"))
    {
    add=5;
    level=70;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }

    if(stemp.equals("Cekap Banyak"))
    {
    add=10;
    level=40;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }

    if(stemp.equals("Melampau"))
    {
    add=20;
    level=20;
    setTime = new Thread(this);
    setTime.start();
    return true;
    }


    return false;
    }


    }

  2. #2
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default Re: how to insert new button in the menu as "Credits"

    Hello nabil,

    where do you want to add your name..
    like menubar (help -> about) or just you want to show your name when u click the credit button...

    Note : and also please post your code by using the <code> tag... dont paste like this hereafter
    ThanX
    Rj

  3. #3
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: how to insert new button in the menu as "Credits"

    i'm so sorry for my improper way of posting this coding,i'm new here..forgive me about that..
    when we run the game,the applet will appear with on the first page,there will be 4 level of the game(4 level button),so i want to add another button as a 'credit button'..which mean when we click (credits -> my name appear)..hope that u can understand what am i asking..by the way,thanks..

  4. #4
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: how to insert new button in the menu as "Credits"

    Quote Originally Posted by rjagan View Post
    Hello nabil,

    where do you want to add your name..
    like menubar (help -> about) or just you want to show your name when u click the credit button...

    Note : and also please post your code by using the <code> tag... dont paste like this hereafter

    i've explain the way that i want the name will appear,i hope u can help me rjagan..

  5. #5
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default prblem to create button..

    i got a problem to create a button..
    the button name is "CREDITS"..
    after we click it,we can display our name..
    it will look like this.. (Credits -> dislay name)
    what should i do to create it??
    please somebody help me..

    best regards-nabil

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: prblem to create button..

    There will be a lot more to a GUI program than creating a button. What do you know about creating a GUI app?

    Is this a java program you are trying to write?

    Do a Search on this forum for JButton for lots of code samples.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: prblem to create button..

    Quote Originally Posted by Norm View Post
    There will be a lot more to a GUI program than creating a button. What do you know about creating a GUI app?

    Is this a java program you are trying to write?

    Do a Search on this forum for JButton for lots of code samples.

    yeah..i want to insert the buuton into my snake games menu,as a credit..
    so when people play the game,they can click the 'credit' button and my name as the game creator will appear..
    how should i do it??

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: prblem to create button..

    Create an instance of the class and add it to the GUI container.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: prblem to create button..

    Quote Originally Posted by Norm View Post
    Create an instance of the class and add it to the GUI container.
    ok,i've done it..it works..thank you NORM~~

  10. #10
    nabil is offline Member
    Join Date
    Jan 2012
    Posts
    17
    Rep Power
    0

    Default Re: how to insert new button in the menu as "Credits"

    Quote Originally Posted by rjagan View Post
    Hello nabil,

    where do you want to add your name..
    like menubar (help -> about) or just you want to show your name when u click the credit button...

    Note : and also please post your code by using the <code> tag... dont paste like this hereafter
    i can't see that u really want to help people who need help like me..answer once then silent forever..thank you so much rjagan!!

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: how to insert new button in the menu as "Credits"

    @OP: I merged your two threads; please don't double post.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 07-09-2010, 01:33 PM
  2. Replies: 0
    Last Post: 06-07-2010, 07:23 PM
  3. jsp insert into database error(java.lang.NumberFormatException: For input string: "")
    By cypher_girl in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-22-2009, 04:14 AM
  4. Replies: 10
    Last Post: 12-30-2008, 04:46 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •