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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:11:14+00:00 2026-06-13T13:11:14+00:00

I’m having a lot of trouble trying to make my code move a player

  • 0

I’m having a lot of trouble trying to make my code move a player (Ship) around a screen. I can get the planets to draw on the screen and the the player ship but I can not figure out how to implement the keyListener to at least print out something. Thank you in advance for all the help!

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Toolkit;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;


    public class MapPanel extends JPanel {
public static final int WIDTH = 25;
 public static final int HEIGHT = 20;
 int zone = 0;
 private int xValue;
 private int yValue;
 private Color color;
 public Planet[][] planetGrid = new Planet[WIDTH][HEIGHT];
 static Player currPlayer = new Player("h");
 static Universe universe = new Universe(currPlayer);

 /**
 * Create the panel.
 */
 public MapPanel(Universe univ, Player p) {
    this.universe = univ;
    currPlayer = p;
    int i = 0;
    this.setSize(new Dimension(450,450));
    setVisible( true );
    //this.addKeyListener(new KeyController());
     KeyController kc = new KeyController();
     this.addKeyListener(kc);
    repaint();


 }


 /**
  * Draw method to draw the playing field
  * @param g Graphics object
  * @param tileDimension dimension of the tile
  */
 public void draw(Graphics g)
 {
 universe.draw(g);

    //    KeyController key = new KeyController();
 }
 public void paintComponent(Graphics g) {
     super.paintComponent(g);
     draw(g);
 }
 public static void main(String[] args)
 {
     MapPanel mp = new MapPanel(universe, currPlayer);
     JFrame f = new JFrame();
     f.add(mp);
     f.setSize(new Dimension(450,450));
     f.setVisible(true);
     f.setFocusable(true);


 }
 private class KeyController implements KeyListener {
     public KeyController()
     {
         System.out.println("ghgh");
         setFocusable(true);
        // addKeyListener(this);
     }
        @Override
        public void keyPressed(final KeyEvent key) {
            System.out.println("fgfgf");
            if (currPlayer != null) {

                int oldX = currPlayer.getPosition().x;
                int oldY = currPlayer.getPosition().y;

                switch (key.getKeyCode()) {
                    case KeyEvent.VK_RIGHT:
                      currPlayer.setPosition(new Point(oldX+1, oldY)); //move right
                      System.out.println("RIGHT");
                        break;
                    case KeyEvent.VK_LEFT:
                          currPlayer.setPosition(new Point(oldX-1, oldY));         //move left
                        break;
                    case KeyEvent.VK_DOWN:
                          currPlayer.setPosition(new Point(oldX, oldY+1));         //move down
                        break;
                    case KeyEvent.VK_UP:
                          currPlayer.setPosition(new Point(oldX, oldY-1));         //move up
                        break;
                }

            }
            repaint();
        }
        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("ggg");
        }
        @Override
        public void keyTyped(KeyEvent e) {
            System.out.println("typeeeddd");
        }
    }





    }
  • 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-13T13:11:15+00:00Added an answer on June 13, 2026 at 1:11 pm

    KeyListener is not the most appropriate method to achieve your results.

    It would be better to use the key bindings API.

    Apart from simplifying the code, it will also allow you to provide better focus control over when the keys should triggered.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.