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();
}
Related
I am working on a delete button, and I created the following:
void onDelete() {
if (!getText().equals(strResult) && !isError) {
mDisplay.dispatchKeyEvent(new KeyEvent(0, 67));
strResult = "";
} else {
clear(false);
}
}
So, when I press the buttons 456 on the calculator app, press delete, 45 remains on the small screen on top ( I don't really know what to call it, just the field that shows what your input is, just like a normal calculator has), exactly how it is supposed to be. However, this will only work if I 'select' the small screen on top. So, when I start the app, click the buttons 456, then press the del button, nothing happens, since I have to 'touch' the small screen on top first to let the cursor 'take place'. I want, however, that it is not necessary to select the calculator screen before using the delete button.
The code I used is based on this code:
I am creating a click test for mousepad in Java. I can differentiate between left, right and middle click through:
if(evt.getButton()==3) // or 1 or 2
What I can't seem to do is to differentiate between 2 lefts.
The 2 lefts being click on 1 and click on 2 in the image above. I have tried to see if I get different values from them when I click by debugging and checking the event object but its the same. For keyboard one can differentiate between two Ctrl or 2 shifts by getting keyLocation, can one do something similar with click? Like not get where on the screen is clicked but which button was pressed.
private void formMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(evt.getButton());
if (evt.getButton() == 3) {
//3=right
button1.setBackground(Color.GREEN);
textField1.setText("Code : Right");
} else if (evt.getButton() == 2) {
//middle
button6.setBackground(Color.GREEN);
textField1.setText("Code : Middle");
} else if (evt.getButton() == 1) {
//1=left
button5.setBackground(Color.GREEN);
textField1.setText("Code : Left");
} else {
textField1.setText("Code : " + evt.getButton());
}
}
This above is my code so far in regards with click.
I have searched a lot but couldn't find information that could help yet.
As far as I know, there is no way to do this. The trackpad will simply present itself as if mouse1 has been pressed when you click the trackpad. Keyboards have different keycodes for left/right shift, etc... I don't believe there are different keycodes for "trackpad click" and "mouse 1".
Anyway, why would you want to add functionality that only laptop users (and only certain laptop users) would be able to use. What about desktops? What about mobile?
You can you below code SwingUtilities methods
SwingUtilities.isLeftMouseButton(MouseEvent anEvent)
SwingUtilities.isRightMouseButton(MouseEvent anEvent)
SwingUtilities.isMiddleMouseButton(MouseEvent anEvent)
Like:
if(SwingUtilities.isLeftMouseButton(evt)){
// do your work
}
// same for right and middle mouse key
But if you want to check where click happed on the mouse from enter link description here
Java Swing is an old technology, it supports the traditional mouse wheel rotation events.
So i don't think its possible to get difference b/w click happening on diff location of touchpad.
From MouseEvent
There is no way to get the location of mouse click.
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!
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);
}
I'm very new to programming with Android, so I'm making a simple text RPG to practice developing for the platform. The player has access to a menu, which shows their items as a group of Radio Buttons. That way one can be selected to Equip/sell/etc. The game is handled in a completely different Activity. I want a random event that can happen in the Game activity to be able to add a new custom Radio button to the Inventory Activity page. On my Inventory Activity page, I've written a simple method:
public void addRadioButtons(){
RadioGroup items=(RadioGroup) findViewById(R.id.invItems);
RadioButton newItem = new RadioButton(this);
newItem.setText("New Rare Item");
newItem.setId(idCounter);
idCounter++;
items.addView(newItem);
}
When the random drop event is calculated in the Game Event, it just does a object.addRadioButtons() method call. This force closes every time, no matter what I try. I've also tried pre-formatting the button before adding it, also to no avail. Am I missing something?
It seems, that the reason in idCounter value. In Android view id must be calculated according to certain rules. Look at this post for more detals.