Individually adding Buttons - java

I plan to make a text Adventure for a school project, for this I need to add individual buttons which all have actions
Example:
You stand before a troll, a button shows "Attack" and it actually lets
you attack
you stand before a door, a button on the exact same spot shows "Open"
and it opens the door
Every tutorial I've found only shows how to make buttons and add them but I need to individually generate buttons with full functions.

You can create all of your buttons and place them where you want to (same position as you mentioned). After that you only have to change the visibility of the buttons with the setVisible() method.
Like this:
if(/*stand before a troll condition*/)
{
attackButton.setVisible(true);
openButton.setVisible(false);
}
else if(/*stand before a door condition*/)
{
attackButton.setVisible(false);
openButton.setVisible(true);
}
else // standing before nothing
{
attackButton.setVisible(false);
openButton.setVisible(false);
}

Related

How do action events work with players' turns, need help showing which card the user played before the computer shows what card it played

Im creating an uno game and it is almost fully functional and i'm using javaFX to create it. It is Computer vs User and the problem is that when a user clicks on a card button, the middle card is supposed to be replaced with that card and then the computer goes, however what ends up happening is that the computer goes right away and so you only see the computer's card played in the middle. I am sure this has something to do with how i structured my code specifically the action event pertaining to the button clicked. However, since this is my first ever java GUI and i am fairly new to java i am struggling to understand how to structure it so that i am able to show the user's move. Also due to this, i am unable to implement a skip card even though i have tried using a boolean and creating another method i think the main problem lies within how the main gameloop and the actionevent inside is set up.
This method is called and it checks for a button click and then gets the index of the button in the player's arraylist/hand/ The boolean was my attempt at implementing a skip card but what kept happening is the computer would always play right after the user's move when a skip card was played.
public void gameLoop(){
for(int i =0; i<buttonList.size();i++){
buttonList.get(i).setOnAction((ActionEvent)->{
indexOfHandButton = buttonList.indexOf((Button)ActionEvent.getSource());
humanMakeMove();
if(!isReverseTurn()) {
computerMakeMove();
}
});
}
}
This method used the index given from the gameloop actionevent and makes the actual move in the backend. It then clears the hbox containing the users hands and calls showhumanHand which fills it up with the updated cards and similarily with the middle card. The if statements to try and implement the skip cards have not worked, i have also tried creating a new action even inside the if statement and creating a method or recursively calling humanMakeMove but of course the index will be the same and will cause an error.
public void humanMakeMove() {
String result = human.makeMove(game, theDeck, indexOfHandButton, computer);
if(result.equals("skip")){
setReverseTurn(true);
}
hbHumanHandBtn.getChildren().clear();
hbMiddleCard.getChildren().remove(middle);
middle = generateMiddleCard();
hbMiddleCard.getChildren().add(middle);
showHumanHand();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>" + buttonList);
System.out.println(result);
if (middleCard.getType() != null) {
if (middleCard.getType().equals("skip") && isReverseTurn()) {
System.out.println(isReverseTurn());
gameLoop();
setReverseTurn(false);
}
}
}
So my main question is how can i restructure this or add something that allows for the user's card to show in the middle before the computer goes right away, i have tried a timer/sleep method too. And my skip card doesnt work but i think that has to do with how i set up my action listener which is messing up two problems. Thank you for reading this and let me know if you have any input!

Info card prioritizing -java flash card applet

I am wanting to make an applet for practice, and my goal is to make a program that displays seven rectangles with info inside of each one. I would also like the cards to display in a random order.
After the cards are displayed, the user should be able to click on the card, and then the card should be removed from the options and be displayed beneath them, in the order that you click them. This may sound confusing, but I basically want the user to be able to prioritize or sort the info cards.
For example, if the cards had dates on them, the user could sort them in order from past to present.
My first idea was to draw rectangles on the screen and get the mouse click x and y to see if the user clicked that card, but I'm sure that there is another way that doesn't have to be that complicated.
I'm sorry that I don't have decent code to post, I would rather not post my messy version. I can update this with code later.
I'm wondering what the best solution would be, because I would like to learn as much as I can from this project.
You can use panels, and register for action events. Action events don't care the coordinates where your mouse was clicked, but rather if the component was clicked or not. You can use setActionCommand() to identify each panel (card) or use some other attribute of your panel that you can read after capturing the event (the event.getSource() method returns the component that was clicked).
panel.setActionCommand("card1");
panel.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("card1") {
// do something
}
}
}
You can also use an existing LayoutManager or adapt or write one to display your panels any way you like.

