Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  creating a restart button help?  (Read 8940 times)
0 Members and 1 Guest are viewing this topic.
Offline ganaschka

Senior Newbie





« Posted 2012-07-22 16:02:55 »

how to create a restart button on fruit puzzle game ? Anyone help me on this. I would deeply appreciate it if it was helped...
Offline jonjava
« Reply #1 - Posted 2012-07-22 18:18:27 »

Do you mean how to actually make a "button"? A button that you can click and stuff happens? Or do you mean how do you set the puzzle game at the beginning? or both?

For the button, you could use swing (javax.swing.JButton) or make your own and not use swing.

For the second, isn't it as simple as clearing the level of all fruit and setting the score to 0?

You're question is a bit vague so I can't do any better but answer vaguely :L

If you need more help, please try to be a bit more specific Smiley

Offline ganaschka

Senior Newbie





« Reply #2 - Posted 2012-07-22 18:38:42 »

ok I have decided to show the game source here since unclear question that would be able to understandable in order to figure out with RESTART METHOD ! please follow up with the source below and tell me what this need to do or write a restart method with explanation if possible

BEst regards,

looking forward for reply
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #3 - Posted 2012-07-22 19:33:17 »

D:

Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #4 - Posted 2012-07-22 19:51:21 »

ganaschka, instead of making all your code italic font, do this:

1. Edit your last post
2. Click the # button, just underneath "Change color" in the editor
3. You'll get these tags (code) (/code) (with [ and ] instead of ( and ) )
4. Copy all your code, and paste it between those two tags, like this:
(code)
ALL MY CODE
(/code)

which, when using [ and ] instead, will end up looking like this:
1  
ALL MY CODE


This will make it a LOT easier for everyone to read.

- Jonas
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #5 - Posted 2012-07-22 20:00:11 »

Please don't do that, USE PASTEBIN instead Smiley

Short code => do what Ultoman said
Long code => Pastebin Smiley

Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #6 - Posted 2012-07-22 20:04:23 »

Please don't do that, USE PASTEBIN instead Smiley

Short code => do what Ultoman said
Long code => Pastebin Smiley
Oh, sorry! Do what _Al3x said! I'm just so used to only posting relevant code Smiley This instance, however, requires your full source to know what's going on.

- Jonas
Offline ReBirth
« Reply #7 - Posted 2012-07-23 05:39:29 »

By restarting method you mean just reset all fields that affect your current state to their initial values.

Offline ganaschka

Senior Newbie





« Reply #8 - Posted 2012-07-23 06:06:15 »

Yes, rebirth thats exactly I need to do on the game
Offline ReBirth
« Reply #9 - Posted 2012-07-23 06:13:39 »

Store your initials values in a constant, so basically you have two fields for one "thing". One the constant, one normal variable that you modify on run.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ganaschka

Senior Newbie





« Reply #10 - Posted 2012-07-23 06:16:00 »

Ultroman, thanks for your advice.  could you please edit my last post with sources in follow up with you steps ?
Offline ReBirth
« Reply #11 - Posted 2012-07-23 06:18:25 »

Ultroman is going to fight a monster right now. The only one except mods that can edit your post is yourself.

Offline ganaschka

Senior Newbie





« Reply #12 - Posted 2012-07-23 06:20:30 »

Rebirth, could give me an example of storing initial values and concepts? that would be awsome
thanks
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #13 - Posted 2012-07-23 06:25:06 »

Wow. This is just.. wow.

For humanity's sake, I hope you're just trolling us.

Anyways.. resetting data is very simple. It mostly varies depending on the way you store your data. For instance, if I want to have a String that holds the user's name, but if he clicks "reset" it changed the name back to "DEFAULT" then I would do this

1  
2  
3  
4  
5  
6  
private final String DEFAUL_NAME = "DEFAULT";
private String name = DEFAULT_NAME;

public void resetName() {
     name = DEFAULT_NAME;
}


Do you understand now?

 | Nathan

Check out my website!
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #14 - Posted 2012-07-23 06:28:09 »

Stop posting so much code!!

Dude. No one is going to write your code for you. Everyone here is giving you great advice on how to learn about how to fix this issue of yours.

