
-----------------------------------
AzureFire
Thu May 17, 2007 8:40 pm

Method as a parameter
-----------------------------------
Hey guys, I have sort of a strange problem that I don't even know how to begin on solving.

I have made my own GUI system for my final project, and I want each button to run a certain function when hit, ideally, each button will refer to a different function. I have all the hit detection and all that, I just need help with how to tell the object which function it's supposed to run.

Currently, I have a large if structure that's very messy and doesn't like to be very flexable.

Here's how my class is designed;

Class: InterfaceButton
-Methods:
-- InterfaceButton() : sets all the variables to default
-- assign(Image imgSrc, int x, int y) : assigns the image to be used as a button as well as it's X and Y ((This is where I would like to put the reference to a function))
-- draw(Graphics g) : draws the button, if not disabled
-- isOver(int mouseX, int mouseY) : detects whether the mouse is over top of the button

Just to clarify, I would like to add a parameter to assign() that refers to a function that should be run when isOver() returns true.

eg. buttonDoThisAction());

If that's not possible, could I do something along the lines of

buttonDoThisAction();

like in Flash?


Thanks for any help :)
P.S. I'm running Ready To Program Java (I think it's 1.7 at school, but I only have 1.0 at home) and I'm restricted to the default packages (eg. java.awt)

-----------------------------------
Luny
Thu May 17, 2007 9:16 pm

RE:Method as a parameter
-----------------------------------
1.7 was not released yet. 1.6 just came over in October. It would make life a lot easier if you download at least Java 1.4! The javax.swing package and the java.awt packages are essential to GUI programs!

-----------------------------------
wtd
Thu May 17, 2007 10:01 pm

RE:Method as a parameter
-----------------------------------
Check out the ActionListener interfaces.  If you don't know anything about interfaces... learn now!

-----------------------------------
AzureFire
Thu May 17, 2007 10:05 pm

Re: Method as a parameter
-----------------------------------
Ah yes, it's probably 1.6, I've never really checked :P

However, I've already tried to use swing at school and it tells me that it can't find the package, so that's not an option. I'm not making a Windows(or Java, I suppose would be more correct) style GUI anyway, the program is actually a World of Warcraft based game, so the buttons look like that.

If what I'm asking is really off, then does anyone have a good suggestion for how I should go about it?

-----------------------------------
Luny
Thu May 17, 2007 10:30 pm

RE:Method as a parameter
-----------------------------------
Personally, I think you are reinventing the wheel. Why don't you look up the classes before you use them that way you don't have any error or compiler messages saying "not found" or whatever. That's my only suggestion.

-----------------------------------
OneOffDriveByPoster
Thu May 17, 2007 11:46 pm

Re: Method as a parameter
-----------------------------------
eg. buttonDoThisAction());

If that's not possible, could I do something along the lines of
buttonDoThisAction();
Make an interface that specifies the form of your "DoThisAction" method.  Pass in objects of classes that implement that interface.
This is what Java does with all of its Listener interfaces.

-----------------------------------
AzureFire
Fri May 18, 2007 4:58 pm

Re: Method as a parameter
-----------------------------------
Check out the ActionListener interfaces.  If you don't know anything about interfaces... learn now!

Perfect, thanks so much!

+karma

P.S. Sorry, I didn't see that yesterday 'till after I posted my second message.

-----------------------------------
cedricb
Mon May 21, 2007 1:58 pm

Re: Method as a parameter
-----------------------------------
Here's in my opinion the best way to add multiple action listeners in one GUI

where the is your button put:

ActionListener uniqueActionName = new UniqueActionName();
yourButton.addActionListener( uniqueActionName );


Now you want to create a new private class for that specific action listener

private class UniqueActionName implements ActionListener {

      public void actionPerformed( ActionEvent evenement )
      {
         //you action here
      }
}


This is how I made the GUI for my chat program and it's the best if you have a lot of action to do
I'd suggest reading more on actions
