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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:41:44+00:00 2026-06-08T12:41:44+00:00

Hi friends i’m trying to write a simple program that will move a little

  • 0

Hi friends i’m trying to write a simple program that will move a little square right,left,up, and down smoothly.

As is, the square will indeed move in the four directions but there is a noticeable delay when trying to change the direction of the square. I plan on eventually writing a little game similar to the ‘brick’ game but in order to do that my object has to move smoothly. Can someone give me some advice on how to achieve this?

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

public class FutureGame extends Applet implements KeyListener{

int deltax; 
int deltay;

public void init(){

    deltax = 0;
    deltay = 0;
    this.addKeyListener(this);
}

@Override
public void keyPressed(KeyEvent ke) {

    if(ke.getKeyCode() == ke.VK_RIGHT){
        deltax = deltax +10;
    }

    if(ke.getKeyCode() == ke.VK_LEFT){
        deltax = deltax - 10;
    }

    if(ke.getKeyCode() == ke.VK_UP){
        deltay = deltay - 10;
    }

    if(ke.getKeyCode() == ke.VK_DOWN){
        deltay = deltay +10;
    }
    this.repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

public void paint(Graphics g){

    g.drawRect(deltax, deltay, 10, 10);
}

}
  • 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-08T12:41:46+00:00Added an answer on June 8, 2026 at 12:41 pm

    If your thread(games’s main calculations) sleeping/waiting time is very small, your key-listeners could not catch your keystrokes within that small time. Make those thread waiting times bigger and try again. I mean the things in try-catch blocks.

    You are drawing directly. Use buffering/doublebuffering.

    Changing the buffer is much more faster than changing the contents of screen.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

friends i am trying to work with predesigned visual studio dotNet projects. so i
Friends please help. I know that using jdk1.7 we can get the last access
Friends, I've a datagridview with 5 columns and 10 fixed rows. That means I've
Friends, I am trying to processing a huge amount of audio files using a
Friends In, testing of Apple Push Notification Service ,server sends me response .k that
Friends I have a problem We need to make a user control that has
friends please help my java program crashes when run. I am using Jnotify to
friends, i am trying to delete row and then when i try to fetch
Friends i am a newbie in php and trying to learn it more and
Friends, i m trying add image to my Jbutton using seticon method but it

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.