question about custom buttons

Not really custom, more like your own. I was wondering whats everyone’s opinion on the proper way to make your own buttons. If I have a few buttons fixed on the screen at certain screen coords, then what is the best way to detect a button press. For now i just have a mouseClicked method that checks to see if the mouse was clicked in the region of the button. So if I have a 100x100 button at 50,600 xy coords I check if the click took place between 50 and 150 x, and 600 and 700 y. Is there a more proffesional way of doing this?

If you’re using awt you could override canvas and get the events from it.

But if your using opengl or something you would need to do all the work yourself. What you describe sounds like a basic way of doing this it. I think the proffesional way would be to create somehing similar to awt. Where you would have a general Component that get all the propper events. Then just override it to create your button.

I am using AWT. Could you please elaborate on how it could be done. I am currently just drawing boxes in certain locations. What would I use if I overwrode the AWT?

By overriding Canvas you are making your own button component that you use instead of java.awt.Button.

Here’s what you will have to do:
*Create your custom button class that extends Canvas.
*Register it as listener to the mouse, focus, keybard, etc in the constructor.
*Override paint(Graphics g) to draw the button to the screen.
*Keep an array of ActionListeners that is called when the putton is clicked. You would have to implement addActionListener and removeActionListener functions

Got the idee?

Yes I got the idea. Thanks, Ill try it sometime this weekend.

If thsi si an AWT app why not just create a new component?

Even simpler why not sub-class the AWt Button and just chnage the paint code??

When overriding java.awt.Component the component will be drawn in the paint function of the parent java.awt.Container.

When overriding java.awt.Canvas, the component will be more heavyweight. It can be repainted independent from the parent.

I don’t think it’s a good idee to override java.awt.Button because it’s a true heavyweigth component that is drawn by native code. And you don’t have access to it’s internal state. So you don’t wich state to draw in the paint function.

If you don’t want heavyweights you should be using SWING.

In which case you can override JButton :slight_smile:

Point taken Jeff. Mine is a game with my own render method and I am using Graphics object and awt to draw all images and text to the screen. I will try to sub-class the awt button and add my own paint code.

Oh by the way if I am devedloping a game and am using active rendering and going for fast frame rates is overridding the Swing stuff a good aproach? I read somewhere that it wasnt as it creates too much overhead.

Ah.

yeah if you are trying to get fast frame rate and integrating the gui into your game its probably best to do what traditional games have done and just write your own menu logic.

So to reiterate my question: is storing button screen coords and checking for mouse clicks in specific areas the right menu logic for a fast game loop implementation?