Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Class organisation - Jbutton constructor  (Read 4302 times)
0 Members and 1 Guest are viewing this topic.
Offline hug0x

Senior Newbie





« Posted 2009-12-28 19:39:46 »

Hello,

While programming my game "Doode" I realized that the code was confusing, tangled, and I do not know how or where to start

I want to first create a constructor method that creates as many times wanted a JButton, which itself will react to user input ...

Because in fact, right now, my code was creating multiple constiste JButton "DoodeVert1", "DoodeVert2" ... and this is the wrong solution if I want to create 50 (especially the part's name, nothing should change.)

So, here is my project:

A "Jeu" class extending Jframe, implementing Mouselistener :



public class Jeu extends JFrame implements MouseListener{


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public Jeu (String titre) {             
     
                setTitle (titre);
      Container contenu2 = getContentPane();  
      contenu2.addMouseListener(this);
     
      contenu2.setLayout(null);
      Image image = Toolkit.getDefaultToolkit().createImage("DoodeImages/map5.jpg");         //background
     fond.setIcon(new ImageIcon(image));


}




And a "Doode" class wich create Jbutton everytime we call her  = Doode d = new Doode(){   
These Jbuttons have to react about user inputs.


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  
public class Doode implements Mouselistener{

public Doode(){

Jbutton Doode = new Jbutton("Doode");


public void mouseClicked(MouseEvent o) {   // quand on clique sur le fond

   }






   public void mouseEntered(MouseEvent arg0) {


   }


   public void mouseExited(MouseEvent arg0) {


   }


   public void mousePressed(MouseEvent e) {
     
      if(SwingUtilities.isRightMouseButton(e)) {
         Object source = e.getSource();
         
            }

   }






   public void mouseReleased(MouseEvent e) {
      Object source = e.getSource();  
      if ( SwingUtilities.isLeftMouseButton(e) ) {
         
         
         if (source == "Doode"){
         
system.out.print("Doode!!!");
         
}


This is for example... Is this type of system realizable? Why mine dont work! If you see something wrong, or something like that, please help me..

Im very bad with architectural developpement... please explain me what is the right way to organize classes...  Cry







}
Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2009-12-29 01:35:59 »

I think you are asking how to use the JButton class. Here is a simple example.
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  
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class ButtonTest extends JFrame implements ActionListener{

   public ButtonTest(String title) {
      super(title);
      setLayout(new FlowLayout());
      JButton button1 = new JButton("button one");
      button1.setActionCommand("button1");
      button1.addActionListener(this);
      add(button1);
      JButton button2 = new JButton("button two");
      button2.setActionCommand("button2");
      button2.addActionListener(this);
      add(button2);
      pack();
      setVisible(true);
   }
   
   public static void main(String[] args) {
      new ButtonTest("Test");
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      String command = e.getActionCommand();
      if("button1".equals(command)){
         System.out.println("button 1 pressed");
      }
      else if("button2".equals(command)){
         System.out.println("button 2 pressed");
      }
   }

}
Offline hug0x

Senior Newbie





« Reply #2 - Posted 2009-12-29 11:05:14 »

Thank you, but I know that..
 I just want to create a class constructor wich create a Jbutton everytime we call this constructor by a: Doode d = new Doode();   Tongue

I wish all the buttons created by this constructor obey to the same rules ...

I hope you understand me.



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

JGO Coder


Medals: 5



« Reply #3 - Posted 2009-12-29 15:58:40 »

well, you could just in the constructor make a JButton if that is what you are asking.

just JButton button = new JButton();

so for example
1  
2  
3  
4  
5  
6  
public Doode(JFrame frame)
{
JButton button = new JButton();
//add it to frame (cannot rememeber how off top of head)
//all your other stuff
}


I hope I could help....
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2009-12-29 17:00:50 »

I'm completely lost. If h3ckboy's post doesn't answer your question, you'll need to ask the question more clearly.

Offline hug0x

Senior Newbie





« Reply #5 - Posted 2009-12-29 21:15:17 »

Hem, nobody understood my asking...  Undecided

So, I know how to make a Jbutton, I know it, but.... imagine this jbutton:

(this is an example,,its normal if I forgot some lines)



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class Doode implements Mouselistener{


public Doode(){

Jbutton Doode = new Jbutton("Doode");

container.add("Doode");
Doode.setVisible(true);
Doode.setBounds(500,500,50,50);




   public void mouseEntered(MouseEvent p) {

object source = p.getSource;
if (source == Doode){
system.out.print("test");

   }



So, i want that everytime I call this constructor in main class with : Doode d = new Doode(); 
, it create a Jbutton, with a different name, as Doode,Doode2,Doode3,Doode4...

If you dont understand, I can give you the zip file with the project of my game.   Wink



Offline h3ckboy

JGO Coder


Medals: 5



« Reply #6 - Posted 2009-12-29 21:25:22 »

I understand you now thank you Wink.

this is where static varialbes come in.

if you dont know hwat they are then here is a short description. They are variables that are not specific to each object they are in every object and it has the same value every time.

taking your example you would do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public class Doode implements Mouselistener{
//initialize static variable
static int number = 1;
//a local copy of the number so that things dont get mixed up.
int localNumber;
public Doode(){
JButton Doode = new JButton("Doode"+number);
//syncronize static with non-static
localNumber = number;
//add one to static number so that the next one is one higher
number++;
container.add("Doode");
Doode.setVisible(true);
Doode.setBounds(500,500,50,50);




   public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Doode"+localNumber))
{
//whatever code you want when it is activated
}
   }


I added comments in the things I added, I completely changed however how you checked the button, becasue quite frankly it is very far off Wink.

hope I could help,
h3ckboy
Offline hug0x

Senior Newbie





« Reply #7 - Posted 2009-12-30 20:57:50 »

Ok, thank you guy !

Hum... anyway, you use actionPerformed with an implementation of Mouselistener ^^
 
(My code is using Mouselistener because i want to differentiate right,middle and left click....)


So, my class "jeu" should extends my superclass "Doode"... but Jeu class already extends JFrame class.... Dumb problem...
but anyway, thanks a lot !!! Grin



Love you all JGO members !   Roll Eyes
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #8 - Posted 2009-12-30 21:35:29 »

Ok, thank you guy !

Hum... anyway, you use actionPerformed with an implementation of Mouselistener ^^
 
(My code is using Mouselistener because i want to differentiate right,middle and left click....)


So, my class "jeu" should extends my superclass "Doode"... but Jeu class already extends JFrame class.... Dumb problem...
but anyway, thanks a lot !!! Grin



Love you all JGO members !   Roll Eyes

you jsut have to implement actionListener, and then do button.addActionListener(this);

glad to know I helped Smiley
Offline hug0x

Senior Newbie





« Reply #9 - Posted 2009-12-30 21:40:13 »

yes yes, thank you , i know that, but my problem is to extends JFrame and Doode class...

My doode class can be an interface?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #10 - Posted 2009-12-30 21:42:44 »

you could just have a varialbe named JFrame frame. and whenever you would have put "this" when it needed a JFrame, you would put frame.

hope I could be of assistance Smiley
Offline hug0x

Senior Newbie





« Reply #11 - Posted 2009-12-30 22:58:23 »

I dont understand where to make variable "Jframe frame;"

I dont understand how Jframe can work without extends class with, but i'll search it on google because i think you are bored about me  Undecided


Thanks you anyway, and if you are interested by my project, say me, i'll give you game project jar  Smiley
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #12 - Posted 2009-12-30 23:29:27 »

no no, I am not bored iwth you Wink.

you declare that as a variable at the top outside of any methods.

the reason you do not have to extend sit is because all of hte times you give the variable "this" you are actaulyl giving the JFrame you are extending, so if you just created one instead, then you can just give you JFrame that you have locally.

do you understand?

EDIT:
here is an example, zoto's code extended JFrame, here is the same code, but without extending JFrame.
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  
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class ButtonTest implements ActionListener{
//declare JFrame
JFrame frame;
   public ButtonTest(String title) {
//initiate the JFrame
     frame = new JFrame(title);
      frame.setLayout(new FlowLayout());
      JButton button1 = new JButton("button one");
      button1.setActionCommand("button1");
      button1.addActionListener(this);
      frame.add(button1);
      JButton button2 = new JButton("button two");
      button2.setActionCommand("button2");
      button2.addActionListener(this);
      frame.add(button2);
      frame.pack();
      frame.setVisible(true);
   }
   
   public static void main(String[] args) {
      new ButtonTest("Test");
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      String command = e.getActionCommand();
      if("button1".equals(command)){
         System.out.println("button 1 pressed");
      }
      else if("button2".equals(command)){
         System.out.println("button 2 pressed");
      }
   }

}
Offline hug0x

Senior Newbie





« Reply #13 - Posted 2009-12-30 23:41:54 »

Nice !!!


Thanks for all !

Now, my code is more clearer , and more efficient x)

p.s: Who doesn't ? hum.. good answer  Huh everybody is loving you !

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #14 - Posted 2009-12-30 23:55:22 »

Nice !!!


Thanks for all !

Now, my code is more clearer , and more efficient x)

p.s: Who doesn't ? hum.. good answer  Huh everybody is loving you !



I guess the joke was lost on you. that is fine however

glad to help Wink
Offline hug0x

Senior Newbie





« Reply #15 - Posted 2009-12-31 00:47:13 »

Don't worry, it was good joke..  Grin

But, I resolved most of problem of change "extends JFrame" by "Jframe frame; ...."

But It stay ONE problem...


setCursor(HAND_CURSOR); dont work now..

frame.setCursor(HAND_CURSOR); ? no.
container.setCursor(HAND_CURSOR); ? no.
grrr..



rectification...

eclipse show me zero red cross, zero errors, but when i launch the game it crash =(

I'll hang myself... All seems good.. Cry

I'll post all my code in few moments if I dont find the problem.. Cry



EDIT: ok, good, i resolved it, but, when I lauch my game, nothing appear, just a frame =))

I know its a container problem... so , this is my container code:

   public Jeu (){
1  
2  
3  
4  
5  
6  
7  
8  
      
      frame = new JFrame("Doode - Jeu");
      frame.setLayout(null);
     
      Container contenu2 = new Container();  
     
      contenu2.addMouseListener(this);
     

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #16 - Posted 2009-12-31 01:09:01 »

well, I dont see any point where you add the container to the frame, that could be the issue, but to be honest I havent ever used any conainers so I dont know anything beyond that.
Offline hug0x

Senior Newbie





« Reply #17 - Posted 2009-12-31 01:39:14 »

thank.. im so dumb !
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public Jeu (){
     
      JPanel contenu2;


      frame = new JFrame("Doode - Jeu");
      frame.setLayout(null);
     
      contenu2 = new JPanel();
           
      contenu2.addMouseListener(this);
     
       frame.setContentPane(contenu2);

         

Now, all is working fine thanks to you!!

You'r a great man !!! Grin
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #18 - Posted 2009-12-31 01:47:13 »

hehe np, glad to be of help.
Offline zoto

Senior Member


Medals: 4



« Reply #19 - Posted 2009-12-31 04:15:29 »

I forgot a line of code in there, you can add this to the new constructor.
1  
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

without this the program will still be running after it's closed.
Offline hug0x

Senior Newbie





« Reply #20 - Posted 2010-01-04 21:05:31 »

My class is finished!!! But dont work -___-'

It crash :S

code:

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  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;


public class DoodeVert {


   public int ClickX = 0;
   public int ClickY =0;
   public int CoorX=0;
   public int CoorY=0;
   public byte nombreDeDoodeVert=0;  
   public byte nombreDeDoodeBleu=0;
   public byte nombreDeDoodeRouge=0;
   public byte nombreDeDoodeJaune=0;
   public byte nombreEnnemis=75;
   public int nombreDeDoode=nombreDeDoodeVert+nombreDeDoodeBleu+nombreDeDoodeRouge+nombreDeDoodeJaune;
   public int doowatt=500;
   public int nourriture=500;
   public short geleeNoire=0;
   public short nombreEnnemisTues=0;
   public String pseudo;
   public Container contenu2;
   public JButton igmenu = new JButton("", new ImageIcon("DoodeImages/menu.gif"));;
   public   JLabel fond = new JLabel();
   public JComboBox choix = new JComboBox();
   public JButton doodah = new JButton("", new ImageIcon("DoodeImages/doudah.png"));
   public String Selected="rien";
   public String Selected2="rien";
   public boolean champioqp = false;
   public boolean doodahexist = false;
   public boolean btdoodah1=false;
   
   
   public JButton barre1 = new JButton("", new ImageIcon("DoodeImages/barredoode.png"));   //affichage barre doodes
  public JLabel BarreVert = new JLabel();
   public JLabel BarreBleu = new JLabel();
   public JLabel BarreRouge = new JLabel();
   public JLabel BarreJaune = new JLabel();
   
   
   public JButton BarreNourriture = new JButton("", new ImageIcon("DoodeImages/nour.png"));   //affichage barre nourriture
  public JLabel Bdoowatt = new JLabel();
   public JLabel Bnoire = new JLabel();
   public JLabel BBouffe = new JLabel();
   
   
   
   public JLabel champ1 = new JLabel(new ImageIcon("DoodeImages/champ.png"));   //affichage décor
  public JLabel champ2 = new JLabel(new ImageIcon("DoodeImages/champ.png"));
   public JLabel champ3 = new JLabel (new ImageIcon("DoodeImages/champ2.png"));
   public JButton rive = new JButton("", new ImageIcon("DoodeImages/riviere.gif"));
   public JButton arbre = new JButton ("", new ImageIcon ("DoodeImages/arbre.gif"));
   public JLabel arbre2 = new JLabel (new ImageIcon ("DoodeImages/arbre2.gif"));
   
   
   
   public JButton DoodeVert = new JButton ("",new ImageIcon("DoodeImages/vert.gif"));  //doode vert
  public JButton DoodeVert2 = new JButton ("",new ImageIcon("DoodeImages/vert.gif"));

   
   
   
   
   public JButton CreerDoodah = new JButton ("",new ImageIcon("DoodeImages/btdd.png"));



   Image saut = Toolkit.getDefaultToolkit().createImage("DoodeImages/2 - Copie.png");
   Image normal = Toolkit.getDefaultToolkit().createImage("DoodeImages/vert.gif");



public DoodeVert(){

System.out.print("doode crée");




public void mouseReleased(MouseEvent e) {
   Object source = e.getSource();  

   if(SwingUtilities.isRightMouseButton(e)) {
     
   
      CoorX=e.getX();
       CoorY=e.getY();
       
       
   
     
       if(source == DoodeVert & doodahexist==false){
             
             Selected2="DoodeVert";
         
       }
       
       
       
       
       
       
       
     
       
       
       
     
         
      if (source == fond & Selected == "DoodeVert"){
          DoodeVert.setBounds(CoorX-15,CoorY-25,52,65);
          DoodeVert.setIcon(new ImageIcon(normal));
          Selected2="rien";
          }
         
         
         
          if (source == champ1 & Selected == "DoodeVert"){
             
             DoodeVert.setBounds(1025,308,105,128);
             DoodeVert.setIcon(new ImageIcon(saut));
             Selected2="champ1";
             
          }
         
              if (source == champ2 & Selected == "DoodeVert"){
                   
                   DoodeVert.setBounds(425,555,105,128);
                   DoodeVert.setIcon(new ImageIcon(saut));
                   Selected2="champ2";
               
             
          }
             
             
              if (source == champ3 & Selected == "DoodeVert"){
                   
                   DoodeVert.setBounds(943,270,105,128);
                   DoodeVert.setIcon(new ImageIcon(saut));
                   Selected2="champ3";
                   
               
             
          }
             
              if (source == rive & Selected == "DoodeVert"){
                 DoodeVert.setIcon(new ImageIcon(normal));
                   DoodeVert.setBounds(140,CoorY,52,65);
                   Selected2="rive";
     
             
              }
              if (source == fond & Selected == "DoodeVert2"){
                   DoodeVert2.setBounds(CoorX-15,CoorY-25,52,65);
                   DoodeVert2.setIcon(new ImageIcon(normal));
                   Selected2="rien";
                   }
                   
                   
                   
                   if (source == champ1 & Selected == "DoodeVert2"){
                     
                      DoodeVert2.setBounds(1025,308,105,128);
                      DoodeVert2.setIcon(new ImageIcon(saut));
                      Selected2="champ1";
                     
                   }
                   
                       if (source == champ2 & Selected == "DoodeVert2"){
                           
                            DoodeVert2.setBounds(425,555,105,128);
                            DoodeVert2.setIcon(new ImageIcon(saut));
                            Selected2="champ2";
                       
                     
                   }
                       
                       
                       if (source == champ3 & Selected == "DoodeVert2"){
                           
                            DoodeVert2.setBounds(943,270,105,128);
                            DoodeVert2.setIcon(new ImageIcon(saut));
                            Selected2="champ3";
                       
                     
                   }
                       
                       if (source == rive & Selected == "DoodeVert2"){
                          DoodeVert2.setIcon(new ImageIcon(normal));
                            DoodeVert2.setBounds(140,CoorY,52,65);
                            Selected2="rive";
                           
                           
                            }
               
                     }}}


public void go(){
   BarreVert.setText(""+nombreDeDoodeVert);
   BarreBleu.setText(""+nombreDeDoodeBleu);
   BarreRouge.setText(""+nombreDeDoodeRouge);
   BarreJaune.setText(""+nombreDeDoodeJaune);
   BBouffe.setText(""+nourriture);
   Bdoowatt.setText(""+doowatt);
   Bnoire.setText(""+geleeNoire);

}


                         class affiche implements Runnable{




                           
                            public void run() {      // la runnable qui permet tout !

                               go();  

                            }}}


   
Offline zoto

Senior Member


Medals: 4



« Reply #21 - Posted 2010-01-04 21:52:54 »

Can you post the error?
Offline hug0x

Senior Newbie





« Reply #22 - Posted 2010-01-04 22:22:55 »

No problem..

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  
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
   void is an invalid type for the variable mouseReleased
   Syntax error on token "(", ; expected
   Syntax error on token ")", ; expected

   at DoodeVert.<init>(DoodeVert.java:89)
   at Jeu.<init>(Jeu.java:23)
   at Menu.actionPerformed(Menu.java:85)
   at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
   at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
   at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
   at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
   at java.awt.Component.processMouseEvent(Unknown Source)
   at javax.swing.JComponent.processMouseEvent(Unknown Source)
   at java.awt.Component.processEvent(Unknown Source)
   at java.awt.Container.processEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
   at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
   at java.awt.Container.dispatchEventImpl(Unknown Source)
   at java.awt.Window.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #23 - Posted 2010-01-04 22:59:08 »

you forgot the close the constructor aka public Doode()
Offline hug0x

Senior Newbie





« Reply #24 - Posted 2010-01-05 19:17:11 »

Yes, thank you!!

But I can't add my "}" after my "public void mouseReleased(MouseEvent e) { "

This error appear: "void is an invalid type for the variable mouseReleased"

...


So, I can close my class just before it, but it's dumb because JButtons won't react to user's input.
Offline zoto

Senior Member


Medals: 4



« Reply #25 - Posted 2010-01-05 21:03:34 »

I'm not sure what editor you use but if its eclipse you can select all(ctrl-a) and the fix indentation(ctrl-I) and it will greatly increase readability.
Heres your code with the above mentioned problem fixed:
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  
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;


public class DoodeVert {
   public int ClickX = 0;
   public int ClickY =0;
   public int CoorX=0;
   public int CoorY=0;
   public byte nombreDeDoodeVert=0;  
   public byte nombreDeDoodeBleu=0;
   public byte nombreDeDoodeRouge=0;
   public byte nombreDeDoodeJaune=0;
   public byte nombreEnnemis=75;
   public int nombreDeDoode=nombreDeDoodeVert+nombreDeDoodeBleu+nombreDeDoodeRouge+nombreDeDoodeJaune;
   public int doowatt=500;
   public int nourriture=500;
   public short geleeNoire=0;
   public short nombreEnnemisTues=0;
   public String pseudo;
   public Container contenu2;
   public JButton igmenu = new JButton("", new ImageIcon("DoodeImages/menu.gif"));;
   public   JLabel fond = new JLabel();
   public JComboBox choix = new JComboBox();
   public JButton doodah = new JButton("", new ImageIcon("DoodeImages/doudah.png"));
   public String Selected="rien";
   public String Selected2="rien";
   public boolean champioqp = false;
   public boolean doodahexist = false;
   public boolean btdoodah1=false;


   public JButton barre1 = new JButton("", new ImageIcon("DoodeImages/barredoode.png"));   //affichage barre doodes
  public JLabel BarreVert = new JLabel();
   public JLabel BarreBleu = new JLabel();
   public JLabel BarreRouge = new JLabel();
   public JLabel BarreJaune = new JLabel();
   public JButton BarreNourriture = new JButton("", new ImageIcon("DoodeImages/nour.png"));   //affichage barre nourriture
  public JLabel Bdoowatt = new JLabel();
   public JLabel Bnoire = new JLabel();
   public JLabel BBouffe = new JLabel();
   public JLabel champ1 = new JLabel(new ImageIcon("DoodeImages/champ.png"));   //affichage décor
  public JLabel champ2 = new JLabel(new ImageIcon("DoodeImages/champ.png"));
   public JLabel champ3 = new JLabel (new ImageIcon("DoodeImages/champ2.png"));
   public JButton rive = new JButton("", new ImageIcon("DoodeImages/riviere.gif"));
   public JButton arbre = new JButton ("", new ImageIcon ("DoodeImages/arbre.gif"));
   public JLabel arbre2 = new JLabel (new ImageIcon ("DoodeImages/arbre2.gif"));
   public JButton DoodeVert = new JButton ("",new ImageIcon("DoodeImages/vert.gif"));  //doode vert
  public JButton DoodeVert2 = new JButton ("",new ImageIcon("DoodeImages/vert.gif"));
   public JButton CreerDoodah = new JButton ("",new ImageIcon("DoodeImages/btdd.png"));
   Image saut = Toolkit.getDefaultToolkit().createImage("DoodeImages/2 - Copie.png");
   Image normal = Toolkit.getDefaultToolkit().createImage("DoodeImages/vert.gif");



   public DoodeVert(){
      System.out.print("doode crée");
   }

   public void mouseReleased(MouseEvent e) {
      Object source = e.getSource();  

      if(SwingUtilities.isRightMouseButton(e)) {
         CoorX=e.getX();
         CoorY=e.getY();

         if(source == DoodeVert & doodahexist==false){
            Selected2="DoodeVert";
         }
         if (source == fond & Selected == "DoodeVert"){
            DoodeVert.setBounds(CoorX-15,CoorY-25,52,65);
            DoodeVert.setIcon(new ImageIcon(normal));
            Selected2="rien";
         }

         if (source == champ1 & Selected == "DoodeVert"){

            DoodeVert.setBounds(1025,308,105,128);
            DoodeVert.setIcon(new ImageIcon(saut));
            Selected2="champ1";

         }
         if (source == champ2 & Selected == "DoodeVert"){

            DoodeVert.setBounds(425,555,105,128);
            DoodeVert.setIcon(new ImageIcon(saut));
            Selected2="champ2";


         }
         if (source == champ3 & Selected == "DoodeVert"){

            DoodeVert.setBounds(943,270,105,128);
            DoodeVert.setIcon(new ImageIcon(saut));
            Selected2="champ3";
         }

         if (source == rive & Selected == "DoodeVert"){
            DoodeVert.setIcon(new ImageIcon(normal));
            DoodeVert.setBounds(140,CoorY,52,65);
            Selected2="rive";


         }
         if (source == fond & Selected == "DoodeVert2"){
            DoodeVert2.setBounds(CoorX-15,CoorY-25,52,65);
            DoodeVert2.setIcon(new ImageIcon(normal));
            Selected2="rien";
         }

         if (source == champ1 & Selected == "DoodeVert2"){

            DoodeVert2.setBounds(1025,308,105,128);
            DoodeVert2.setIcon(new ImageIcon(saut));
            Selected2="champ1";

         }

         if (source == champ2 & Selected == "DoodeVert2"){

            DoodeVert2.setBounds(425,555,105,128);
            DoodeVert2.setIcon(new ImageIcon(saut));
            Selected2="champ2";
         }

         if (source == champ3 & Selected == "DoodeVert2"){

            DoodeVert2.setBounds(943,270,105,128);
            DoodeVert2.setIcon(new ImageIcon(saut));
            Selected2="champ3";
         }

         if (source == rive & Selected == "DoodeVert2"){
            DoodeVert2.setIcon(new ImageIcon(normal));
            DoodeVert2.setBounds(140,CoorY,52,65);
            Selected2="rive";
         }
      }
   }


   public void go(){
      BarreVert.setText(""+nombreDeDoodeVert);
      BarreBleu.setText(""+nombreDeDoodeBleu);
      BarreRouge.setText(""+nombreDeDoodeRouge);
      BarreJaune.setText(""+nombreDeDoodeJaune);
      BBouffe.setText(""+nourriture);
      Bdoowatt.setText(""+doowatt);
      Bnoire.setText(""+geleeNoire);

   }


   class affiche implements Runnable{
      public void run() {      // la runnable qui permet tout !
        go();  
      }
   }
}

Also note the single & is a bitwise AND and the && is a logical AND, you might want to change that.
Offline hug0x

Senior Newbie





« Reply #26 - Posted 2010-01-11 20:01:31 »

Pffff, i'll never solve it..  Undecided


Here is an exemple:

class DoodeVert:


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  
public class DoodeVert implements MouseListener{



public int CoorX=0;
public int CoorY=0;
public Container contenu2;
public JButton DoodeVert = new JButton ("",new ImageIcon("DoodeImages/vert.gif"));



public DoodeVert(){
      System.out.print("doode crée ");
      contenu2.add(DoodeVerta);
      DoodeVerta.setBounds(15,15,650,75);
      DoodeVerta.setFocusPainted(false);
      DoodeVerta.setBorderPainted(false);                  
      DoodeVerta.setContentAreaFilled(false);
      DoodeVerta.addMouseListener(this);


public void mouseReleased(MouseEvent f) {
      Object source = f.getSource();  

      if(SwingUtilities.isRightMouseButton(f)) {
         CoorX=f.getX();
         CoorY=f.getY(); }
}
}}


class jeu:


public class Jeu extends DoodeVert implements MouseListener, ActionListener{


   JFrame frame;


   public Jeu (){
      
      JPanel contenu2;


      frame = new JFrame("Doode - Jeu");
      frame.setLayout(null);
      
      contenu2 = new JPanel();
            
      contenu2.addMouseListener(this);
      
       frame.setContentPane(contenu2);





      contenu2.setLayout(null);
      Image image = Toolkit.getDefaultToolkit().createImage("DoodeImages/map5.jpg");// nouveau JLabel backgound
      fond.setIcon(new ImageIcon(image));

      
      
      DoodeVert DoodeVert = new DoodeVert();
DoodeVert DoodeVert2 = new DoodeVert();

 }}
      



Please explain me what is wrong and what I have to fix. Cry

If you think I should have to use another library, say it to me.. (but only core please =)   )


Thank you...
Offline zoto

Senior Member


Medals: 4



« Reply #27 - Posted 2010-01-12 00:12:18 »

Move one of the curly braces from the last line up to the line before mouseReleased.
A few tricks give each { a value of +1 and each } a value of -1, add together each one you come across, if you have any total besides 0 there is a problem with the number of braces.  That only helps you see if there is the right amount, as there is in your example, next would be to look for places you expect to see a curly brace(like the end of functions) and make sure they are in the correct spot.
Pages: [1]
  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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (23 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
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!