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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:59:17+00:00 2026-05-31T14:59:17+00:00

I am trying to move a circle left with a keyEvent. So far, the

  • 0

I am trying to move a circle left with a keyEvent. So far, the circle is drawn on the window but it does not move left! I feel like the problem is where I add the Window() constructor to the container. The is no output on the console to tell me that it is working. So I dont think it even reaches the KeyEvent class. Here is my code:

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


public class Window extends JPanel {

    private static Ellipse2D.Double circle;

    public Window() {
        super();
        int width = 400;
        int height = 400;
        circle = new Ellipse2D.Double(0.5 * width, 0.9 * height,
                0.1 * width, 0.05 * height);
        addKeyListener(new MoveCircle());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        Graphics2D brush = (Graphics2D) g;
        int width = getWidth();
        int height = getHeight();
        g.clearRect(0, 0, width, height);
        brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        brush.draw(circle);
    }

    public class MoveCircle implements KeyListener {

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("Working on top!");
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                System.out.println("Working on bottom!");
                circle.x++;
                repaint();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
        }
    }

    public static void main(String[] args) {
        Window window = new Window();
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        container.add(new Window());
        frame.addKeyEvent(window.new MoveCircle());
        frame.setSize(800, 700);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.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-05-31T14:59:19+00:00Added an answer on May 31, 2026 at 2:59 pm

    Actually what is happening is this, you are adding Window to the JFrame, but the focus lies with the JFrame, so when you type on your Keyboard that thing goes to the JFrame not the KeyListener attached to the Window class. So in order to get over it, you simply have to call requestFocusInWindow() on the Window class’s Object. Try this code, I had done some modification regarding EDT and stuff.

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.Ellipse2D;
    import javax.swing.*;
    
    
    public class Window extends JPanel {
    
        private static Ellipse2D.Double circle;
        private JFrame frame;
    
        public Window() {
            super();
            int width = 400;
            int height = 400;
            circle = new Ellipse2D.Double(0.5 * width, 0.9 * height,
                    0.1 * width, 0.05 * height);        
        }
    
        public Dimension getPreferredSize()
        {
            return (new Dimension(frame.getWidth(), frame.getHeight()));
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponents(g);
            Graphics2D brush = (Graphics2D) g;
            int width = getWidth();
            int height = getHeight();
            g.clearRect(0, 0, width, height);
            brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            brush.draw(circle);
        }
    
        public class MoveCircle implements KeyListener {
    
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("Working on top!");
                if (e.getKeyCode() == Event.ENTER) {
                    System.out.println("Working on bottom!");
                    double newX = circle.x - 1;
                    circle.x = newX;
                    repaint();
                }
            }
    
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
            }
    
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        }
    
        private void createAndDisplayGUI(Window window)
        {
            frame = new JFrame();
            Container container = frame.getContentPane();       
            container.add(window);
            window.addKeyListener(new MoveCircle());        
            frame.setSize(800, 700);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
            window.requestFocusInWindow();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    Window window = new Window();
                    window.createAndDisplayGUI(window);
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to move off of ActiveMQ but one feature we'd like to keep
I am trying to move a rectangle but I am not sure how to
I'm trying to move my game development into the third dimension, but I'm having
I'm trying to move some Excel-Data to MySQL, but having troubles with encoding. What
I'm trying move a window partially off the top of the screen. However, Windows'
Im trying to move a system from using morbid to rabbitmq, but I cannot
I'm trying to move the button under the textarea from js-code. But I can't
So I am trying to move this random jumble of two polygons, a circle,
I'm trying to move a fairly slow function off of the main thread, but
I'm trying to move my database.mdf file from a development environment to a SQL

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.