I am creating a 15 puzzle game in java. I have already created a game panel and written method that finds the position of the empty place (not JButton) on a field and a method that controls whether it is a winning combitanion or not (JButtons on a correct places). So, I got something like this:
I have a trouble with writing an ActionListener. Is it possible to write a method that determines that actionListener should be added to the JButtons that surround the empty space.
I really need some tips or/maybe already written methods/solutions.
Thanks!
I think your best bet would be to keep the action listener active on all the buttons. When clicked, it simply does nothing if it determines itself to not be adjacent to the empty box.
I would recommend ditching the idea of only adding actionListeners to the buttons that surround the empty space: keep actionListeners added to all buttons and only process a click if it is valid.
Also, in my experience, it is very user-hostile to only allow a click on one of the tiles that surround the empty space. In the example picture, you are planning to only allow a click on 7, 1, and 11. You should allow a click on 5, 7, 1, 13, 14 and 11. So, for example a click on 14 should move 1, 13 and 14 to the left.
Related
I am programming a lottery program in JavaFX (6 numbers of a 6x7 field + 1 lucky number) which allows the user to create max 10 tips at once. Then the program evaluates if he won.
My current problem is to delete a Tip that has been added. I have a Class "Tip" which consists of an HBox with 7 labels inside (for the 7 chosen numbers) and a button to delete this Tip (so every Tip has its own delete button).
My function to add a Tip works (even though, I don't really know if this is really clean. So if you see any improvements, I'd be really thankful! :-)).
Under the following link, you can find my entire code:
https://github.com/Lucindor/Lottery.git
The main issue or part that I'm stuck is in the Controller from line 73 to 80 (deleteTip method).
My main idea was to removethe parent of the button (which should be the Tip(?)), which should have removed the entire tip...but this didn't work. I couldn't get any ideas more.
In the end, I want to be able to click on any delete button which removes this tip and moves all the tips below (So that it's like a list).
Thanks a lot!
So I have an application developed in C# that creates a bunch of controls on command from a button click. It does a lot of control creation but I've scaled it down to just the first two of a set that it creates, for simplicity. In the picture below you'll see that I have pressed the Create button (which goes invisible upon clicking) and it made 16 (ability to scroll to see more) text boxes and combo boxes each respectively aligned with each other.
Now, I know I should have thought of developing this in a cross platform environment before production, but disregarding that, my main problem now is emulating this application in Java using Swing and AWT GUI objects.
I do have the ability to create the text boxes all in line as shown.
In addition I have the ability to create the combo boxes where I'd want them to be, in line as shown.
However, once I try to do a dynamic create with both, then the location/positioning gets really messed up.
Is there an attribute or property that I'm missing? The code for the location positioning is below... They don't have any other attributes turned on or off that are different from their defaults.
panelContainer.add(newComboBox);
newComboBox.setSize(95, 20);
newComboBox.setLocation(miniCount * (newComboBox.getWidth() + 10) + 80, 45 + levelCount * 170);
panelContainer.add(newTextBox);
newTextBox.setSize(95, 20);
newTextBox.setLocation(miniCount * (newTextBox.getWidth() + 10) + 80, levelCount * 170);
The miniCount and levelCount variables just make sure that we have only 6 (miniCount) items per row and to go to the next row if we need to (levelCount). The rest of the magic numbers are for positioning of course.
The text boxes are AWT TextFields boxes. The combo boxes are AWT Choice boxes.
EDIT:
The Java application and C# application are separate. They ultimately run the same algorithm, but I'm just have trouble making the GUIs act the same.
I'm new to stackoverflow. I have this question for Java, though, and there might be a simple way to do it but I don't know how.
Here is my problem. I have my game, on the press of a button (X), to display text on the screen under the title of the top left of the game. That works fine, but lets say I have multiple. If I press N which could be the fifth one, it puts it what looks like random on the screen. But if I have all the buttons pressed, it will look normal, because all will be in line with each other.
Here is the code I have to write a line under the title of the game.
var8.drawStringWithShadow("Test", 2, 11, 0xFFFF);
The first parameter is the text, second is x, 3rd is y.
Now, I have it drawing another one at the press of another key.
var8.drawStringWithShadow("Test2", 2, 20, 0x008B);
As you can see, the y parameter was incremented by 9, so it would be under the other one.
What I want to do, is have the Y always be the same (11), But, if text is already there, push it down.
So lets say I had "Test" being displayed. But then I press the button to enable "Test2", and I want it to take the place of "Test", but not overrite it, when it is pressed, if "Test", or any other buttons are enabled, it will increment the y of "Test" or another one by 9.
I hope there is a simple way to do this, if there is, please tell me. Thanks!
Make a counter for how many strings are there and let
y = counter*9 + 11
I'm running a large Java Eclipse project and when it starts up, I see 3 pop-up windows tiled on top of each other, as so:
I need to have them start up separated positions, as I'm always testing the program and dragging the windows. Can I somehow put them all in a bigger GUI window(pane)?
Use setLocation() method
windowName.setLocation(location_parameters);
passing the 'null' as an argument will make the window center.
There is another way,
setLocationRelatedTo(item_to_relate)
method. this will locate them according to the existing things like JLabels, existing windows, etc.
Can I somehow put them all in a bigger GUI window(pane)?
You presently have 3 JFrames, I think.
You could change this to 3 JPanels in one frame.
You would use a layout manager to arrange the 3 JPanels the way you want.
I wanted to make a "memories" or "find pairs" game for Java, just to start with a basic game before starting something more difficult. My game works very well, but I just have 1 big problem. I don't really know how to hide the 2 images x seconds after the user clicks on the second one.
This is what is done:
The game is created with 4x4 buttons in a array
The values of the cards are distributed (name, position, icon)
The user then clicks on the first card, which shows up immediately
The user clicks on the second one, if the first is equal to the second one then disable the 2, if not then re-hide the 2.
But I don't know how I can make the program show the second card and then after x seconds hide the 2...
How can I solve this?
Depending on what framework you are using, there should be a timer utility available to you.
For instance, if you are using Swing, then you should be able to use javax.swing.Timer as suggested in the above comments. Follow this link for a simple Swing Timer tutorial. As they say on that page, swing timers can be used in one of two ways and one of those ways is:
To perform a task once, after a delay
This sounds like exactly what you are trying to achieve.
You could also try a library like Joda Time which has lots of features and options.
you should make match function and another unmatch function and inside the unmatch function
you can use setTimeout and give the two cards class(flip) when you choose them and after certain time and when they didn't match remove this flip class.