The compiler is not recognizing my input for .getPoint - java

We areto create a plane and have its speed increase 5mph every time the user inputs the up arrow key and decrease 9mph when hitting the down key. It is supposed it report this back to the user in output box whenever whichever action is completed. How do I set it up so that the keylistener will tell the user that they have pressed the up key? Im still working on this so sorry if it is messy.
private class PolygonPanel implements MouseListener, MouseMotionListener, KeyListener
{
public void mousePressed(MouseEvent event){} //unused event
public void mouseDragged(MouseEvent event){} //unused event
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event){}
//public void keyPressed(KeyEvent event){}
public void keyPressed(KeyEvent event) {
Integer planespeed=event.getKeyCode();
if (event.getKeyCode() == KeyEvent.VK_RIGHT)
{
//Not gonna use this.
}
if (event.getKeyCode() == 39)//KeyEvent.VK_LEFT
{
System.out.print("This key is up");
}
if (event.getKeyCode() == KeyEvent.VK_UP)
{
System.out.print("This key has been pressed");
//speed=planespeed+5;
}
if (event.getKeyCode() == KeyEvent.VK_DOWN)
{
//speed=planespeed-9;
}
repaint();
}

A frame doesn't detect keybaord input if it is not focused on. Therefore, in order to retrieve keyboard input of any kind, you would have to set your frame to be focusable, using a line such as setFocusable(true); in your constructor. You might also have to add a line such as addKeyListener(new PolygonPanel()); so the listener is registered at all.

Related

How do I respond to two different KeyEvents Simultaneously?

So I am basically creating a platformer game in java. Here is a simple question. Can I respond to two different KeyEvents simultaneously? For example, I am pressing the right arrow key and my player is moving right. Now, I want my player to jump but keep moving right. For this I would need to respond to two different KeyEvents simultaneously. How do I achieve this? I have not tried any code yet because I don't know where to start with. Thank you for the answer in advance.
You can use seperate "KeyDown" and "KeyUp"-events and set state values for your player.
You need a State-Machine for your player.
public class KeyboardListener extends JFrame implements KeyListener {
public KeyboardListener() {
addKeyListener(this);
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Right key pressed");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Left key pressed");
}
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Right key Released");
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Left key Released");
}
}
public static void main(String[] args) {
new KeyboardListener();
}
}
Use the keyPressed and keyReleased-EventHandlers from the KeyListener Interface and save your current player states like walking, jumping, walk_left, walk_right etc as booleans for example.
In your mainloop you can check what state your player is in and change its coordinates. Like:
if(player.left) { player.x += speed } //etc.
You might want to use Threads and then add pressed keys to a list the player physics class.
public class KeysListener implements KeyListener
...
//FIRST THREAD
new Thread(new Runnable() {
#Override
public void run() {
public void keyEvent(KeyEvent e) {
int key = e.getKeyCode();
//TODO ADD KEY TO A LIST IN THE CLASS THAT HANDLES YOUR PHYSYICS
}
}
}).start();
//SECOND THREAD
new Thread(new Runnable() {
#Override
public void run() {
public void keyEvent(KeyEvent e) {
int key = e.getKeyCode();
//TODO ADD KEY TO A LIST IN THE CLASS THAT HANDLES YOUR PHYSICS
}
}
}).start();
...
}
Add as many threads as the number of simultaneous keys you require.

How can the Mouselistener be used ? to move buttons?

I am currently working on a small boardgame. I have to place "objects" on a Button array. I thought of placing the objects are 1*1 1*2 etc,They are represented by disabled buttons. Is there any way that i can move the spawned Disabled Buttons with Keylisteners of my keyboard, since i cant get it to work
public GuiP1() {
super();
Panel.setLayout(null);
this.add(Panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("BattleshipsP1");
this.setSize(640, 1000);
this.setVisible(false);
this.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent arg0) {
}
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("VK_RIGHT");
} else if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("VK_LEFT");
} else if (arg0.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("VK_UP");
} else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("VK_DOWN");
}
}
public void keyTyped(KeyEvent arg0) {
}
});
Grid();
this.setVisible(true);
}
It wont listen for keys beeing pressed
Thank you JFluX
Your KeyListeners are not working because the listened to component must be focusable and have focus for them to work. A kludge solution is to force the listened to component to have focus by
making the listened to component focusable via setFocusable(true)
By giving it the focus via requestFocusInWindow()
and by making added components non-focusable by calling setFocusable(false) on them.
A much better solution is to use Key Bindings which isn't tied as closely to component focus.

How to Override Key Repeat - KeyListener

I have a Graphics project that contains KeyListener, where the arrow keys and space bar controls the movement of a spaceship. However, when a key is held down, it registers the next key presses after a delay after the first. I would like for a held down key to register continuously.
I suspect that this is due to my computer's setting for key repeat delay, so is there any way to temporarily override this in my program, or a workaround for it?
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 38) { //Up Arrow
currentLevel.moveSpiffUp();
} else if (e.getKeyCode() == 40) { //Down Arrow
currentLevel.moveSpiffDown();
} else if (e.getKeyCode() == 32 && !currentLevel.getFrapRayActive()) {
currentLevel.shoot();
}
repaint();
}
I would suggest using key press and key released
public class MyKeyListener implements KeyListener {
//class gets the input from the player
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
if(KeyEvent.getKeyText(e.getKeyCode()).equals("Left")) {
//left arrow key
} else if (KeyEvent.getKeyText(e.getKeyCode()).equals("Right")) {
//right arrow key
}
}
public void keyReleased(KeyEvent e) {
if(KeyEvent.getKeyText(e.getKeyCode()).equals("Left"){
//left key released
}
if(KeyEvent.getKeyText(e.getKeyCode()).equals("Right")) {
//right key released
}
}
}
I then used this bit of code in my main constructor
main() {
KeyListener listener = new MyKeyListener();
addKeyListener(listener);
setFocusable(true);
}
This way you wont have your delay when hitting keys.

addKeyListener to Jpanel

I want to addKeyListener to a JPanel but when I press two keys at the same time, just one of them executes. What is the solution to this problem - press two keys at the same time?
I have a class that extends JPanel
this.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
upPlayer2();
} if (e.getKeyCode() == KeyEvent.VK_A ){
leftPlayer2();
}
}
});
Thanks.
Don't use a KeyListener. Swing was designed to be used with Key Bindings.
See Motion Using the Keyboard for more information and solutions.

keylistener in java not working

I want my java program to be running in the background by default, but use a keylistener to call my changewallpaper class. The changewallpaper class definently works, but the keylistener does not call the method. The keyevent will be changed later it's currently just for testing.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class listener implements KeyListener {
public static void main(String[] args){
}
#Override
public void keyReleased(KeyEvent arg0) {
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_UP) {
changewallpaper.main();
}
}
#Override
public void keyTyped(KeyEvent arg0) {
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_UP) {
changewallpaper.main();
}
}
#Override
public void keyPressed(KeyEvent arg0) {
int key = arg0.getKeyCode();
if (key == KeyEvent.VK_UP) {
changewallpaper.main();
}
}
}
A KeyListener does not listen to all keyboard events indiscriminately - it only listens to events on a particular Component, when that Component has keyboard focus. You have to attach the listener to something with an addKeyListener method or similar.
See the Java How to Write a Key Listener tutorial

Categories