Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: need help in changing button and highscore

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help in changing button and highscore

    hello guys..i'm new here and also in java world..i need you guys help..

    me and my friend are building a java snake game for our uni final project..
    everything is going well..just now,our supervisor have ask us to change the look of our button for every menu..he want it to be look as 3D button..so i want to know how should i do it??can i call an image and put it at the same place with the already button???


    another problem that we faced was,we need to add something to our game,which we can save the score after playing the game..but we want to do it without using database,just using a small amount of textfile..but the problem is we just dont know how to do it..we just use one class to do everthing because we dont know how to connect many class..this is our coding,hope somebody can help us..


    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;
    }
     
     
    }

    kind regards-NABIL


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help in changing button and highscore

    First of all, that's waaay too much code. Remember- we're doing this for free, in our spare time, so you should make it as easy as possible for people to help you. For example, you have two questions here- one about setting the look and feel of a button, another about writing to text file. There is no need for any code involving game logic, game painting, etc. Ideally, you'd create two SSCCEs that demonstrated exactly what you've tried for each problem.

    For the question about buttons, can't you just set the border of the button? The background, etc?

    And as for writing to text file, this is the first result for googling "java file io": Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help in changing button and highscore

    Quote Originally Posted by KevinWorkman View Post
    First of all, that's waaay too much code. Remember- we're doing this for free, in our spare time, so you should make it as easy as possible for people to help you. For example, you have two questions here- one about setting the look and feel of a button, another about writing to text file. There is no need for any code involving game logic, game painting, etc. Ideally, you'd create two SSCCEs that demonstrated exactly what you've tried for each problem.

    For the question about buttons, can't you just set the border of the button? The background, etc?

    And as for writing to text file, this is the first result for googling "java file io": Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

    hey,thanks for your reply..

    i'm really sorry that we have made it difficult for you..and i know that this coding look so mess...but we have no choice now..we are new in java..and now we just have a few days before submit this game..

    about button that u said,about set the border of the button..what do u mean acctually??i can't really understand..how to make the border to the button??what should i use??


    and about the link for basic I/O that u gave me justnow,at which part should i look at??we really dont know how to make that 'save the score' that we need...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help in changing button and highscore

    Like I said, break your problem down into smaller pieces. Create an SSCCE for each piece.

    Consult the API for functions that will let you set the border: Java Platform SE 6

    You should read that whole tutorial, focusing on the lessons on file io.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help in changing button and highscore

    ok first of all..

    1)how to make a 'save the highscore'?

    2)can i do it in the same class with the snake?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: need help in changing button and highscore

    If you execute your program as an applet, you will have to give the applet permission to write to and read from the local PC's disk.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: need help in changing button and highscore

    Quote Originally Posted by nabil.elpistolero View Post
    1)how to make a 'save the highscore'?
    Which part of this is giving you trouble? Creating a JButton? Saving to file? Something else? Create an SSCCE that demonstrates exactly where you're stuck.

    Quote Originally Posted by nabil.elpistolero View Post
    2)can i do it in the same class with the snake?
    What happened when you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Changing content
    By Aivaras in forum Java Servlet
    Replies: 2
    Last Post: March 16th, 2012, 11:43 AM
  2. Changing from a screen to another!
    By gargamel7 in forum Java Theory & Questions
    Replies: 23
    Last Post: October 10th, 2011, 06:27 AM
  3. Changing my GUI help
    By Xrrak in forum AWT / Java Swing
    Replies: 13
    Last Post: August 10th, 2011, 03:11 PM
  4. changing GUI
    By timmin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 3rd, 2011, 08:16 AM
  5. Boolean Value Not Changing
    By bosox960 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 21st, 2010, 02:11 PM