Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Highlight Circle When Mouse Over

  1. #1
    Zenos is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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 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:
    Java 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?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,941
    Rep Power
    8

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Highlight Circle When Mouse Over

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

    db
    KevinWorkman likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Zenos is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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 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 View Post
    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 View Post
    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.

Similar Threads

  1. GNU Source-highlight 3.1.6
    By java software in forum Java Software
    Replies: 0
    Last Post: 01-01-2012, 12:58 PM
  2. highlight text
    By Saran185 in forum AWT / Swing
    Replies: 2
    Last Post: 01-28-2011, 03:24 PM
  3. Date highlight
    By Shyamz1 in forum New To Java
    Replies: 8
    Last Post: 11-26-2010, 02:19 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  5. GNU Source-highlight 2.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:39 AM

Posting Permissions

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