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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:45:39+00:00 2026-05-23T04:45:39+00:00

This is my code.I am trying to make the ball move using arrow keys.

  • 0

This is my code.I am trying to make the ball move using arrow keys.
As i run the above program ball is not displayed (if i change the coordinates to like 0,30 ball is displayed)
and event is not fired ,ball neither moves nor jumps

What is the problem?

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

class ControlledBall extends JPanel{

int diameter = 30;
int height = 30;
int x_Pos = 0;
int y_Pos;

ControlledBall() {
  JFrame fr = new JFrame("Controlled Ball");
  this.setBackground(Color.black);
  fr.add(this);
  fr.setVisible(true);
  fr.setSize(600,400);
  y_Pos = this.getHeight() - diameter ;
  register();  
  repaint();
}

public void register() {
  this.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent ke) {
       if( ke.getKeyCode() == ke.VK_RIGHT ) {
           increment();
       }
   else  if( ke.getKeyCode() == ke.VK_LEFT ) {
           decrement();
       }
       else if( ke.getKeyCode() == ke.VK_UP) {
           jump();
       }
    }
 });
}

public void paintComponent(Graphics g) {
 super.paintComponent(g);
 g.setColor( Color.blue );
 g.fillOval( x_Pos , y_Pos , diameter , height );
 System.out.println("testing...");
}  

public void increment() {
 x_Pos++;
  if( x_Pos > (this.getWidth() - diameter) ) {
    x_Pos = this.getWidth() - diameter;
  }
 repaint();
}

public void decrement() {
 x_Pos--;
  if( x_Pos <= 0) {
    x_Pos = 0;
  }
 repaint();
}

public void jump() {
 y_Pos++;
  if( y_Pos <=0 ) {
   y_Pos = 0;
 }
repaint();
}

  public static void main( String args[] ) {
    new ControlledBall();
  }
}
  • 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-23T04:45:40+00:00Added an answer on May 23, 2026 at 4:45 am
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class ControlledBall extends JPanel{
    
    int diameter = 30;
    int height = 30;
    int x_Pos = 0;
    int y_Pos;
    
    ControlledBall() {
      JFrame fr = new JFrame("Controlled Ball");
      fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setBackground(Color.red);    
      setPreferredSize(new Dimension(400,300));
      fr.add(this);
      fr.setVisible(true);
      // important!
      fr.pack();
      //fr.setSize(600,400);
      y_Pos = this.getHeight()/2;
      register();
      repaint();
    }
    
    public void register() {
        // very important!
        setFocusable(true);
      this.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent ke) {
           if( ke.getKeyCode() == ke.VK_RIGHT ) {
               increment();
           }
       else  if( ke.getKeyCode() == ke.VK_LEFT ) {
               decrement();
           }
           else if( ke.getKeyCode() == ke.VK_UP) {
               jump();
           }
        }
     });
    }
    
    public void paintComponent(Graphics g) {
     super.paintComponent(g);
     g.setColor( Color.orange );
     g.fillOval( x_Pos , y_Pos , diameter , height );
    }
    
    public void increment() {
     x_Pos++;
      if( x_Pos > (this.getWidth() - diameter) ) {
        x_Pos = this.getWidth() - diameter;
      }
     repaint();
    }
    
    public void decrement() {
     x_Pos--;
      if( x_Pos <= 0) {
        x_Pos = 0;
      }
     repaint();
    }
    
    public void jump() {
     y_Pos++;
      if( y_Pos <=0 ) {
       y_Pos = 0;
     }
    repaint();
    }
    
      public static void main( String args[] ) {
        new ControlledBall();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<body onload=document.getElementById('menuTest').style.removeAttribute('display')> This is the code I'm using. Basically I'm trying to make an
i am trying this code for make a validation for a value. (regex from
To be specific, I was trying this code: package hello; public class Hello {
i am trying to use this code to bind my asp.net menu control to
I'm trying to use this code to replace spaces with _, it works for
I am currently trying out this code: NSString *path = [[NSBundle mainBundle] pathForResource:@dream ofType:@m4a];
I've been trying to get this code to work for hours! All I need
I'm trying to compile a POCO with this code public class MenuItem { public
I was trying to understand something with pointers, so I wrote this code: #include
hI, I'm trying to get this code from Larry Nyhoff's book to compile in

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.