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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:11:56+00:00 2026-06-10T00:11:56+00:00

The program detects left and right arrow keys. If any of those keys are

  • 0

The program detects left and right arrow keys. If any of those keys are pressed, my Knight object which is an instance of GImage class goes through an array of strings(those strings are directory path to my animation of the Knight object.

The problem with my code is that I cannot seem to find a proper fluid animation with the java code that I came up with. When I press the arrow keys, the animations loops quick and then it slows down but when I lift the arrow key and press again, it animation still loops quick and then it slows down.

Each movement animation has 4 frames of animation.

Here is a reference to the library I am using
http://jtf.acm.org/javadoc/student/acm/graphics/GObject.html#pause%28double%29

Any tips is appreciated.

import java.awt.event.KeyEvent;

import acm.graphics.GImage;

import acm.program.GraphicsProgram;



public class Castlevania extends GraphicsProgram {



    public void init(){
        setGameBackground();
        knight = new Knight("knight anim/knightFaceLeft/knight1.png", 500, 500);
        add(knight);
        setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        addKeyListeners();


    }


    private void setGameBackground() {
        // TODO Auto-generated method stub
        gameBackgroundImage = new GImage("link sprites/zelda_background.png");
        add(gameBackgroundImage);
    }



    public void keyPressed(KeyEvent e){ 

        /* Knight's Movement
         * 
         */
        int knightMovementKey = e.getKeyCode();
        if(knightMovementKey == KeyEvent.VK_LEFT)
        {

            this.xSpeed = 10;
            ySpeed = 0;


            knight.setImage(KnightFaceLeft[knightFrame]);
            knight.move(-xSpeed, ySpeed);

            knightFrame ++; 

            if(knightFrame >= KnightFaceLeft.length)
            {
                knightFrame = 0;
            }


        }

        else if(knightMovementKey == KeyEvent.VK_RIGHT){

            knight.setImage(KnightFaceRight[knightFrame]);
            knight.move(xSpeed,ySpeed);
            knightFrame++;

            this.xSpeed = 10;
            ySpeed = 0;

            if(knightFrame>=KnightFaceRight.length){

                knightFrame = 0;
            }

        }
        knight.pause(DELAY);
    }

    private String[] KnightFaceLeft = {"knight anim/knightFaceLeft/knight1.png","knight anim/knightFaceLeft/knight2.png","knight anim/knightFaceLeft/knight3.png","knight anim/knightFaceLeft/knight4.png"}; //Add in as many images as you want for your animation
    private String[] KnightFaceRight = {"knight anim/knightFaceRight/knight1.png","knight anim/knightFaceRight/knight2.png","knight anim/knightFaceRight/knight3.png","knight anim/knightFaceRight/knight4.png"}; 
    private GImage gameBackgroundImage;
    private Knight knight;
    private final int APPLICATION_WIDTH = 1200;
    private final int APPLICATION_HEIGHT = 800;
    private int knightFrame = 0;
    private int xSpeed ; //the number of pixels to move in x
    private int ySpeed = 0; //0 so you only move horiontally
    private double DELAY = 35;
}

import acm.graphics.GImage.*;

public class Knight extends GImage {

                public Knight(String imageDirectory, double knight_Location_XCoord, double knight_Location_YCoord) {
                    super(imageDirectory,knight_Location_XCoord, knight_Location_YCoord);
                }
            }
  • 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-10T00:11:58+00:00Added an answer on June 10, 2026 at 12:11 am

    Your call to pause() is blocking the event dispatch thread. In this context, use acm.util.SwingTimer to pace the animation in the timer’s ActionListener.

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

Sidebar

Related Questions

Is there any method for C# XNA games which detects if the user clicked
I have a C# program that detects incoming TCP/IP packets on any given ethernet
I made a program that sends out data to my arduino which detects what
I am trying to write an event-based c/c++ program which detects that how many
I'm trying to make a program that, using pointers, detects lines in an image
edit: VCL has no problem around right dragging and the sample program below works
On Win32, I wonder how to detect whether Left Shift or Right ALT is
I have a program that detects when certain machines are online and creates a
I have a script which detects OS using Catalina.bat for windows and Catalina.sh for
I am trying to build some program that detects if a server of some

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.