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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:41:51+00:00 2026-06-07T16:41:51+00:00

I thought I would try and write a program that would paint a ball,

  • 0

I thought I would try and write a program that would paint a ball, and when the arrow keys are pressed, would move the ball around the screen in the direction pressed. First I started off to try and make a program that would just do “UP” arrow key motion.

I’ve looked around for a solution, and just can’t figure out what’s wrong with this code. I don’t know if it’s a problem with my input and action maps (i.e., a problem with the program recognizing key presses) or if it’s a problem with how the JComponent and JFrame classes work in swing. I thought maybe the problem might also be focus; I don’t really know how to tell when a component has focus. I think the key has been set to CNTRL+Y instead of up for now, just because at some point I thought it might be a problem with my string designating the up arrow in the input map maker.

At this point, I’m so frustrated I’m just trying to get the damn thing to do something, so I’m using more input maps than should be necessary.

the code is as follows, it’s pretty short, formatted horribly (sorry):

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

class BallMover
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                BallFrame frame = new BallFrame();
            }
        });

    }
}


class BallFrame extends JFrame
{
    private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_HEIGHT = 500;
    private BallComponent comp;

    public BallFrame()
    {
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setSize(this.DEFAULT_WIDTH, this.DEFAULT_HEIGHT);
        super.setResizable(false);
        super.add(new BallComponent());
        super.setVisible(true);
        super.setFocusable(true);
    }
}


class BallComponent extends JComponent
{
    private Ellipse2D.Double ellipse;
    private double x = 225;
    private double y = 225;
    private ActionPress actionPress;

    public BallComponent()
    {
        super();
        super.setFocusable(true);

        InputMap imap1 = this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        imap1.put(KeyStroke.getKeyStroke("ctrl Y"), "keyUp1");
        InputMap imap2 = this.getInputMap(JComponent.WHEN_FOCUSED);
        imap1.put(KeyStroke.getKeyStroke("ctrl Y"), "keyUp2");
        InputMap imap3 = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);   
        imap1.put(KeyStroke.getKeyStroke("ctrl Y"), "keyUp3");

        ActionMap amap = this.getActionMap();
        amap.put("keyUp1", actionPress);
        amap.put("keyUp2", actionPress);    
        amap.put("keyUp3", actionPress);
    }

    public void paintComponent(Graphics g)
    {
        super.repaint(); // clear component //
        Graphics2D g2d = (Graphics2D)g;
        this.ellipse = new Ellipse2D.Double(x, y, 50, 50);
        g2d.fill(this.ellipse);
    }

    private class ActionPress extends AbstractAction
    {
        public void actionPerformed(ActionEvent event)
        {
            y = y + 10;
            ellipse = new Ellipse2D.Double(x, y, 50, 50);
            repaint();
        }
    } 
}
  • 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-07T16:41:53+00:00Added an answer on June 7, 2026 at 4:41 pm

    It seems that you never initialized actionPress – try adding this to your BallComponent constructor:

    actionPress = new ActionPress();
    

    ie, your constructor would look like this

    public BallComponent()
    {
        super();
        super.setFocusable(true);
    
        InputMap imap1 = this.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        imap1.put(KeyStroke.getKeyStroke("ctrl Y"), "keyUp1");
        InputMap imap2 = this.getInputMap(JComponent.WHEN_FOCUSED);
        imap1.put(KeyStroke.getKeyStroke("ctrl Y"), "keyUp2");
        InputMap imap3 = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);   
        imap1.put(KeyStroke.getKeyStroke("ctrl Y"), "keyUp3");
    
        actionPress = new ActionPress();
        ActionMap amap = this.getActionMap();
        amap.put("keyUp1", actionPress);
        amap.put("keyUp2", actionPress);    
        amap.put("keyUp3", actionPress);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, I thought I would try one last update and see if it gets
It's possible to call .NET from MATLAB, so I thought I would try to
This should be so simple... I thought it would be nice to try the
I'm doing a book exercise that says to write a program that generates psuedorandom
I am a somewhat experienced Rails developer and I thought I would try out
I am working on a program that needs to do the following: Write a
I was a huge 960 grid system user, and I thought I would try
I thought I'd try a simple GUI app using the world/universe mutation-free approach, but
This is what I thought would be a simple select clause, however the following
I am looking to do something I thought would be fairly straight forward, Adding

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.