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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:40:47+00:00 2026-06-12T21:40:47+00:00

Java novice here! I’m developing a small game that’s just simply moving around the

  • 0

Java novice here!

I’m developing a small game that’s just simply moving around the screen and will attack the little AI Rectangles i’m creating.

I have two GIF files pikachu.gif (yes, its a pokemon game), and pikready.gif.

I have implemented the KeyListener and everything and can get the Pikachu.gif to move around the frame with the arrow keys.

What i am wanting is, to change the image to the pikready.gif when I press down on the arrow keys, I have looked around online and can’t seem to find the answer.

Code is below, anything commented out is different stuff i’ve tried.

As i said im a Java Novice so go easy on me!

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class PikachuAttack extends JFrame implements Runnable{

    Graphics dbg;
    Image dbImage;
    Image Pik1;
    static ImageIcon active;

    int x, y, xDirection, yDirection;

    public void run(){
        try{
            while(true){
                move();
                Thread.sleep(10);
            }
        }catch(Exception e){
            System.out.println("Uh-oh, something went wrong!.");
        }
    }

    private void move() {
        x += xDirection;
        y += yDirection;

    }

     public void setXDirection(int xdir) {
            xDirection = xdir;
        }

        public void setYDirection(int ydir) {
            yDirection = ydir;
        }


    // KEY COMMANDS //
      public class AL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

                if(keyCode == e.VK_LEFT) {
                    setXDirection(-1);
                }
                if(keyCode == e.VK_RIGHT) {
                    setXDirection(+1);
                }
                if(keyCode == e.VK_UP) {    
                    setYDirection(-1);
                }
                if(keyCode == e.VK_DOWN) {
                    setYDirection(+1);
                }   
                /*(if(keyCode == e.VK_LEFT | keyCode == e.VK_RIGHT | keyCode == e.VK_UP | keyCode == e.VK_DOWN){
                    active = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikready.gif");
                }else{
                    active = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikachu.gif");
                } */

        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
                    if(keyCode== e.VK_LEFT){
                       setXDirection(0);                    
                    }
                    if(keyCode== e.VK_RIGHT){
                       setXDirection(0);                    
                    }
                    if(keyCode== e.VK_UP){
                        setYDirection(0);
                    }
                    if(keyCode== e.VK_DOWN){
                        setYDirection(0);                   
                    }

        }

        @Override
        public void keyTyped(KeyEvent e) {


        }

    }

    // CONSTRUCTOR //
    public PikachuAttack(){

        //Image Import
        ImageIcon still = new ImageIcon("C:\\Java\\GIFS\\Pikachu.gif");
        Pik1 = still.getImage();
        //ImageIcon ready = new ImageIcon("C:\\Users\\Neil\\workspace\\MyOwnTutorials\\bin\\game\\pikready.gif");
        //Pik2 = ready.getImage(); */
        // Pik1 = active.getImage();

        //JFrame properties
        addKeyListener(new AL());
        setTitle("Pikachu Attack");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        x = 15;
        y = 15;
    }



    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent(dbg);
        g.drawImage(dbImage, 0, 0, this);   

    }

    public void paintComponent(Graphics g){
        g.setColor(Color.red);
        g.fillRect(200, 190, 12, 20);
        g.drawImage(Pik1, x, y, this);
        g.setColor(Color.red);



        repaint();
    }


    public static void main(String[] args) {
        PikachuAttack game = new PikachuAttack();
        Thread t1 = new Thread(game);
        t1.start();
    }

}
  • 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-12T21:40:48+00:00Added an answer on June 12, 2026 at 9:40 pm

    Why not create a new reference to the current Image, say currentImage and on the DOWN key do:

    if (keyCode == e.VK_DOWN) {
       setYDirection(+1);
       currentImage = Pik2; // the ready image
    }   
    

    then in paintComponent, you could draw the current image:

    g.drawImage(currentImage, x, y, this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can Scala be used to write GWT applications? (NOTE: Java/Scala novice here...)
I am a novice to android and java development. Currently, I got a small
i'm just learning java, and i meet some problems. Here we have simple factory
Ok,so I'm a complete novice at programming and I just started coding in Java.
I am a novice programmer in (Java/C++/C#) and I also know Python. I am
I'm relatively novice when it comes to C++ as I was weened on Java
am working on a java(tomcat) app. that sometimes writes to stdout. But I notice
I am interested in having a bitmap icon that when clicked will open a
I'm designing a concurrent Java application that reads data from various medical devices available
I just installed NetBeans and the Android SDK following the instructions here . I

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.