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 9027237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:42:30+00:00 2026-06-16T06:42:30+00:00

Background I am building a game library to make game development faster, and so

  • 0

Background
I am building a game library to make game development faster, and so far it is going fairly smoothly.

I have the following:

  • Main entry class public static void main(String[] args)
    • this class then initiates a class from my library called Game
    • then sets the window size and adds a Room to the JFrame
  • When an extendable Room (Room is a game level) class is created it it starts a thread from the room to draw all the objects added to it.
    • Objects can have events added to them (the part I am stuck on)

So above is what the main idea entails.

Here is the entry class (very simple).

package test;

import JGame.Game.Game;
import JGame.Room.Room;
import javax.swing.JFrame;
import test.Rooms.Room1;

public class Main extends JFrame{

    public static void main(String[] args){
        Game game = new Game("Test Game"); // This sets the window title
        game.start(800, 600); // This sets the size of the window

        Room room1 = new Room1();
        game.setRoom(room1);  // adds the JPanel to the main frame
    }
}

Room1 extends Room. Within Room1 all of the game object that are associated with that room are added to it.

public class Room extends JPanel implements Runnable{
    ArrayList<GameObject> gameObjects = new ArrayList<>();
    @Override
    public void run(){
        try{
            while(true){
                this.repaint();
                Thread.sleep(5);
            }
        }
    }
    public void addGameObjectAt(GameObject gameObject, int x, int y){
        // Sets private variables in GameObject
        // These are then grabbed in the paintComponent to draw at that location
        gameObject.setX(x); 
        gameObject.setY(y);
        gameObjects.add(gameObject);
    }
    @Override
    public void paintComponent(Graphics g){
        g.drawImage(bg, 0, 0, this);
        for(int i = 0; i < gameObjects.size(); i++){
            GameObject go = gameObjects.get(i);
            g.drawImage(go.getSprite(), go.getX(), go.getY(), this);
        }
    }
}

So lets say we create a room:

public class Room1 extends Room{
    public Room1(){
        this.createShip();
    }
    public void createShip(){
        Ship = new Ship();
        // Add an object to a list setting its x,y to 10,10
        this.addGameObjectAt(ship, 10, 10);
    }
}

Note: These objects are not added to the window with addGameObjectAt they are simply added to an ArrayList and then in the thread in Room are painted to the screen.

Now that we added Ship to the room it can be drawn on the screen using paintComponent(). This all works fine!

Here is where things start to stop working. Now that we have a Ship class added, I would like to add some key events, currently I have to add Main for them to work, but I don’t want to add them there, because it gets messy, I would like to add them to Ship, since that is what the events will effect in the end.

This code doesn’t attach the keylistener

// GameObject extends JPanel
public class Ship extends GameObject{
    public Ship(){
        this.addKeyListener(new AL());
    }

    public class AL extends KeyAdapter{

        @Override
        public void keyPressed(KeyEvent evt){
            System.out.println("here");
        }

        @Override
        public void keyReleased(KeyEvent evt){
        }
    }
}

This doesn’t work, pressing a key does not print out here, but if I move the AL class and the addKeyListener() to the Game class it works, but I don’t want it in the Game class I want it in the Ship class.

// This class just sets up the size of the application window
// It also holds an int list of all the game rooms
public class Game extends JFrame{

}

I have been trying to figure this out for at least a week now, and I can’t seem figure out how can I get this so that it works in the Ship class?

  • 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-06-16T06:42:31+00:00Added an answer on June 16, 2026 at 6:42 am

    JPanel is not a focusable component so cannot interact with KeyEvents.

    In Swing, the preferred approach is to use Key Bindings. You can map an Action to a KeyStroke even when a component doesn’t have focus.

    See this example

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

Sidebar

Related Questions

I'm currently building a game for the iPhone with cocos2d and have the following
A little background: We're building a library/framework for working with scientific models. We have
I'm building a 16 bit game for my final project and have completed all
i am new to LibGDX i'm building a game and i have an animated
I'm building a game for the iphone, and the main window displays an image
Background info: Me and a couple of friends are building this platform game in
I'm trying to learn python (with my VBA background) buy building a black-jack game
I am building a chess game and i have a loop that runs through
A bit of background info: I am building a geolocation based game for Android
I'm developing a rendering engine for a game i am currently building.. I have

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.