I've been looking everywhere for resources on how to create your own GUI system, and I can't find anything good or helpful, nor can I find anything I can understand.

I'm working on a title menu for my game, and I need help writing a decent button class that can detect the mouse's coordinates.

I have mouse input working, but I am having issues simply detecting an area of the screen. I have a point called msl (mouse location) that is controlled by my Input class. I then check for the point by doing something like this:

if (msl == newPoint(12, 12)) hover = true;

. That would only detect one pixel, how would I go for detecting a large area on the screen, similar to what I'd do with an array?

Sorry if this question sounds dumb or if my English isn't spot on, but I really want help with this. I'm tired of text-based GUIs .

The mouse location is a point. The button [is/should be] a rectangle. Search for point-rectangle intersection tests. A button can (but likely shouldn't) be any other shape, so infer what word you would change in the search terms if the button wasn't a rectangle.

From what I remembered from my java2d tower defence game what I did was have a button class (duh ) and inside of it have a rectangle object for mouse collision. In the constructor I set the width and height and x and y. Then I added this to an arraylist in my main class which then, every update or repaint, iterates through that list and checks if the mouse is on it. If so, set the (there is a button image which is slightly transparent) whiteish rectangle behind the button to look like it is lighting up. Then, if clicked inside the rectangle, call the button's click method which is overridden in the class the button is implemented in. I know that is very confusing but that is kinda how I remember it. Just try and do what Agro said instead of hard coding the buttons like I did. Unless you want a challenge

'mse' is the point of the mouse coordinates that are collected in my input class.

As Wesley said, your 'mse' variable is never updated (at least not in the code you posted). Your tick function is referencing the class level variable which is stuck at [0,0].

My approach to doing something like this is to create a base control class that contains the basic properties (visible, dimension, render point, etc) a "contains" function to check for mouse events, and an abstract render method then just extend/override the base class for your more specific classes. It may be a bit of an overkill if you're just looking to add a couple of buttons to your main menu, but it makes implementing other controls such as progress bars, input controls, check boxes, and the like a lot easier (and more fun) in the long run. Your mileage may vary, but this approach has been serving me well while I've been developing my GUI library over the past couple of months.

Edit: Just realized that your 'mse' variable is public. Are you updating this variable directly from outside of the button class? If that's the case, have you tried tracing what value it's set to on each tick? One other note, since 'mse' is already a Point, there's really no need to create a new point in your tick method for the contains check. Just use the 'mse' variable directly.

Arthur: Are all men from the future loud-mouthed braggarts?Ash: Nope. Just me baby...Just me.

Why are you having "butt" do the bounds checking, and why are you suddenly referencing the member variables of your button class as if they're static? Assuming "butt" is a globally referenced object (it's hard to tell because your posted code doesn't seem to contain all the important bits), shouldn't the code be more along the lines of:

This would still appear to be incorrect since "butt" would have to have it's bounds altered in creative ways each time it wanted to do a contains check with a control. In this scenario, you really want the button to determine whether the mouse point is currently inside of it instead of the input class being responsible for the contains check (like your earlier code had it):

The boolean on the tick event can be omitted if you're only using one button. If you're using more, then it becomes useful when iterating over multiple button objects (as in Slyth2727's scenario) to determine if the event has been processed by a particular button thus ending the iteration loop.

BurntPizza's recommendations are also solid and pretty much how my GUI controls are implemented as well. It adds a bit of complexity in the short term, but makes things far easier in the long term. Delving into the source for Java's Component class may also provide some enlightenment/ideas for how to implement your controls.

Arthur: Are all men from the future loud-mouthed braggarts?Ash: Nope. Just me baby...Just me.

The tick method should definitely be constantly checked. However if you have a million buttons (which I have a hard time imagining why you would but still) that will be bad. What I had was a Gui class which basically held and array of all of that gui's buttons. Then in my main class I had a Gui displayedGui that, in the update loop, called displayedGui.drawGui() which drew and updated (tick()) the buttons.

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