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

  • SEARCH
  • Home
  • 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 9081935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:26:13+00:00 2026-06-16T20:26:13+00:00

Im working on making a simple Java game and had the great idea of

  • 0

Im working on making a simple Java game and had the great idea of separating my input handling to a separate class then the main game. I am having trouble getting my InputHandler class to actually receive input.

Main Game Class (DrawPanel.java)

package com.eriksaulnier.DesignedToFail;

import java.awt.*;
import java.awt.image.*;
import com.eriksaulnier.DesignedToFail.InputHandler;

import javax.swing.*;

public class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
BufferedImage buffer;
InputHandler inputHandler;
Entity player;
Entity enemy;
public boolean spawnBullet = false;

public DrawPanel () {
    setIgnoreRepaint(true);
    setVisible(true);
    setFocusable(true);
    addKeyListener(inputHandler);
    addMouseListener(inputHandler);
    new InputHandler();
}

public void initialize() {
    buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
    player = new Entity(370, 270);
    enemy = new Entity(100, 100);
}

public void update() {
    player.move();
}

public void checkCollisions() {
    if (player.getBounds().intersects(enemy.getBounds()))
        player.collision = true;
    else
        player.collision = false;
}

public void drawBuffer() {
    Graphics2D b = buffer.createGraphics();
    b.setColor(Color.white);
    b.fillRect(0, 0, 800, 600);
    if (player.collision == false) {
        b.setColor(Color.blue);
        b.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight());
        b.setColor(Color.red);
        b.fillRect(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
        b.dispose();
    }
    else {
        b.setColor(Color.black);
        b.drawString("Collision!", 350, 300);
        b.dispose();
    }
}

public void drawScreen() {
    Graphics2D g = (Graphics2D)this.getGraphics();
    g.drawImage(buffer, 0, 0, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void startGame() {
    initialize();
    while(true) {
        try {
            update();
            checkCollisions();
            drawBuffer();
            drawScreen();
            Thread.sleep(15);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

}

}

InputHandler (InputHandler.java)

package com.eriksaulnier.DesignedToFail;

import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class InputHandler extends JPanel implements KeyListener, MouseListener {
public boolean isShooting = false;
Entity player;

public InputHandler () {
    System.out.println("Listener Works!");
}

public void mouseClicked(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {
    int button = e.getButton();
    if (button == MouseEvent.BUTTON1)
        isShooting = true;
        System.out.println("Shooting!");
}

public void mouseReleased(MouseEvent e) {
    int button = e.getButton();
    if (button == MouseEvent.BUTTON1)
        isShooting = false;
        System.out.println("Not Shooting!");
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_W)
        player.up = true;
    if (key == KeyEvent.VK_S)
        player.down = true;
    if (key == KeyEvent.VK_A)
        player.left = true;
    if (key == KeyEvent.VK_D)
        player.right = true;
}

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_W)
        player.up = false;
    if (key == KeyEvent.VK_S)
        player.down = false;
    if (key == KeyEvent.VK_A)
        player.left = false;
    if (key == KeyEvent.VK_D)
        player.right = false;
}

}
  • 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-16T20:26:14+00:00Added an answer on June 16, 2026 at 8:26 pm

    You seem to be passing null handlers into your GUI. Where for instance do you instantiate the inputHandler variable before using it?

    For example:

    public class DrawPanel extends JPanel {
      //...
    
      InputHandler inputHandler; // here you declare the variable 
    
      //...
    
      public DrawPanel () {
        setIgnoreRepaint(true); // why this line?
        setVisible(true); // not needed in a JPanel's code
        setFocusable(true);
        addKeyListener(inputHandler); // here you use a null variable
        addMouseListener(inputHandler); // ditto, here you use a null variable
        new InputHandler(); // I don't know what you're doing here
      }
    

    In the code above I don’t see anywhere you have inputHandler = new InputHandler() before using it. Your line where you appear to create a new InputHandler, you don’t assign the object to any variable or use it, and so it seems a futile line of code, hence my comment on how I’m not sure what that line is supposed to achieve. Note that these problems have nothing to do with Swing and all to do with basic core Java.

    Also:

    • Your handler classes should not extend Swing components such as JPanels. They should implement listener interfaces only.
    • You should avoid use of KeyListeners with Swing GUI’s and instead use Key Bindings if possible. Please Google for and check the Key Bindings tutorial for more on this.
    • The use of handlers is well explained in the Oracle Swing tutorials. Again, please Google these and study them. I can attest to their usefulness as they are where I learned my Swing coding.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple game in Java using swing and am having problems with
I am working on making a simple drawing program. So far I am able
I'm working on making an ASCII based game, and everywhere I look people are
I'm working on making a simple simulation for my website, and I've looked around
I'm working on a simple painting application in Java but Swing doesn't want to
I am making a simple Java Swing GUI chessboard where the player can drag
I'm working on creating a simple 3D rendering engine in Java. I've messed about
I am working on a simple XML parser in Java. I have been using
Hi i'm working on just making a simple list i just want an icon
I'm making a simple form in Node.js. Everything else seems to be working correctly,

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.