# Highlight Circle When Mouse Over

• 04-02-2012, 07:47 PM
Zenos
Highlight Circle When Mouse Over
Need ideas or fix for properly changing colour of circle when mouse over it, do not want a square area like this basic code:
Code:

```class Circle {     int radius = 100; //radius is actually diameter     int x, y; //top-left corner     void draw(Graphics blurb) {         if (xpos >= this.x && xpos <= this.x+radius && ypos >= this.y && ypos <= this.y+radius) {             blurb.setColor(Color.black);         } else {             blurb.setColor(Color.red);         }         blurb.fillOval(this.x, this.y, radius, radius);     } }```
Tried pythagoras to get circle shape but failed, broken code compiles but none of the circles change colour:
Code:

```class Circle {     int radius = 100; //radius is actually diameter     int x, y; //top-left corner     double square, root, xDiff, yDiff, xDiffSquared, yDiffSquared, totalDiff, pythValue;     void draw(Graphics blurb) {         this.xDiff = (this.x+radius/2)-xpos; //xpos/ypos are mouse positions         this.yDiff = (this.y+radius/2)-ypos;         this.xDiffSquared = Math.pow(xDiff, square);         this.yDiffSquared = Math.pow(yDiff, square);         this.totalDiff = xDiffSquared+yDiffSquared;         this.pythValue = Math.pow(totalDiff, root);         if (pythValue <= 50) {             blurb.setColor(Color.black);         } else {             blurb.setColor(Color.red);         }         blurb.fillOval(this.x, this.y, radius, radius);     } }```
Any ideas?
• 04-02-2012, 07:50 PM
KevinWorkman
Re: Highlight Circle When Mouse Over
Why don't you just get the center of the circle and find its distance from the mouse position? If it's less than the radius of the circle, the mouse is inside the circle.
• 04-02-2012, 08:00 PM
DarrylBurke
Re: Highlight Circle When Mouse Over
Or just use Ellpse2D.Double#contains(...) instead of reinventing the wheel.

db
• 04-02-2012, 08:27 PM
Zenos
Re: Highlight Circle When Mouse Over
Managed to see the problem once I saw the code on the forum post - hadn't noticed problem for at least 30 minutes looking at IDE window. This shows the danger the simple errors that are hiding in plain sight.

Fixed code now works:

Code:

```class Circle {     int radius = 100; //radius is actually diameter     int x, y; //top-left corner     double square = 2, root = 0.5, xDiff, yDiff, xDiffSquared, yDiffSquared, totalDiff, pythValue; //problem 1 - square and root both had no value set     void draw(Graphics blurb) {         this.xDiff = (this.x+radius/2)-xpos; //xpos/ypos are mouse positions         this.yDiff = (this.y+radius/2)-ypos;         this.xDiffSquared = Math.pow(this.xDiff, square); //problem 2 - 'this.' was somehow missing from this line and 4 more lines below         this.yDiffSquared = Math.pow(this.yDiff, square);         this.totalDiff = this.xDiffSquared+this.yDiffSquared;         this.pythValue = Math.pow(this.totalDiff, root);         if (this.pythValue <= 50) {             blurb.setColor(Color.black);         } else {             blurb.setColor(Color.red);         }         blurb.fillOval(this.x, this.y, radius, radius);     } }```
Quote:

Originally Posted by KevinWorkman
Why don't you just get the center of the circle and find its distance from the mouse position? If it's less than the radius of the circle, the mouse is inside the circle.

At the time, using pythagoras was the only way I knew to find distance between two coordinates.
Quote:

Originally Posted by DarrylBurke
Or just use Ellpse2D.Double#contains(...) instead of reinventing the wheel.

Thanks, had never heard of Ellipse2D class before, will use it next time I need.