I am not good in java at all, but I am trying to programm in my spare time. I know to do some actions but I have no idea to do any mouse events. Does anyone have a good tutorial for this? Because I want to click an image to go to the next screen. For the menu, so the buttons can work. I got now all the buttons in the menu, but if you can't press them it's really useless.

Im sure you have a Input Handler of some sought, that handles muse and keyboard presses.

If you do not have an InputHandler, research that first.

Now you have your InputHandler, the simplest way, would to check if the cursor is within a region e.g. a rectangle or square, once determined the cursor is within the rectangle, you should then check for a mouse clicked.

You should have a class called Button, that will have a render and update method, that you update in your menu screen class.

The first if statement checks to see if the mouse cursor is within a rectangle of the size of the image, if it is, it will then checked it the mouse is clicked it will then proceed and change the screen, in this case print "The button works!".

You would use a "layout" of some kind, even just a simple one, to lay out an arbitrary number of items centered on screen. And you would have a Button type of class that handles mouse click, using a contains(int, int) method to check if the mouse is inside the point.

Using a bunch of if-else statements works, but it leads to messy code that is (a) error-prone, (b) difficult to debug and (c) difficult to adapt. For example; say you want to add a new button later down the road, you might need to copy and paste huge chunks of code in many parts of your program. Or, if you want to change the way the button looks or acts, you would again need to change a lot of code. Ideally it would be better if everything was "modular" and well separated -- that way, if you decide to add/remove buttons, or change them around, it should take minimal code changes.

This would be a good candidate for the Enum pattern. For a larger application, enums might not always be the best choice, but for small games and generally increased productivity they can lead to very clean code.

In this example, we only need to define a new Enum constant when we want to add, remove, or change a button from the menu screen.

During our GUI setup, we don't actually know anything about what buttons exist or how large they are. This "abstraction" means that we won't need to change the following code if, say, we want to add, change or remove a button:

If your button click logic is complex, you might want to move it to new classes (using a command design pattern or something similar). Perhaps it would be implemented by passing the listener to the enum constructor, rather than overriding the enum.

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