I'm trying to create a simple Java GUI window in which when I a number is placed in the window, the program determines what the value of the number is (ie greater than 2, or less than 2), and then executes an autoclick depending on the int value.
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
for (int field = 0; field <= 2; field++) {
if (field <= 2) {
(EXECUTE AUTOCLICK;
}
else if ( field >= .15 ) {
do nothing
Obviously it's broken and the 'EXECUTE AUTOCLICK' is not a function, but I figured i'd put it in to show what i'm trying to accomplish.
Additionally, how would I add an auto click function into the code and where would I add it?
Can you tell tell that I'm a noob?
Related
I am creating a program that takes an input file, parses that information and builds a GUI calculator from that information. Currently the program works well except for when I implement an ActionListener on buttons that should set a text field to the value of the buttons getText() method.
I have tried a few different loop constructions using for and while, but all of them I have implemented have not found the where i or a counter is equal to the parsed int from numPad.getText() or returned 0 for all of the buttons.
The problem I am having while testing is that the variable i never matches numPoint. Logically my approach is to decrement i so that the loop will continue to look for matches, but never does this. The test output statements infinite loop "-1" for i and "7" for numPoint. As a note, the numPad array is not in order but instead the elements are as follows {7, 8, 9, 4, 5, 6, 1, 2, 3, 0}.
I realize that this loop may not be logically correct, but I am having a hard time finding a solution that works. I want to avoid hard coding if statements (such as i == Integer(parseInt.numPad[0].getText()) which would work.
This is the loop that creates the new buttons and adds them to an Array, sets the text based on a list created from the values of the input file and adds an ActionListener.
for (int i = 0; i < run.buttons.size(); i++) {
numPad[i] = new JButton();
numPad[i].setText(run.buttons.get(i));
numPad[i].addActionListener(new ButtonActionListener());
panel1.add(numPad[i]);
}
This is the most recent attempt at creating a loop that should make the assignment.
public static class ButtonActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int i;
int numPoint;
for (i = 0; i < numPad.length; i++) {
numPoint = Integer.parseInt(numPad[i].getText());
if (i == numPoint) {
//Match, assign
System.out.println("works");
break;
} else {
//Decrement and continue
i--;
System.out.println("test statement" + i + " " + numPoint);
continue;
}
}
}
}
There are several ways you might do this, but let's start with the basics
for (int i = 0; i < run.buttons.size(); i++) {
numPad[i] = new JButton();
numPad[i].setText(run.buttons.get(i));
// You don't "have" to do this, as the action command defaults
// to the text of the button, but this is away to provide some
// kind of identifier to the action which might be
// different from the text
numPad[i].setActionCommand(run.buttons.get(i))
numPad[i].addActionListener(new ButtonActionListener());
panel1.add(numPad[i]);
}
Then in your ActionListener...
public static class ButtonActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
int numPoint = Integer.parseInt(command);
// Perform what ever action you need
}
Disclaimer: I'm really new at this and I apologize in advance if:
1) my question has already been asked (I've tried searching and had a lot of trouble finding what I needed)
or 2) if I'm not asking the question correctly.
Basically, I'm trying to make a game where pressing the spacebar triggers a sort of "super-power" that will perform a set of actions just once. Afterwards, if they try to press it again, it'll run up some sort of dialogue box that says their one-time super-power has already been used.
What I have:
try {
Key move = canvas.getLastKey();
int space = 0;
if(move == Key.SPACE) {
if (space == 0) {
space = 1;
}
if (space == 2){
JOptionPane.showMessageDialog(null, "superpower already used");
}
}
if( space == 1 ) {
//action here
canvas.resetKey();
space = 2;
}
}
Right now, the super-hero action is on an endless loop but if I reset the key here:
if(move == Key.SPACE) {
if (space == 0) {
space = 1;
canvas.resetKey();
}
the user can just use the super-power over and over again. Any help would be appreciated!
In the third line, you have written int space=0 so your variable is constantly reset to 0...
You have to initialize it elsewhere (the beginning of your program is a good place for any global variable).
You should consider moving int space = 0, outside of the try block. I suppose your try block gets invoked repeatedly, so you should declare this variable under a global scope.
I'm trying to make a 2d game using java microedition and i just want to make my control smoother but the problem is when i press the LEFT key while pressing the UP Key the condition is not working i dont know why
public void moveJet2() throws IOException{
int gameAction = getKeyStates();
if(gameAction==LEFT_PRESSED && gameAction==UP_PRESSED){
padX-=padXVel;
padY-=padYVel;
}
else if(gameAction==RIGHT_PRESSED){
padX += padXVel;
}
else if(gameAction==UP_PRESSED){
padY-=padYVel;
}
else if(gameAction==DOWN_PRESSED){
padY+=padYVel;
}
}
getKeyStates() returns the state of keys in a single int. The various keys have individual values. UP_PRESSED = 0x0002 and LEFT_PRESSED = 0x0004. So if you press UP on your d-pad while calling getKeyStates(), you'll get 2 back, and if (getKeyStates()==UP_PRESSED) will thus be true.
Likewise, if you press LEFT on your d-pad while calling getKeyStates(), you'll get 4 back.
But if you press UP and LEFT at the same time, you can't get back 2 and 4 - because that's obviously 2 ints - and getKeyStates() only returns one int.
What you do get back though, is rather simple: 2 + 4 = 6.
So, asking if (getKeyStates()==6) will be true if pressing UP and LEFT at the same time. Or if (getKeyStates()==UP_PRESSED+LEFT_PRESSED).
Typically though, you would ask using bit-operators, like this:
public void moveJet2() throws IOException{
int gameAction = getKeyStates();
if((gameAction & LEFT_PRESSED)!=0) {
padX -= padXVel;
}
if((gameAction & RIGHT_PRESSED)!=0) {
padX += padXVel;
}
if((gameAction & UP_PRESSED)!=0) {
padY-=padYVel;
}
if((gameAction & DOWN_PRESSED)!=0){
padY+=padYVel;
}
}
Because using that approach will work with any of the 8 directions you can press at the same time.
I have a problem with my code. I'm writing a program that has to manage weekly squat trainings. There is the possibility to record a previous training or to program a full week. In the second case, for each day, user has to put in the number of squat and the difficulty he thinks it will be. I manage the difficulty of each day with a choicebox that has 3 voices (easy, medium, hard) and when I read the value selected, I save it as an int in order to use it easly for other things in the program (easy=1, medium=2, hard=3).
Now, my problem is that I have 7 choiceBox, one per day, and I need just one method to handle them. Everytime user select a value from one of those choiceBox, it should call the method that scans the value of all the 7 choiceBox and update the relative int values. I adopted this solution because I use only one method for 7 choiceBox and I don't know which one calls the method. To do this, I created a vector of choiceBox and I use a for cicle from 0 to < .length, but everytime I select a voice in any choiceBox, the cicle goes out of bounds. I also created a relative array of int to save the different difficulties.
Here the exception and my method:
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException: 7
public void weekDifficulties() {
for (j = 0; j < arrayCB.length; j++) {
arrayCB[j].getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue ov, Number oldValue, Number newValue) {
if ((Integer) newValue >= 0 && (arrayCB[j].getItems().get((Integer) newValue).equals("Easy")))
arrayDifficulties[j] = "1";
else if ((Integer) newValue >= 0 && (arrayCB[j].getItems().get((Integer) newValue).equals("Medium")))
arrayDifficulties[j] = "2";
else if ((Integer) newValue >= 0 && (arrayCB[j].getItems().get((Integer) newValue).equals("Hard")))
arrayDifficulties[j] = "3";
}
});
}
}
Thanks for helping
j seems to be a field. In that case when the listeners are triggered, it contains the value last assigned to it, which is arrayCB.length = arrayCB.length (= the first int where the j < arrayCB.length yielded false). This of course leads to a ArrayIndexOutOfBoundsException.
Since you obviously need to access the value of the j field at the time the loop body is executed, you need to copy it to a variable:
for (j = 0; j < arrayCB.length; j++) {
final int index = j;
arrayCB[j].getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue ov, Number oldValue, Number newValue) {
if ((Integer) newValue >= 0 && (arrayCB[index].getItems().get((Integer) newValue).equals("Easy")))
arrayDifficulties[index] = "1";
...
}
});
}
Furthermore j probably shouldn't be declared a field in the first place
Check if the arrayCB.length and arrayDifficulties.length are equal otherwise you would get java.lang.ArrayIndexOutOfBoundsException. Else
EDIT
Can you store the index j in final variable and use it in the changed method. Possibly it get incremented by the time change Listener notify the method.
OK so I have these 2 labels where if both are them are 0, a button will be disabled. This is what I have done but didn't manage to do it, please guide me thanks a lot!
int quantity = 0;
int sum = 0;
I initialize them as 0, after that going through some IF loops and working well, and there's one event which is sort of like clear, I reassign them 0 again which looks like this:
quantity = 0;
sum = 0;
Then now I have 2 Labels which I want to compare to these 2 value, if both are 0, then disable a button, This is what I have done but failed ,the button still remained enabled. Then I realized I'm comparing to string 0 instead of integer 0, how can I compare it with the quantity and sum?? thanks a lot!
if ("0".equals(jLabel4.getText()) || ("0".equals(jLabel4.getText())));
{
jButton2.setEnabled(false);
}
if you want to compare integers, why compare Strings?
0 == Integer.parseInt(jLabel4.getText());
also surround it with try-catch block
boolean equals = false;
try{
equals = ( 0 == Integer.parseInt(jLabel4.getText()));
}catch(NumberFormatException e){
//equals = false;
}
You should separate your gui code (the "view") from your non-gui logic code (the "model"). The GUI should display the state of the model, i.e., show in your JLabels the values held by the two ints of the model, but it is the model's ints which should be checked for 0 value, and then your GUI should enable or disable the button accordingly.