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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:21:03+00:00 2026-06-14T09:21:03+00:00

I have created a program which draws a circle when I click on the

  • 0

I have created a program which draws a circle when I click on the screen. I have it working so that I can draw as many circles as I want. I can even drag one circle and not the others if I hard code which circle I am dragging. The code is:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class DrawBall extends JPanel implements MouseListener, MouseMotionListener {
private List<Ball> balls;
private int x, y;
private int numBalls = 0;
boolean drag = false;

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Draw Ball");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent newContentPane = new DrawBall();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

public DrawBall() {
    super(new BorderLayout());
    balls = new ArrayList<Ball>(10);
    addMouseListener(this);
    addMouseMotionListener(this);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RederingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for(Ball ball: balls) {
        ball.paint(g2d);
    }
    g2d.dispose();
}

public void mouseClicked(MouseEvent event) {
    x = (int) event.getPoint().getX();
    y = (int) event.getPoint().getY();
    Ball ball = new Ball(Color.red, numBalls, 30);
    ball.setX(x);
    ball.setY(y);
    balls.add(ball);
    numBalls = numBalls + 1;
    repaint();
}

public void mousePressed(MouseEvent event) {
    drag = true;
}

public void mouseReleased(MouseEvent event) {
    drag = false;
}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}

public void mouseDragged(MouseEvent event) {
    if(drag == true) {
        x = (int) event.getPoint().getX();
        y = (int) event.getPoint().getY();
        if(event.getSource() == balls.get(0)) {
            Ball ball = balls.get(0);
            ball.setX(x);
            ball.setY(y);
            balls.set(0,ball);
        }
        repaing();
    }
}

public void mouseMoved(MouseEvent event) {}

public class Ball {
    private Color color;
    private int x, y, diameter, id;

    public Ball(Color color, int id, int diameter) {
        setColor(color);
        setID(id);
        setDiameter(diameter);
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setID(int id) {
        this.id = id;
    }

    public void setDiameter(int diameter) {
        this.diameter = diameter;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getID() {
        return id;
    }

    public int getDiameter() {
        return diameter;
    }

    public Color getColor() {
        return color;
    }

    protected void paint(Graphics2D g2d) {
        int x = getX();
        int y = getY();
        g2d.setColor(getColor());
        g2d.fillOval(x, y, getDiameter(), getDiameter());
    }
}
}

My question is this: In my mouseDragged method, is there an easy way to tell which circle I am hovering over? I have played around with the event.getSource() but it doesn’t seem to be working for my circles, at least not in the way I would expect. Thanks for any help.

  • 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-14T09:21:04+00:00Added an answer on June 14, 2026 at 9:21 am

    Modify your Ball class so it creates a circle based on the center point and the radius, rather than the upper left point and the diameter.

    Then, you can calculate if you click inside of a circle by applying the distance formula to the point you’ve clicked and the center point of each of the balls in turn.

    The first ball where the distance is less than the radius is the ball you’ve clicked on. Or, if you want to be more sophisticated, the ball with the smallest distance less than the radius is the ball you’ve clicked on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a program using Turtle Graphics in Python 3 which draws the
I have created a c/c++ program which uses opencv library. I want to convert
I have created a program which allows me to upload images to my server.
I have created a concurrent, recursive directory traversal and file processing program, which sometimes
I have created a non-form c# program that uses the NotifyIcon class. The text
In my program I have a memory DC to which I frequenty draw, then
I have a program that draws the contents of a QGraphicsScene to a PNG
I have some objects that are created/destroyed very often and that can exist in
I have a program which creates JButtons which are then added to a JPanel
I have been trying to create a simple program with Python which uses OpenCV

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.