LISTEN TO IT.

Read my previous post again if you need to.

 | Nathan

Check out my website!
Offline ganaschka

Senior Newbie





« Reply #15 - Posted 2012-07-23 06:34:18 »

by the way, Yes I understood Nathan great advice for issue of mine
Offline ReBirth
« Reply #16 - Posted 2012-07-23 06:35:35 »

I'll put it simple:
- separate asset making code from constructor to normal method, said "void init()". Anything regarded to GUI constructing should stay.
- on the same constructor, call reset() to replace the loose
- now anytime you want to restart, call the reset()

If by any chance you failed this step, without insult please consider to grab some basic tutorials.

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #17 - Posted 2012-07-23 09:29:08 »

ganaschka please don't get us wrong, we WANT to help you, really! If you have more doubts, try to ask them as detailed as possible, you have learned how to use [.code] and [./code] (remove the dot "." to make it work) tags for showing short code, like Nathan did. You've learned how to use pastebin, or at least that it existed, and that it's the BEST tool to share LONG code. And you've also learned how to use the final keyword to define constants to help you make your reset.

As you can see, we want to help you, so PLEASE don't hesitate to ask again, we are all your friends here Smiley

Offline ganaschka

Senior Newbie





« Reply #18 - Posted 2012-07-23 14:37:39 »

I need to figure out with following code below. when press a restart button, it goes set default and reinitialize game again. HELP ME...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
 menuBar = new JMenuBar();
            menu = new JMenu("Restart");
            menu.setMnemonic(1);
            menuBar.add(menu);
            menu.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                            if(e.getSource() == menu){
                             
                                allImages.clear();
                                 for(int i=0; i<images.size(); i++){
                                    allImages.add(images.get(i));
                                }
                                   for(int i=0; i<images.size(); i++){
                                     allImages.add(images.get(i));
                             }
                                 for(int i=0; i<iconNames.length*2; i++){
                                     ImageIcon randIcon = randomIcon();
                                        Button b = (Button)tovchVec.get(i);
                                        b.setZurag(randIcon);
                                        b.setEnabled(true);
                                }
                            }          
                        }});
       



Offline Sickan

Senior Member


Medals: 9



« Reply #19 - Posted 2012-07-23 14:41:32 »

ganaschka, please don't assume that people are going to write your code for you. If you cannot do some debugging yourself, maybe you should learn more about programming before you continue with your game.
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #20 - Posted 2012-07-23 14:43:50 »

1- Do you use Eclipse? Try "Ctrl + Shift + F" Smiley
2- I don't know much about swing, but why don't you just do something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
menuBar = new JMenuBar();
            menu = new JMenu("Restart");
            menu.setMnemonic(1);
            menuBar.add(menu);
            menu.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                            if(e.getSource() == menu){
                             game.init();
                            });


Where game.init(); is the method you call when the game FIRST starts.
You can play with
1  
game.init(int level)
where with "int level" you say what level you want to restart. Maybe something like
1  
game.init(game.currentLevel)

Mind you give us ALL your code in PASTEBIN? Smiley

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #21 - Posted 2012-07-23 14:49:45 »

1- Do you use Eclipse? Try "Ctrl + Shift + F" Smiley
2- I don't know much about swing, but why don't you just do something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
menuBar = new JMenuBar();
            menu = new JMenu("Restart");
            menu.setMnemonic(1);
            menuBar.add(menu);
            menu.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                            if(e.getSource() == menu){
                             game.init();
                            });


