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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:12:13+00:00 2026-06-11T20:12:13+00:00

I am really new to Java and I need a circle to move around

  • 0

I am really new to Java and I need a circle to move around a JFrame when it’s clicked, but the circle has to get random coordinates. So far this code generates a new circle every time it’s clicked, but all the other circles stay there as well. I only need one circle to move around the frame. So maybe someone can help me a little 🙂

Here is my code:

public class test2 extends JFrame implements MouseListener {
int height, width;
public test2() {
    this.setTitle("Click");
    this.setSize(400,400);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    addMouseListener(this);
    width = getSize().width;
    height = getSize().height;
}

public void paint (Graphics g) {
    setBackground (Color.red);
    g.setColor(Color.yellow);
    int a, b;
    a = -50 + (int)(Math.random()*(width+40));
    b = (int)(Math.random()*(height+20));
    g.fillOval(a, b, 130, 110);
}

    public void mouseClicked(MouseEvent e) {
    int a, b;
    a = -50 + (int)(Math.random()*(width+40));
    b = (int)(Math.random()*(height+20));
    repaint();
}

public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}

public static void main(String arg[]){

    new test2();
}

}
  • 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-11T20:12:14+00:00Added an answer on June 11, 2026 at 8:12 pm

    See if this helps, here i have filled the entire rect with background color before drawing the circle. Though not efficient but serves the purpose

    replace the paint method as follows

    public void paint (Graphics g) {
            setBackground (Color.red);
            g.setColor(Color.red);
            g.fillRect(0, 0, width, height);
            g.setColor(Color.yellow);
            int a, b;
            a = -50 + (int)(Math.random()*(width+40));
            b = (int)(Math.random()*(height+20));
            g.fillOval(a, b, 130, 110);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to java and really need some help. I created a command line
I'm pretty new to Java and this has me really confused. I have two
I'm new to Java and really need your help. I am presently using a
I'm really new to java, and I need to know is this a memory
I am really new to java (started learning 2 days ago). Sorry if this
I am really new to Java and I read that synchronized is very expensive
I am really new to the programming but I am studying it. I have
I'm pretty new to java and really enjoying learning about it. I have made
I'm quite new to java programming, but I'll try to use the correct terms
I am relatively new to Java and I need some help to extract multiple

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.