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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:03:32+00:00 2026-06-01T23:03:32+00:00

I have begun to write a simple platform game in java. As a test,

  • 0

I have begun to write a simple platform game in java. As a test, I wrote this simple
program that moves a rectangle around the applet when you press the arrow keys. The key events have not been firing at all. Here’s the code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends Applet implements Runnable, KeyListener
{
  //setup data
  Thread t;
  Image buffimg;
  Graphics draw;
  Dimension dim;

  //game variables
  int charx = 400;//rectangles X and Y positions
  int chary = 50;
  boolean leftArrow = false;
  public void init()
  {
        setSize(800, 500);
        t = new Thread(this);
        t.start();
       addKeyListener( this );
  }
  public void run()
  {
        while(true)
        {
          repaint();
          moveChar();//move the rectangle
          try {
                t.sleep(1000/30);
          } catch (InterruptedException e) { ; }
        }
  }
   public void keyPressed( KeyEvent e ) 
   { 
       int k = e.getKeyCode();
       if(k == 37)
       {
           leftArrow = true;
           charx--;
       }

   }
   public void keyReleased( KeyEvent e ) 
   { 
       if(e.getKeyCode() == 37)
       {
           leftArrow = false;
       }
   }
   public void keyTyped( KeyEvent e ) 
   {
   }
  public void moveChar()
  {
      //move rectangle on left arrow key press
      if(leftArrow == true)
      {
          charx--;
      }  
  }
  public void paint(Graphics g)
  {
      g.drawRect(charx, chary, 100, 100);
  }
  public void update (Graphics g)
  {
      //double buffering

      // initialize buffer
      if (buffimg == null)
      {
          buffimg = createImage (this.getSize().width, this.getSize().height);
          draw = buffimg.getGraphics ();
      }
      // clear screen in background
      draw.setColor (getBackground ());
      draw.fillRect (0, 0, this.getSize().width, this.getSize().height);
      // draw elements in background
      draw.setColor (getForeground());
      paint (draw);
      // draw image on the screen
      g.drawImage (buffimg, 0, 0, this);
  } 
}

Why aren’t they firing and how should I fix 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-06-01T23:03:32+00:00Added an answer on June 1, 2026 at 11:03 pm
    this.requestFocusInWindow(); // end of init(), or better, in start()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Studing STL I have written a a simple program to test functors and modifiers.
I'm new to Java so I have a simple question that I don't know
I've just finished reading 'Java for Dummies' and have begun to create a simple
Im trying to write a simple program that takes 5 images and allows you
I am trying to write a C++ program that works like the game 24.
Please have a look at this page www.pixeli.ca/issue. I have begun making a page
I have recently begun delving into the world that is database programming with C++
I'm about to have to write a web page/app that will serve the agenda
I am trying to write simple mysql stored procedure and it seems that I
I have made a simple, but relatively computationally complex, UDF that queries a rarely

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.