# Thread: Highlight Circle When Mouse Over

1. Member Join Date
Apr 2012
Posts
2
Rep Power
0

## 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:
Java Code:
```class Circle {
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);
}
}
}```
Tried pythagoras to get circle shape but failed, broken code compiles but none of the circles change colour:
Java Code:
```class Circle {
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.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);
}
}
}```
Any ideas?  Reply With Quote

2. ## 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.  Reply With Quote

3. ## Re: Highlight Circle When Mouse Over

Or just use Ellpse2D.Double#contains(...) instead of reinventing the wheel.

db  Reply With Quote

4. Member Join Date
Apr 2012
Posts
2
Rep Power
0

## 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:

Java Code:
```class Circle {
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.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);
}
}
}``` 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. 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•