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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:11:21+00:00 2026-05-30T11:11:21+00:00

I have a Ball class which I want to have extend JComponent and implement

  • 0

I have a Ball class which I want to have extend JComponent and implement mouseListener.

public class Ball extends JComponent implements MouseListener {
    Int x, y, radius;

    public Ball(int X, int Y, int Radius){
        //contains only three ints and redefines x,y,radius
        x=X;
        y=Y;
        radius=Radius;
    }

    public void draw(Graphics g){
        //draw oval using x,y,radius
    }

    //5 mouselisteners undefined yet
}

So ball is the constructor which is used by a panel which is within a frame.

Sorry I have not yet entered all the code. I will submit my complete code soon.

So what I would have to do is use MouseEntered listener in the ball class so that when the mouse enters the component (the ball/oval) .
But I don’t know how to define the component so that it knows it has been entered.
Does it need some dimensions? Because all I am doing is using the draw function in a panel.

  • 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-30T11:11:23+00:00Added an answer on May 30, 2026 at 11:11 am

    If this were my class, I wouldn’t have it extend JComponent and wouldn’t give it a MouseListener or MouseMotionListener especially if I wanted to display multiple balls in a single JComponent. Instead I would give it public methods that allow other classes to get its boundaries (such as is available from the Shape interface), and whether something is contained in the shape or not (again the Shape interface works well for this), and other public methods that allow outside classes to change the state (appearance?) of this object.

    I would then have a JComponent hold one Ball or an ArrayList<Ball>, and in the MouseListener/MouseMotionListener/MouseAdapter for this JComponent, iterate through the ArrayList<Ball> seeing if the mouse is inside of any ball, and if so, change that ball’s state. Then in the JComponent’s paintComponent method, I’d iterate through the ArrayList<Ball> calling draw(g) on each Ball contained.

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

Sidebar

Related Questions

I have a class called Ball, and i want to call a method called
I have this code: package graphics { import flash.display.Sprite; import flash.events.*; public class Ball
I have a ball class which is composed by an UIImage I draw on
I have a class which extends BufferedReader, and a list of file streams b.close()
I have a class Drawview: public class DrawView extends View { private ColorBall[] colorballs
I have an Object, Ball, which belongs_to a Girl, which can have_many balls. Everything
I have the following List which contains the following collection. How i can transform
In C++, I have an Object class, from which a number of other classes
I have a canvas with several small canvases on it. Like this: public class
I'm having problems with addChild() from a class. I have a Ball class: package

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.