I am already familiar with JButtons, JLabels and such, but I want to start making a game a very "colorful" menu. Is there a way to do this using Canvas (like adding a mouse listener and make some buttons in PhotoShop and detect if the mouse hovers over and clicks the button), or is there a better way?
Since you're already familiar with JButtons, you may find it easier (and more practical) simply to extend the existing JButton and modify its appearance, so that it looks like an image rather than the traditional grey button.
Amongst other things, it means you get all the standard button behaviour for free, including special cases, and in exchange all you have to do is override a couple of methods.
Have a read through the accepted answer for the following question, which explains pretty much exactly how to do that:
Creating a custom button in Java
Related
So to expand on the question stated, to my best understanding android apps have a set of activities such as the default activity_main.xml and you could even generate a login screen (activity_login.xml). After fiddling around with it I sort of feel comfortable with the way buttons work, yadda yadda, but my problem emerges when I want to create a game activity. I am not concerned about that part, I would just make another activity (activity_gameplay.xml) that I would run the game on. My problem is that I want to render my own custom buttons onto the screen which would be transparent and would be stationary while the actual game environment would be moving. Such as in Minecraft(android version obviously).
So to conclude I need to know how to render custom buttons, and though not included in the above, how to position the buttons accordingly sense the XML way only allows stuff such as upper left-hand corner etc.
I have a 2.5D isometric game that I would like to add a interactive HUD to. I tried using JButtons with Icons but could not draw both the button and my game at once. I'm looking for a cost-efficient (computer resources-wise) technique in which I can draw my own images/buttons to the screen on top of my game.
Most likely will require you to create your own.
First you will need a method of capturing the click event (You may be able to use Swing's existing one depending on your engine). You could for example register a single click event on the screen itself and then implement a method of "capturing" that event through to the component which was clicked on.
Next you will want to create button class which handles its state (MouseOver,Pressed,Unpressed), size and position.
Finally style or how you want your buttons to look. one possible method would be a 9-Patch, which is essentially 9 tiny images that are repeated in such a way to form a stretchable button.
9 Patch Resource - It's for android but the concept still applies
You can also take a look to see how other engines do it such as LibGDX.
Hope This Helps
I have music Applet which uses keyboard, mouse and GUI buttons. When applet is first loaded keyboard events work fine, as do mouse events. However, after I have pressed one of my GUI buttons the mouse events still work but the keyboard events don't, and don't start working again until I refresh the applet.
After hunting around on the net I found some posible solutions, I've tried adding button.setFocusable(true); and button.addKeyListener(this); to all my buttons, and and my panels. No effect at all. I've seen recommendations for converting to a JApplet and using key binding, but surely there must be a simpler way?
Sorry for the lack of code, I've been working on this project since I was a newbie and it's a bit of a mess, and very long!
Any help much appreciated!
button.setFocusable(true); and button.addKeyListener(this); to all my buttons
For JButton use Swing Action or default implementations for ActionListener, rather than KeyBindings (for Swing based Container and JComponents), nor using KeyListener
EDIT
if isn't there really important reasons, don't use prehistoric AWT Applet, use JApplet, may be sufficient would be plain JFrame
Try to cut the problem area out of your project and put it here. Its highly probable, than while localizing the problem area you'll find some errors already.
If your project is a mess already, then the first and the most important thing you should do, is to order it. If it is a mess for you, that means you don't understand it. So, it simply can't work. That is your first and main error.
I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src
I would like to use Swing to program a simple learning game.
I am wondering what would be best way to switch between UI screens.
For example, I would have a screen for the Main Menu, and then when the user presses a button on that screen, I would swap out the whole screen for a completely different one.
Then, arbitrary screens can be swapped in at any moment, and all of their event handlers would be reactivated while the inactive screen's event handlers will be deactivated.
What type of Swing component/control would I use for each of the 'screens'. Is this even doable?
You can consider using CardLayout for that purpose.
Each 'screen' can be made as a separate panel. Then you need a container-panel with card layout. And you'll add all screens to that panel. Switching between cards is easy, it's demonstrated in the linked tutorial.
You could also use JTabbedPane so each screen is connected with one button :-)
You probably don't need to reactivate listeners (although the "business" model end of things might want to check state). Simple switch panels.