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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:57:07+00:00 2026-06-03T02:57:07+00:00

Why is it that the ellipse does not respond correctly to user input? I

  • 0

Why is it that the ellipse does not respond correctly to user input?
I want the a-s-d-w to control Player1 and the arrow keys to control Player2.

Class Robot

//imports
...

public class Robot extends JComponent{

public Color color = null;
public int direction = 360;
public int xPos = 100;
public int yPos = 100;
public int xVel = 10;
public int yVel = 10;

Robot(){

}

public int getXpos(){
    return xPos;
}

public void setXpos(int x){
    this.xPos = x;
}

public int getYpos(){
    return yPos;
}

public void setYpos(int y){
    this.yPos = y;
}

// Same thing for getXvel, setXvel, getYvel, and setYvel

public void move(){
    switch(this.direction){

    case 0: 
        this.yPos -= this.yVel;
        break;

    case 90:
        this.xPos+= this.xVel;
        break;

    case 180:
        this.yPos += this.xVel;
        break;

    case 270:
        this.xPos -= this.xVel;
        break;

    default:
        System.out.println("hi");
        break;

    }
}
}

Class Player1 and Player2

public class Player1 extends Robot{

Player1(){
xPos = 300;
yPos = 300;
xVel = 10;
yVel = 10;
direction = 0;
Color = Color.WHITE;
}

public void paintComponent(Graphics g){ 
    Graphics2D g2 = (Graphics2D)g;

            Ellipse2D.Double ball = new Ellipse2D.Double(xPos, yPos, 100, 100);
    g2.setPaint(Color);
    g2.draw(ball);
    g2.fill(ball);


    }       
}

// Class Player2 is the same exact copy. The xPos = 500; yPos = 500; xVel = 10; yVel = 10;

Class KeyController

public class KeyController implements KeyListener{

Player1 player1 = new Player1();
Player2 player2 = new Player2();

KeyController(){

}

public void keyPressed(KeyEvent e) {

    // Player 1 Left

    if(e.getKeyCode() == KeyEvent.VK_A){
        player1.setDirection(270);
    }

    // Player 1 Down

    if(e.getKeyCode() == KeyEvent.VK_S){
        player1.setDirection(180);
    }

...

if(e.getKeyCode() == KeyEvent.VK_LEFT){

        player2.setDirection(270);

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

The Main Class: SpaceGame

public class SpaceGame extends JFrame implements Runnable{

Player1 player1 = new Player1();
Player2 player2 = new Player2();
KeyController key = new KeyController();
Thread animator;
int delay = 1000;
int frame = 0;
public static final int DEFAULT_WIDTH = 1280;
public static final int DEFAULT_HEIGHT = 740;

SpaceGame(){

    // Making the frame
    animator = new Thread(this);
    animator.start();
    setTitle("Game");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
    setBackground(Color.GREEN);
    addKeyListener(key);
    add(player1);
    add(player2);
}

public void run(){

        while (animator != null) 
        {
            repaint();

            player1.move();
player1.setDirection(360); // 360 does nothing
            player2.move();
player2.setDirection(360); // 360 does nothing
            try  
            {  
                Thread.sleep(delay);  
            }  
            catch (InterruptedException e)  
            {  
                break;  
            }  
            frame++; 
            System.out.println(frame);
        }
}

    public void paint(Graphics g){

        Graphics2D g2 = (Graphics2D)g;
        super.paintComponents(g);
        player1.paintComponent(g2);
        player2.paintComponent(g2);

        }


    public static void main(String[] args) {

        SpaceGame game = new SpaceGame();
        game.setVisible(true);

        }

}
  • 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-03T02:57:09+00:00Added an answer on June 3, 2026 at 2:57 am

    As noted in the Key Listener API, “To fire keyboard events, a component must have the keyboard focus”. Use Key Bindings instead, as shown here.

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

Sidebar

Related Questions

I have a DepencencyProperty (a boolean) that works fine on an Ellipse, but not
I noticed that my Eclipse does not include JSP Editor. Does standard JSP editor
I am using DeferredTextImpl Class and eclipse does not seems to complain about it
I just created a web service client using axis and eclipse that does not
I have drawing program that the user can draw either an ellipse or a
I know that google maps provide API for drawing circles, polygons but not ellipse.
Does anyone know a link to a set of color profiles for Eclipse that
Does anyone have any recommendations for tools that you can add to Eclipse, Netbeans
I'm completely mystified. I do not understand why Eclipse does not show me my
My understanding is that Processing.js converts Processing code into javascript. But if one does

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.