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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:13:35+00:00 2026-05-28T06:13:35+00:00

Ok, how to i move from keyboard a ball using an Applet? I have

  • 0

Ok, how to i move from keyboard a ball using an Applet?

I have so far this code, that don’t do anything.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KeyboardGame extends Applet implements KeyListener
{

    private static final long serialVersionUID = 1L;
    private static boolean keyboadrRightPressed = false;

    public void init()
    {
         addKeyListener(this);
    }

    public void keyPressed(KeyEvent e) 
    {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_RIGHT)
        {
            keyboadrRightPressed = true;
        }
        else
        {
            keyboadrRightPressed = false;
        }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {
    }

    public void paint(Graphics g)
    {
        g.fillOval(20,20,20,20);
        g.drawString("String :"+keyboadrRightPressed,20,30);
    }

}

And also i have to understand how it works. I don’t get why my action listener won’t work, do i need an

while(true)

or an Thread?

  • 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-28T06:13:35+00:00Added an answer on May 28, 2026 at 6:13 am

    Your action listener might actually be working fine, but you need to repaint the applet when the key is pressed so that your string actually appears. Try changing the keyPressed to this:

    public void keyPressed(KeyEvent e) 
    {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_RIGHT)
        {
            keyboadrRightPressed = true;
        }
        else
        {
            keyboadrRightPressed = false;
        }
        repaint();
    }
    

    Actually moving the ball will differ depending on how you want the ball to actually move. I’m guessing you want it to continue moving right while the key is held down, so what I would do is implement a timer or some other form of thread that every .25 seconds (or however long you want) checks the keyboardRightPressed and will move the ball right if it is true. Then in the keyReleased portion of your code you should also add logic to set keyboardRightPressed back to false when you let up on the key.

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

Sidebar

Related Questions

I have this issue using assembly MIPS. From the keyboard I read an integer
We are considering a move from SVN to Mercurial, and have encountered a stumbling
Customer wants to move from SQL server to Sybase database. Don't know if LINQ
In my project I'm currently preparing a step-by-step move from legacy code to new,
This is linked to my other question when to move from a spreadsheet to
I wanted to move my image using keyboard arrow keys. when I pressed the
I've found some code that helps me resign the keyboard when a user touches
I have 3 selects that I am enabling keyboard navigation(right and left arrow keys)
I currently have an NSTableView inside an NSScrollView and this gets it's contents from
I have converted C++ source from using GLUT to using SDL/OpenGL The problem is

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.