Hi, I wish to know why the AND operator is not working here? Even though without operator it working fine what is wrong while including it?
else if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT && keyCode == KeyEvent.KEYCODE_S) {
int visibility=new_Sell_order_cart.getVisibility();
if(visibility==View.VISIBLE)
{
openCart();
}
}
If its not possible how jQuery developer Achive this
if (e.keyCode == 65 && e.ctrlKey) {
alert('ctrl A');
}
});
keyCode cannot be two keys at once. It can be either CTRL_LEFT or KEYCODE_S.
This statement returns false because either one will be true but not both.
(Its like a==3 && a==4, a cannot be 3 and 4 at the same time)
keyCode == KeyEvent.KEYCODE_CTRL_LEFT && keyCode == KeyEvent.KEYCODE_S
Probably you are looking for OR (||) operator:
else if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_S) {
EDIT:
If you want to check for two keypress. Here is the solution suggested in this post.
One way would be to keep track yourself of what keys are currently
down.
When you get a keyPressed event, add the new key to the list; when you
get a keyReleased event, remove the key from the list.
Then in your game loop, you can do actions based on what's in the list
of keys.
I got one Solution , I know it is not up-to mark but it full-fill my requirement .
else if (isCTRLisPressed(17) && keyCode == KeyEvent.KEYCODE_S) {
int visibility=new_Sell_order_cart.getVisibility();
if(visibility==View.VISIBLE)
{
openCart();
}
}
and
public boolean isCTRLisPressed(int i){
if(i==17){
System.out.println(" key code of ctrl"+i);
return true;
}
else {
System.out.println("key code in else "+i);
return false;
}
}
Related
I am making a typing test in Java with JavaFX. I want to compare the text that is being typed in a TextField to the defined random words. However, the TextField only updates with the letter just typed sometimes and not all the times. The following code is where this problem is occurring.
field.setOnKeyTyped(e -> {
String typingWord = field.getText();
if( typingWord.isEmpty() &&
e.getCharacter().charAt(0) == '\b' &&
!(typedWords.size() == 0) &&
typingWord.equals(previousWord)){
field.setText(typedWords.get(typedWords.size() - 1));
typedWords.remove(typedWords.size() - 1);
field.positionCaret(typingWord.length());
index--;
}
//compare random words to typed words
if (Character.isWhitespace(e.getCharacter().charAt(0))) {
typedWords.set(index, typingWord);
field.clear();
e.consume();
index++;
}
previousWord = field.getText();
});
Is this purely due to the speed of my computer or is it just a bug in JavaFX?
Sir i do not know what you want to do but i want to help, so in your onKeyPressed() or onKeyReleased(), it gives a KeyEvent that is e in your case, you can use that in place of Character.isWhitespace() & e.getCharacter().charAt(0) and also /b
To get your KeyCode
field.setOnKeyTyped(e -> {
KeyCode kc = e.getCode(); // your keycode here is the character you just pressed
now with the KeyCode you can check for a whole lot of goodies,without falling to character
equality checks
if(kc == KeyCode.BACK_SPACE) //checking for your backspace
if(kc.isWhitespaceKey()) // your enter, tabs, space etc
also you can make use of these methods
TextField.deletePreviousChar();,TextField.deleteNextChar(); that might save you all the selection before clearing. if you want to clear like 10 of them then loop it 10 times
lastly why do you check for emptiness & later check if its not empty
So, I've been stumped on this piece of code for about a week now, I want to have the code sending an error message when the user chooses 'No' or 'Cancel' however I get an error which tells me that NO and CANCEL are not variables. Does anyone have any suggestions as to how I can overcome this problem?
int mc = JOptionPane.QUESTION_MESSAGE;
int bc = JOptionPane.YES_NO_CANCEL_OPTION;
int ch = JOptionPane.showConfirmDialog (null, "Select:", "Title", bc, mc);
if (bc == NO)
{
JOptionPane.showInputDialog("Sorry, you cannot continue without agreeing to the rules.");
}
else if (bc == CANCEL)
{
JOptionPane.showInputDialog("Sorry, you cannot continue without agreeing to the rules.");
}
else
{
JOptionPane.showInputDialog("Thank you, you may continue!");
}
This is all explained in the JOptionPane Javadoc.
In the code that you've given, the button that you've clicked is identified by the return value from showConfirmDialog, which you've assigned to ch, not bc. In your case, the logic should be
if (ch == JOptionPane.NO_OPTION) {
...
}
else if (ch == JOptionPane.CANCEL_OPTION) {
...
}
else {
...
}
I have a program in progress that creates two "dominoes", of randomly generated integers end1 and end2. [end1 | end2] is the output of the "domino". I am trying to write a method that checks to see whether two Dominoes are the same. I want this method, equals(), to compare this Domino to the Domino passed as a parameter. For example [4|2] would be equal to [2|4]. I'm not sure how to compare the integers in two objects that are created in a different class. I'm sorry if this isn't clear, I will be happy to answer any questions to help. Thanks so much!
so far, I have
public boolean equals(Domino d) {
return (this.end1 && this.end2 == Domino.end1 && Domino.end2 ? true : false);
}
Clearly, this is very wrong syntactically, but conceptually, it's what I am looking for.
You have to compare the first end of one Domino to the first end of the other, and the second end of the first to the second end of the other. If they don't both match, you compare the first end of one to the second end of the other and vice versa.
public boolean equals(Domino d) {
if (this.end1 == d.end1 && this.end2 == d.end2)
return true;
if (this.end2 == d.end1 && this.end1 == d.end2)
return true;
return false;
}
A little simpler and easier to read:
public boolean equals(Domino d) {
if ((this.end1 == d.end1 && this.end2 == d.end2) ||
(this.end2 == d.end1 && this.end1 == d.end2))
{
return true;
}
else
{
return false;
}
}
Just think about what you wanna do in English:
IF
this end1 and Domino's end1 are the same
AND
this end2 and Domino's end2 are the same
OR
this end2 and Domino's end1 are the same
AND
this end1 and Domino's end2 are the same
RETURN true
ELSE
RETURN false
That normally makes it easier to think about how to set the conditions
public void processLightClick(int row, int col) {
states[row][col] = !states[row][col];
if(states[row][col] = true){
lights[row][col].setFill(ON_PAINT);
}
else if(states[row][col] = false){
lights[row][col].setFill(OFF_PAINT);
}
turns++;
System.out.println(row+":"+col);
status.setText("Turn Number " + turns);
}
This is the code I have. I'm trying to get it to toggle states and colors each time a specific square is pressed but the paint stays the same and doesn't toggle the state back and forth when I press the same square twice
You are using an assignment operator "=" instead of a comparison operator "==" in your if-statement. Try this.
public void processLightClick(int row, int col) {
states[row][col] = !states[row][col];
if(states[row][col] == true){
lights[row][col].setFill(ON_PAINT);
}
else if(states[row][col] == false){
lights[row][col].setFill(OFF_PAINT);
}
turns++;
System.out.println(row+":"+col);
status.setText("Turn Number " + turns);
}
As user2615117 pointed out, you are not using the comparison operator, but the assignment operator. Here, however, I also would use the ternary operator to make the code more concise:
lights[row][col].setFill((states[row][col] == true) ? ON_PAINT : OFF_PAINT);
Another possible improvement would be to have a single array of Light objects which contain both the state and the paint of the light.
I am teaching myself, from online tutorials, how to write games in Java. I am using Java Applets to create a Pong game. each paddle is controlled from different keys for 1v1 competition. this works fine if both users are hitting the keys at different times. but when one key is being held down and then another key is held down(ex: holding down on the arrow key, then user 2 holds the 'S' key), the second key overrides the first and the first paddle will stop moving. i'm guessing that i need to use threads but i don't know much about them and i am having trouble understanding how to use/implement them. how would i go about handling the case when two (or more) keys are being held down?
Bonus: like i said i don't know much about threads - i'm assuming i also need one for the ball/puck to be moving around while all else is going on. is the right and if so how do i put a thread on something that takes no input?
Thanks for you help,
DJ
What you usually do is to remember the state of every keypress.
You keep an array of your actions(or an array of all the keys if you want too). A keyDown event results in e.g.
boolean actions[12];...
...
public boolean keyDown( Event e, int key ) {
if (key == 'a') {
actions[ACTION_LEFT] = true;
}
..
}
And you'll need to catch the keyup event and set the actions to false when the keys are released.
In the movement logic you can just check the states of the keypresses
if(actions[ACTION_LEFT] == true)
moveLeft();
if(actions[ACTION_RIGTH] == true)
moveRight();
Usually instead of threads, games use something called the game loop (google it).
http://en.wikipedia.org/wiki/Game_programming
The basic idea is
Loop
Get all inputs
Get game clock
Update state based on clock and inputs
Update Display
Limit frame rate if you need to
Anyway -- instead of getting keyboard events -- you check the keyboard's state at certain points. This code seems to do it for Java
http://www.gamedev.net/reference/articles/article2439.asp
It uses events to set variables you look at in your loop.
Just in case anyone wanted to see how I ended up answering this question. My keyDown() and keyUp() method are as follows:
public boolean keyDown(Event e, int key)
{
message = key + "pressed";
//right paddle
if(key == 1005)
{
rpaddle_up = true;
}
if(key == 1004)
{
rpaddle_down = true;
}
//left paddle
if(key == 115)
{
lpaddle_up= true;
}
if(key == 119)
{
lpaddle_down=true;
}
//x key = exit
if(key == 'x')
System.exit(0);
return true;
}
public boolean keyUp(Event e, int key)
{
//right paddle
if(key == 1005)
{
rpaddle_up = false;
}
if(key == 1004)
{
rpaddle_down = false;
}
//left paddle
if(key == 115)
{
lpaddle_up= false;
}
if(key == 119)
{
lpaddle_down=false;
}
return true;
}
Hopefully if someone has the same issue this will help them. And thanks everyone for your input and help.
If I'm not mistaken, you have the keyUp() and keyDown() methods at your disposal with Applet. Have you tried setting a flag on keyDown, then unsetting it on keyUp? For example:
public boolean keyDown( Event e, int key )
{
if (key == 'a') {
player1.go("left")
}
}
public boolean keyUp( Event e, int key )
{
if (key == 'a') {
player1.stop("left")
}
}
Just an idea. I'm sure there's a standard way to deal with this.
You could use Swing Timer to periodically fire movement events between a keydown/keyup event.