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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:04:19+00:00 2026-05-30T06:04:19+00:00

I can already create a ball in a panel on MousePressed and MouseReleased and

  • 0

I can already create a ball in a panel on MousePressed and MouseReleased and update coordinates with MotionListener and change the color of the ball when the mouse is over it.
This works fine in the myPanel class because the panel has defined dimensions and the mouse works inside it.
But what I have to do now and am not sure how is to make the Ball class extend Component and implement MouseListener. And with that I have to use MouseEntered in the Ball class to change the color of the ball.
Help?

//Ball
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Ball extends JComponent implements MouseListener{

public int x,y,r;
public Color c = Color.BLUE;
private int distance = 0;

public Ball(int X, int Y, int R){
    super();
    x=X;
    y=Y;
    r=R;
addMouseListener(this);
}

public void draw(Graphics g){
    g.setColor(c);
    g.fillOval(x-r, y-r, 2*r, 2*r);
}

public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){ }
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){
c = Color.ORANGE;
}
public void mouseExited(MouseEvent me){}

}


//myPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;

public class myPanel extends JPanel implements MouseListener{

private Color c = new Color(150,200,100);
public Ball ball = new Ball(100,100,50);
private Point mouseCoords = new Point();

public myPanel(){
    super();
    setLayout(new FlowLayout());
    addMouseListener(this);
    add(ball);
}


public void paintComponent(Graphics g){
    super.paintComponent(g);
    ball.draw(g);
}

public void mousePressed(MouseEvent me){
    ball.x = me.getX();
    ball.y = me.getY();
    labelPanel.setX(me.getX());  //Report x and y values
    labelPanel.setY(me.getY());
//  ball.c = Color.RED;         //change color on click
    repaint();
}
public void mouseReleased(MouseEvent me){}
public void mouseMoved(MouseEvent me) {}
public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}

}

// myFrame
import java.awt.*;
import javax.swing.*;

public class myFrame extends JFrame{

public myPanel left = new myPanel();
public labelPanel right = new labelPanel();

public myFrame(){

    super("This is my Frame");
    setLayout(new BorderLayout());
    setSize(900,700);
    add(left,BorderLayout.CENTER);
    add(right,BorderLayout.EAST);
    setVisible(true);
}

public static void main(String[] args){
    myFrame mF = new myFrame();
    mF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}


//labelPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;



public class labelPanel extends JPanel{

public static JLabel xCoord = new JLabel("X=",JLabel.RIGHT);
public static JLabel yCoord = new JLabel("Y=",JLabel.RIGHT);
public Color c = new Color(100,200,10);

public labelPanel() {
    super();
    setBackground(c);
    setLayout(new GridLayout(2,1));
    add(xCoord);
    add(yCoord);
}

public static void setX(int x){
    xCoord.setText("X=" + x);
}
public static void setY(int y){
    yCoord.setText("Y=" + y);
}

}

So if you run the code, it works as previously mentioned, but I don’t know how to define the Ball class as a Component/JComponent so that it would implement MouseEntered

  • 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-30T06:04:21+00:00Added an answer on May 30, 2026 at 6:04 am

    Use addMouseListener(this) in your constructor:

    public Ball(int X, int Y, int R){
        super();
        x=X;
        y=Y;
        r=R;
        addMouseListener(this);
    }
    

    And remove it from your draw() method.

    Also, if you might want to consider overriding paint(Graphics g). This will allow Swing to determine when to draw. You can always manually choose when to draw by calling repaint(); or calling paint(). repaint() paints this component and all subcomponents, and also clears the component. paint() just paints this component, not subcomponents, and does not clear the screen unless included in the paint method.

    Tell me if that doesn’t fix it.

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

Sidebar

Related Questions

Any idea, how can we create already running website to Mobile Website? Are there,
I can't seem to find the syntax for editing an already-created stored procedure in
I can already do: using System.Windows.Forms; Button b; or: System.Windows.Forms.Button b; but I would
I have a firmware for an USB module I can already control by visual
Simply put; what does my $99 get me, that I can't already get for
I already have a deploy.rb that can deploy my app on my production server.
Can I figure out if a function has already been assigned to an event?
Can I avoid the open DataReader exception (There is already an open DataReader associated
the title already tells the question: you can successfully call the Print-Method on a
How can I call a constructor on a memory region that is already allocated?

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.