Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

java - KeyListener not working (requestFocus not fixing it)

I am trying to make my first game without the help a youtube video. I have watched many youtube series about making java games and they never where what I wanted to do, so after learning enough I started making my own game. I know enough Java to get around and so far everything has worked with no issues.

I have tried searching for some help but all I could find where people saying the try something along the lines of:

setFocusable(true);
requestFocus();

This however has not worked for me. I know that most people would not like to just look through all my code but I think it will be manageable (Its not that much code yet).

I plan on making it a sort of snake like game where you get bigger instead of longer and you are a guinea pig (represented by the orange rectangle for now) instead of a snake.

Game.java:

package com.kaperly.game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame {
    private static final long serialVersionUID = 1L;

    public static JFrame frame;
    public static JPanel panel;

    public static int width = 1000;
    public static int height = 600;

    public Game() {
        frame = new JFrame();
        panel = new Panel();

        frame.setPreferredSize(new Dimension(width, height));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Guinea Pig Eat");
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
        panel.setFocusable(true);
        panel.requestFocus(true);

        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
    }

    public static void init() {

    }

    public static void loop() throws InterruptedException { while(true) {
        Character.move();

        Thread.sleep(10);
    }}

    public static void main(String[] args) throws InterruptedException, IOException {
        Game game = new Game();

        init();
        loop();
    }
}

Panel.java:

package com.kaperly.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Panel extends JPanel implements KeyListener {
    private static final long serialVersionUID = 1L;

    public static Image startButton;

    public static boolean goingRight = false;
    public static boolean goingLeft = false;
    public static boolean goingUp = false;
    public static boolean goingDown = false;

    private static int changeDirection = 0;

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W) {
            goingUp = true;
            changeDirection = 1;
            System.out.println("Moved Up!");

        }else if(e.getKeyCode() == KeyEvent.VK_A) {
            goingLeft = true;
            changeDirection = 2;

        }else if(e.getKeyCode() == KeyEvent.VK_S) {
            goingDown = true;
            changeDirection = 3;

        }else if(e.getKeyCode() == KeyEvent.VK_D) {
            goingRight = true;
            changeDirection = 4;

        }else if(changeDirection == (1 | 2 | 3 | 4)) {
            changeDirection = 0;
            if(changeDirection == 1) {
                goingLeft = false;
                goingDown = false;
                goingRight = false;

            }else if(changeDirection == 2) {
                goingUp = false;
                goingDown = false;
                goingRight = false;

            }else if(changeDirection == 3) {
                goingUp = false;
                goingLeft = false;
                goingRight = false;

            }else if(changeDirection == 4) {
                goingUp = false;
                goingLeft = false;
                goingDown = false;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        //Set background color
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, Game.width, Game.height);


        //Make Walls
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, Game.width, 25);
        g.fillRect(0, 550, Game.width, 25);
        g.fillRect(0, 0, 25, Game.height);
        g.fillRect(970, 0, 25, Game.height);

        //Show Character
        g.setColor(Color.ORANGE);
        g.fillRect(Character.x, Character.y, Character.width, Character.height);

        repaint();
    }
}

Character.java:

package com.kaperly.game;

public class Character {

    public static int x = 475;
    public static int y = 250;

    public static int width = 50;
    public static int height = 75;

    public static void move() {
        if(Panel.goingUp == true) {
            y = y ++;
        }
        if(Panel.goingLeft == true) {
            x = x --;
        }
        if(Panel.goingDown == true) {
            y = y --;
        }
        if(Panel.goingRight == true) {
            x = x ++;
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

KeyListener should be avoid BECAUSE of it's inherent focus related issues. You never know what might take keyboard focus away from your component.

Instead, take advantage of the Key Bindings API, which allows you to control the focus level required to trigger your key events.

It will also allow you to reuse underlying Action associated with the key binding in other places...

You'd be wise to rename you class Panel to something more meaningful. AWT already has a Panel class and it would be very easy to confuse the two


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...