Answer the question
In order to leave comments, you need to log in
How to bind KeyListener to gui in java?
I want to make a snake in Java, but I have a problem with how to connect keyboard buttons and graphics movement. I'm already twisting these listeners this way and that, at some point it worked out for me, but for some reason when I restarted nothing happened again (or is it my glitches?). In general, you need to make the square move across the screen on the WDSA. Help me please.
packagecom.company;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Snake extends JComponent {
int x = 350;
int y = 350;
public MyPanel drawPanel;
JFrame frame;
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new MyPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(700, 700);
frame.setVisible(true);
drawPanel=new MyPanel();
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
System.out.print("start ");
}
public void moveRight(){
x++;
System.out.print(" method in pr slave ");
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveLeft(){
x--;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveDown(){
y++;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveUp(){
y--;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyPan extends JPanel {
Snake snake = new Snake();
Timer timer = new Timer(20,new MyKey());
public MyPan(){
timer.start();
addKeyListener(new MyKey());
setFocusable(true);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 700, 700);
g.setColor(Color.DARK_GRAY);
g.fillRect(snake.x, snake.y, 50, 50);
}
public class MyKey extends KeyAdapter implements ActionListener{
@Override
public void keyPressed(KeyEvent e) {
System.out.print(" react ");
if (e.getKeyCode() == KeyEvent.VK_D) {
snake.moveRight();
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_W) {
snake.moveUp();
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_A) {
snake.moveLeft();
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_S) {
snake.moveDown();
repaint();
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
}
package com.company;
public class Main {
public static void main(String[] args) {
Snake s = new Snake();
s.go();
}
}
Answer the question
In order to leave comments, you need to log in
probably add a lisener to the application window form itself, then the event will be processed in all cases when the application window is active.
package application;
public class Application {
public static Snake s;
public static void main(String[] args) {
s = new Snake();
s.go();
}
}
package application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import javax.swing.*;
import java.awt.event.KeyEvent;
public class Snake extends JComponent {
int x = 350;
int y = 350;
public MyPan drawPanel;
JFrame frame;
Timer timer = new Timer(100, new MyKey());
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel = new MyPan(this);
timer.start();
frame.getContentPane().add(drawPanel);
frame.setSize(700, 700);
frame.setVisible(true);
frame.setFocusable(true);
frame.setFocusTraversalKeysEnabled(false);
frame.addKeyListener(new MyKey());
frame.setFocusable(true);
}
public void moveRight() {
x += 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveLeft() {
x -= 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveDown() {
y += 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public void moveUp() {
y -= 10;
try {
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyKey extends KeyAdapter implements ActionListener {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
moveRight();
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
moveUp();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
moveLeft();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
moveDown();
}
}
@Override
public void actionPerformed(ActionEvent e) {
drawPanel.repaint();
}
}
}
package application;
import javax.swing.*;
import java.awt.*;
public class MyPan extends JPanel {
// Snake snake = new Snake();
private Snake s;
public MyPan(Snake ss) {
s = ss;
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 700, 700);
g.setColor(Color.GREEN);
g.fillRoundRect(s.x, s.y, 50, 50, 40, 40);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question