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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:33:50+00:00 2026-05-27T06:33:50+00:00

So the objective (well the starting point anyway) of this program is to press

  • 0

So the objective (well the starting point anyway) of this program is to press the arrow keys and have bullets come from the middle of the screen in the direction of the arrow. I have four images of bullets I created in paint and am using these two classes:

this is the class that creates and organizes the bullets:

public class BulletAnimator extends JPanel implements ActionListener
{
  int startX,startY;
  boolean yAxis = false;
  boolean xAxis = false;
  Timer animator;
  String direction;


  public BulletAnimator(int sX, int sY, String d)
  {
    direction = d;

    startX = sX;
    startY = sY;

    animator = new Timer (10, this);
    animator.start();
  }

  //chooses the right picture for the right direction
  public ImageIcon chooseBulletPicture ()
  {
    String path = "bullet";

    if (direction.equals("left"))
      path += "LEFT";
    else if (direction.equals ("right"))
      path += "RIGHT";
    else if (direction.equals ("up"))
      path += "UP";
    else
      path += "DOWN";

    path += ".png";

    ImageIcon b= new ImageIcon (path);

    return b;
  }

  public void paintComponent (Graphics g)
  {
    super. paintComponent(g);

    if (startX >= 500 || startY >= 500)
      animator.stop();

    if (direction.equals ("up"))
      startY -=2;
    else if (direction.equals ("down"))
      startY +=2;
    else if (direction.equals ("right"))
      startX += 2;
    else
      startX -=2;


    chooseBulletPicture().paintIcon (this, g, startX, startY);
   }

  public void actionPerformed(ActionEvent e)
  {
    repaint ();
  }
}

and this is the class is to add keyListeners and test it:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Test extends JFrame implements KeyListener
{
  JFrame f;

  public Test ()
  {
    addKeyListener (this);
    setFocusable (true);

    f = new JFrame ();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.setSize(500, 500);
  }

  public void keyPressed(KeyEvent e) 
  {
    BulletAnimator s = new BulletAnimator (250, 250, "initialized---blank");

    //creates bullet w/ correct direction
    if (e.getKeyCode() == KeyEvent.VK_RIGHT )
    {
      s = new BulletAnimator (250, 250, "right");
    } 
    else if (e.getKeyCode() == KeyEvent.VK_LEFT ) 
    {
       s = new BulletAnimator (250, 250, "left"); 
    } 
    else if (e.getKeyCode() == KeyEvent.VK_UP ) 
    {
       s = new BulletAnimator (250, 250, "up");
    } 
    else if (e.getKeyCode() == KeyEvent.VK_DOWN )
    {
       s = new BulletAnimator (250, 250, "down");
    }

    System.out.println ("keyPressed method read");//checks if keyPressed method was looked at

    f.add (s);
    repaint();
  }

  public void keyReleased (KeyEvent e)
  {}
  public void keyTyped (KeyEvent e)
  {}

  public static void main (String [] args)
  {
    Test t = new Test ();
  }
}

As you can see I tried to put in a test that says “keyPressed method read”… when the program runs it doesn’t print. In fact nothing at all happens its just a grey screen… quite frustrating really. Well thank you in advance if you took the time to look at it, I would greatly appreciate any advice!

  • 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-27T06:33:51+00:00Added an answer on May 27, 2026 at 6:33 am

    You are extending the JFrame class so that new Test() is creating an (extended) instance of a JFrame and then you are adding a KeyListener to this instance. However, the frame you are making visible is the one you created in the Test() constructor through

    f = new JFrame ();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.setSize(500, 500);
    

    You did not add your KeyListener to this instance of JFrame. So the KeyListener would only respond if the Test instance was visible and in focus.

    I would suggest doing this:

    public class Test extends JFrame implements KeyListener {
      JFrame f; // Remove this.
    
      public Test () {
        super();
        addKeyListener (this);
        setFocusable (true);
    
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(500, 500);
      }
    }
    

    To paint the Icons, you also need to modify BulletAnimator code:

    public void actionPerformed( ActionEvent e ) {
      repaint();
      revalidate();  // new line
    }
    

    And change f.add(s) to add(s) in keyPressed method to add the icons to the Test frame.

    Disclaimer: As others pointed out, there are better solutions to handling key presses. My suggestions above only identified the problems with your original code posted above.

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

Sidebar

Related Questions

This may well be a stupid question, but here it goes anyway. In objective
Well I have an application that uses both Objective C & c++ but for
i have a objective-c program, i added a little lib with linked list in
I've been programming objective-C for a few months now and have done pretty well
I have a problem finding out if two PolyLines intersect. Well the main objective
I have built a color well using objective c and cocoa but I am
I'm getting this warning all over the place in some perfectly well functioning objective-c
I am starting to program in Objective-C. I am using a book to learn
Well, I think the question is fairly objective. So, is there anyway to do
I'm new to objective c (moving from .NET developing to iPhone). Well Now 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.