Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:03:19+00:00 2026-05-27T11:03:19+00:00

I am trying to make a game engine. I have made the Game class

  • 0

I am trying to make a game engine. I have made the Game class but the error resides in the KeyBoard class. Here I leave some code.

Class:: Game

package transfer2pc.co.cc.game.tileengine;

import java.awt.Graphics;
import java.util.HashMap;

import javax.swing.JPanel;

import transfer2pc.co.cc.game.tileengine.input.KeyBoard;

public abstract class Game extends JPanel implements Runnable {

   /**
    * 
    */
    private static final long serialVersionUID = 640206679500196209L;

    HashMap<String, ?> maps = null;

    KeyBoard keyBoard = null;

    public Game(){
        super();
        keyBoard = new KeyBoard(this);
        setKeyBoard(keyBoard);
        Thread th = new Thread(this);
        th.start();
    }

    public void run(){
        while(true){
            repaint();
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g){

    }

    public void addMap(){

    }

    public void setMap(){

    }

    public abstract void keyPressed(int code);

    public abstract void keyReleased(int code);

    public abstract void keyTyped(int code);

    public void setKeyBoard(KeyBoard key){
        addKeyListener(key);
    }

}

Class:: KeyBoard

package transfer2pc.co.cc.game.tileengine.input;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import transfer2pc.co.cc.game.tileengine.Game;

public class KeyBoard extends KeyAdapter implements KeyListener {

    Game game = null;

    public KeyBoard(Game gm){
        this.game = gm;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("KeyPressed");
        game.keyPressed(e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        game.keyReleased(e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
        game.keyTyped(e.getKeyCode());
    }

    public static char getChar(int key){
        return (char)key;
    }

}

Class:: KeyTest

package transfer2pc.co.cc.game.tileengine.test;

import java.awt.Graphics;

import javax.swing.JFrame;

import transfer2pc.co.cc.game.tileengine.Game;
import transfer2pc.co.cc.game.tileengine.input.KeyBoard;

public class KeyTest extends Game {

    /**
     * 
     */
    private static final long serialVersionUID = 8557676950779023327L;

    char pressed;

    public KeyTest(){
        super();
        addKeyListener(new KeyBoard(this));
    }

    @Override
    public void keyPressed(int code) {
        pressed = KeyBoard.getChar(code);
    }


    @Override
    public void keyReleased(int code) {

    }


    @Override
    public void keyTyped(int code) {

    }

    @Override
    public void paint(Graphics g){
        g.drawString("You pressed: "+pressed, 20, 20);
    }

    public static void main(String[] args){
        JFrame frame = new JFrame("KeyTest");
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new KeyTest());
        frame.setVisible(true);
    }

}

But the error was there was no exception thrown and the input isn’t being read. Could anybody say me the correct way of doing this..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:03:19+00:00Added an answer on May 27, 2026 at 11:03 am

    Simply, your panel needs to be focusable. Add in wherever you create the panel:

    panel.setFocusable(true);
    panel.requestFocusInWindow();
    

    Here’s a SSCCE (I suggest asking questions with one of these in the future):

    import java.awt.Dimension;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class SimpleKeyTest {
    
        public static void main(String[] args) {
    
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    JPanel panel = new JPanel();
    
                    frame.getContentPane().add(panel);
    
                    panel.addKeyListener(new KeyListener() {
    
                        @Override
                        public void keyTyped(KeyEvent e) {}
    
                        @Override
                        public void keyReleased(KeyEvent e) {}
    
                        @Override
                        public void keyPressed(KeyEvent e) {
                            System.out.println("Pressed " + e.getKeyChar());
                        }
                    });
    
                    panel.setFocusable(true);
                    panel.requestFocusInWindow();
    
                    frame.setSize(new Dimension(300, 300));
                    frame.setVisible(true);
                }
    
            };
    
            SwingUtilities.invokeLater(r);
    
        }
    }
    

    Also, https://www.google.com/search?q=jpanel+keylistener

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a game in pygame, but for some reason actor and
I'm trying to make an android game here, but I'm not sure what is
I am just messing around trying to make a game right now, but I
i usually develop for iPhone. But now trying to make a pong game in
I have been trying to make a Cross-platform 2D Online Game, and my maps
I'm trying to implement some sort of 'just-for-me' game engine and the problem's plot
I'm trying to make a game (using irrlicht engine with c++) where you can
I'm trying to make a game with Qt, but i can't load an image.
I am currently trying to make a game in Android. I have this piece
I have read a book and am trying to make a game of tic-tac-toe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.