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

The Archive Base Latest Questions

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

I have a specialized frame class and a specialized panel class. I have a

  • 0

I have a specialized frame class and a specialized panel class. I have a keylistener setup to listen for key presses of the “right” key (->)and then I call repaint but nothing happens when I press the “right” key. I believe my keylistener is done properly so I think it must be the way I call repaint(); Let me know how to fix this please.

Edit* I would like to add that I made x and y in CircleFrame static because I couldn’t find a way to call the incX() and incY() methods I had written. This was because, originally, the creation of the panel was done in the constructor for CircleBox. I took that out and put it in main in order to have a way to call repaint() for the panel but that didn’t work. I feel like theres some vital step im missing here.

package circlebox;

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

public class CircleBox extends JFrame implements KeyListener {
    CircleBox() {

    }

    public void keyPressed(KeyEvent e) {
        int location = e.getKeyLocation();
        if(location == KeyEvent.KEY_LOCATION_NUMPAD) {
            CircleFrame.y += 1;
            repaint();
        }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

    }

    public static void main(String[] args) {
        CircleBox frame = new CircleBox();
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        CircleFrame frame2 = new CircleFrame();
        frame.setLayout(new GridLayout(1,1));
        frame.add(frame2);

        frame.setVisible(true);
    }
}

class CircleFrame extends JPanel {
    static int x = 20;
    static int y = 20;
    int radius = 20;

    CircleFrame() {
        setSize(400,400);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawOval(x,y,radius,radius);
    }

    public void incX(int inc) {
        x += inc;
    }

    public void incY(int inc) {
        y += inc;
    }

    public int checkCollisions() {
        if(x <= 0) {
            return 0;
        }
        else if(y <= 0) {
            return 1;
        }
        else if(x >= 400) {
            return 2;
        }
        else if(y >= 400) {
            return 3;
        }
        else {
            return -1;
        }
    }
}
  • 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:30:23+00:00Added an answer on June 13, 2026 at 1:30 pm

    Your repaint is working just fine. The problem is with the key listener.

    You first need to actually register your key listener. So, add this line in the CircleBox constructor:

    addKeyListener(this);
    

    Then, you must handle the keyPressed correctly:

    public void keyPressed(KeyEvent e) {
        // TODO: your are actually moving the circle down, so it should be VK_DOWN
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            CircleFrame.y += 1;
            repaint();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a specialized object class that sends messages to its components so that
Have looked so long for a library specialized in dealing with iPhone Accelerometer but
I try to have a partially specialized template class inheriting from another template class.
I have a need for specialized collection classes. Let's call them FooItems and BarItems.
I have a helper class used to provide partially specialized versions of methods to
I have a specialized list that holds items of type IThing : public class
I have a specialized class, BusinessObjectList, with this declaration: public class BusinessObjectList<T> : List<BusinessObject>
For work I have written a specialized HTTP server which only performs 301/302/Frame redirections
I have a class with a field that is specialized and is using a
I have an entity and specialized class which is derived from the entity. Is

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.