Program with split screen, how can I change the content [Java]

I had just a few class of java on college, but I want to do a thing that I don't know how.
Is basically a window with a SplitPane, on Left side I have a menu made with toggle buttons, and on the Right side I need to change the content based on each button.
Theres any way to design the ViewA and ViewB on separated JFrame Form and load then inside my Right Side when I click on menu items?
Another idea is, put the ViewA and ViewB put a JTabbedPane on the Right Side, and hide the Tabs. So there's any way to hide the tabs?
I have none experience developing in java, any problem about this concept (difficult, loading time, memory, maintenance), If you guy know a better way to to this, I just don't want a lot of windows popping up.
A really simply way would be to simply have a set of jPanels in the right side, with only one ever set to Visible.
Basically, for each toggle on the left side, you will have an Event Listener that does this:
private void toggle1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
}
Simply changing the true value depending on the individual toggle.
In Netbeans, if using the GUI editor, you can simply double click the toggle button to generate the listener and appropriate method, then fill in the code for it.

Multi Slide Representation

I have a project in which I am supposed to have a simple painting panel and I also have to make it possible for the user to have more than one drawing panel, like a multi slide representation.
So far, I finished the coding for 1 single panel which is an expansion of JPanel. Now, with two smiple JButtons on it(previous and next), I need to be able to open a new clean panel and I also need to be able to go back to the previous one which includes my last drawings.
I'm kinda stuck here and need some idea about how to make this work.
Use a CardLayout for the slides. It has next() / previous() methods.
You can use a LinkedList to represent your slides, each element of the linkedList could be the JPanel. To navigate I think it's easier to use a ListIterator (you can access it with the method LinkedList.listIterator()), so when your user press the forward button your could would look like:
void btnForwardPressed(){
if(!this.iter.hasNext()) System.out.println("No slides forward");
else this.currentSlide = this.iter.next();
}
And for the back button you would have something like this:
void btnBackPressed(){
if(!this.iter.hasPrevious()) System.out.println("No slides back");
else this.currentSlide = this.iter.previous();
}
You could also control the back and forward buttons state by tracking the return of the methods this.iter.hasPrevious() and this.iter.hasNext().

Randomly perform a click on a button in android

I'm making a game as an android app in which the user clicks on buttons to change its color and so on...
One thing I'm trying to implement is to make some initial moves when the app is started by randomly performing clicks on various buttons. However, I'm having a real hard time trying to figure out how to randomly select some number of buttons and do its performClick() method. Does anyone have any ideas?
Thank you
Billy
Place your buttons in array, generete random number, so that number would be an button-array index.
What Mighter said above should work. But it sounds like the code would be cleaner and more MVC-like if you separate your view code(button handler) from controller(the logic to alter game state), and directly call your controller instead of doing performClick(), in other words:
Move the "change color" logic inside
each button click handler into a
method alterState(int actionId);
Call alterState() within each
button's click handler
When the app starts, call
alterState(new Random().nextInt() %
NUM_ACTIONS) in a loop to perform
your random moves.
Very Simple way to select Radio Button Randomly: suppose 3 radio buttons are there
int a = new Random().nextInt(3);
if(a == 0)
{
idAccountOption.click();
//(idAccountOption)-id of radio button on application
}
else if(a == 1)
{
idPremisesOption.click();
}
else if(a == 2)
{
idRouteOption.click();
}

Categories

Resources