Where game.init(); is the method you call when the game FIRST starts.
Mind you give us ALL your code in PASTEBIN? Smiley
Won't the game already be running when you call init()?  Not to mention it's more efficient to just reset values (wait...nvm, shouldn't mention efficiency Tongue)
If there's anything involving GUI in the init() method, it might get a bit screwed up. (e.g. add a component twice, etc.)
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #22 - Posted 2012-07-23 14:57:20 »

Yup you're right, but who knows what's in there! Smiley
Maybe game.startLevel(int level) is safer.
Idk, I'd look at the whole code to give a better advice. (IN PASTEBIN Smiley )

Offline ganaschka

Senior Newbie





« Reply #23 - Posted 2012-07-23 14:57:44 »

problem is that how restore initial values in init() method ? I'm asking this kind of issue !
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #24 - Posted 2012-07-23 14:58:17 »

Post your code using pastebin. Smiley

Offline ganaschka

Senior Newbie





« Reply #25 - Posted 2012-07-23 15:07:27 »

OK everyone !? I have decided to post whole code since my question was a bit of vague. So please be patient to look at whole code below and to find out best way to handle this issue for this tiny little game(yes, I called it game because im beginner)
In addition I want to create a new MENU class that contains start, record etc, if you  have more time, please consider this MENU class as well.

best regards,
ganaschka 

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
package puzzle;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;
/**
 *
 * @author Ganaa
 */

public class Puzzle extends JFrame implements Runnable{

    private String iconNames[] = {"D:/images/1.png", "D:/images/2.png", "D:/images/3.png", "D:/images/4.png"
                                    , "D:/images/5.png", "D:/images/6.png"};
    private Vector images = new Vector();
    private JPanel centerPanel;
    private JLabel topLbl;
    private Vector allImages = new Vector();
    private Vector tovchVec;
    Button lastTovch = null;
    private Thread th;
    private int min=0, sec=0;
    boolean game = false;
    boolean running = false;
    JMenuBar menuBar;//цэс гаргах хэсэг
   JMenu menu;
    private JTextField score;// оноо гаргах хэсэг
   private int onoo = 0;
   

    public Puzzle(){
        super("Fruit Puzzle");
        Container con = getContentPane();
        con.setLayout(new BorderLayout());

        tovchVec = new Vector();

        centerPanel = new JPanel();
        topLbl = new JLabel("0:0", SwingUtilities.CENTER);
        topLbl.setBackground(new Color(255, 225, 169));
        topLbl.setOpaque(true);

        for(int i=0; i<iconNames.length; i++){
            images.add(new ImageIcon(iconNames[i]));
        }

        for(int i=0; i<images.size(); i++){
            allImages.add(images.get(i));
        }
        for(int i=0; i<images.size(); i++){
            allImages.add(images.get(i));
        }

        MyHandler handler = new MyHandler();

        for(int i=0; i<iconNames.length*2; i++){
            ImageIcon randIcon = randomIcon();
            final Button t = new Button(randIcon);
//            t.addActionListener(handler);
           t.addMouseListener(handler);
            tovchVec.add(t);
            centerPanel.add(t);            
            // menu bar
           menuBar = new JMenuBar();
            menu = new JMenu("Restart");
            menu.setMnemonic(1);
            menuBar.add(menu);
            menu.addActionListener(new ActionListener(){// I was trying on this but doesnt work :( yeah I need init()
           public void actionPerformed(ActionEvent e){
                            if(e.getSource() == menu){
                             
                                allImages.clear();
                                 for(int i=0; i<images.size(); i++){
                                    allImages.add(images.get(i));
                                }
                                   for(int i=0; i<images.size(); i++){
                                     allImages.add(images.get(i));
                             }
                                 for(int i=0; i<iconNames.length*2; i++){
                                     ImageIcon randIcon = randomIcon();
                                        Button b = (Button)tovchVec.get(i);
                                        b.setZurag(randIcon);
                                        b.setEnabled(true);
                                }
                            }          
                        }});
       
        setJMenuBar(menuBar);//JFrame dr nemj bn
     
        //score garah heseg
       score = new JTextField();
        score.setEditable(false);
        score.setFont(new Font("Serif",Font.BOLD,12));
        score.setBackground(new Color(255, 225, 169));
        con.add(score,BorderLayout.SOUTH);


        }

        con.add(centerPanel, BorderLayout.CENTER);
        con.add(topLbl, BorderLayout.NORTH);

        setLocation(500,50);
        setSize(530,635);
        setResizable(false);
        setVisible(true);

        th = new Thread(this);
        th.start();
        running = true;
    }
    // reset()
   

    public ImageIcon randomIcon(){

        int r = (int)(Math.random() * allImages.size());
        ImageIcon icon = (ImageIcon)allImages.get(r);

        allImages.remove(r);

        return icon;
    }

    public static void main(String[] args) {
        Puzzle game = new Puzzle();
        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // Thread run
   public void run() {
        while(running){
            try {
                Thread.sleep(1000);
                sec++;
                if(sec>59)
                {
                    min++;
                    sec = 0;
                }
                topLbl.setText(min+":"+sec);

                for(int i=0; i<tovchVec.size(); i++)
                {
                    Button t = (Button)tovchVec.get(i);
                    if(t.isEnabled()){
                        game = true;
                        break;
                    }

                }
                if(game == false){
                    running = false;
                    JOptionPane.showMessageDialog(Puzzle.this, "Баяр хүргье. Та "+min+" минут "+sec+" секундэд амжилттай гүйцэтгэлээ.");
                    th.stop();
                }
                game = false;
            } catch (InterruptedException ex) {
            }
        }
    }

    class MyHandler implements MouseListener{

        public void mouseClicked(MouseEvent e) {

            if( e.getClickCount() == 1)
            {
                for (int i = 0; i < tovchVec.size(); i++) {
                Button firstTovch = (Button)tovchVec.get(i);
                if(e.getSource() == firstTovch){
                    firstTovch.setNuuh(false);
                    if(lastTovch != null){
                        if(firstTovch.getHashCode() == lastTovch.getHashCode() && firstTovch.hashCode() != lastTovch.hashCode()){
                            firstTovch.removeMouseListener(this);
                            lastTovch.removeMouseListener(this);
                            ++onoo;
                            score.setText("\t\t   "+onoo+" "+"зураг амжилттай оллоо");
                            if(onoo ==6){
                                score.setText("\t\tбүх ижил зурагнуудыг амжилттай оллоо !");
                            }
                            firstTovch.setNuuh(false);
                            lastTovch.setNuuh(false);
                            firstTovch.setBackground(new Color(253, 195, 115));
                            lastTovch.setBackground(new Color(253, 195, 115));
                            firstTovch.setEnabled(false);
                            lastTovch.setEnabled(false);
                        }
                    }
                    lastTovch = firstTovch;

                }
                }
            }
            else return;
        }

        public void mousePressed(MouseEvent e) {
//            throw new UnsupportedOperationException("Not supported yet.");
       }

        public void mouseReleased(MouseEvent e) {
//            throw new UnsupportedOperationException("Not supported yet.");
       }

        public void mouseEntered(MouseEvent e) {
//            throw new UnsupportedOperationException("Not supported yet.");
       }

        public void mouseExited(MouseEvent e) {
             for (int i = 0; i < tovchVec.size(); i++) {
                Button tovch = (Button)tovchVec.get(i);
                if(e.getSource() == tovch){
                    tovch.setNuuh(true);
                 }
             }
        }
    }
}
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #26 - Posted 2012-07-23 16:25:28 »

Post your code using pastebin. Smiley
Post your code using pastebin. Smiley
Post your code using pastebin. Smiley
Post your code using pastebin. Smiley
Post your code using pastebin. Smiley
Post your code using pastebin. Smiley
Post your code using pastebin. Smiley

http://pastebin.java-gaming.org/1c5650f7519

 | Nathan

Check out my website!
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #27 - Posted 2012-07-23 16:29:06 »

I got a strange feeling that I've been quoted... Mhmmm nah, I don't think so Smiley

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #28 - Posted 2012-07-23 16:31:51 »

1  
2  
3  
4  
5  
6  
7  
8  
try
      {
         this.countHowMany("pastebin");
      }
      catch (Exception e)
      {
         System.out.println("I'S OVER 9000!");
      }


A little joke Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #29 - Posted 2012-07-23 16:51:46 »

1  
2  
3  
4  
5  
6  
7  
8  
try
      {
         this.countHowMany("pastebin");
      }
      catch (Exception e)
      {
         System.out.println("I'S OVER 9000!");
      }


A little joke Smiley

I prefer

1  
2  
3  
4  
5  
      try {
         countHowMany("PasteBin");
      } catch (Exception e) {
         System.out.println("IT'S OVER 9000!");
      }

Check out my website!
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!