Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Applet Not Detecting Mouse Motion?

    I made a simple applet to draw a circle circumscribed by a hexagon. The length of the sides of the hexagon is a function of the radius of the circle so that if the size of the circle changes, the hexagon will change size to stay circumscribed about the circle.

    The quickest way to test if it would work was to implement MouseMotionListener, and then simply make the radius = e.getX(); .

    However, now my program won't even read the mouse, or so it seems. I changed the implementation to record the mouseX and mouseY, and then print the coordinates in the paint() method, but it seems that the coordinates are not being updated when the mouse is moved.

    Beats me. Take a look please:

    Code:
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class CircleHex extends Applet implements MouseMotionListener {
        private int radius;
        private double side;
        int x;
        int y;
    
        int mouseX;
        int mouseY;
    
        public void init() {
            radius = 100;        
        }
    
        public void paint(Graphics g) {
            x = (getWidth() / 2);
            y = (getHeight() / 2);
    
            side = 2 * radius * Math.tan(Math.PI / 6);
    
            drawCircle(g);
            drawHex(g);
    
            g.drawString("radius: " + Integer.toString(radius), 10, 20);
            g.drawString("size: " + Double.toString(side), 10, 40);
            g.drawString("mouseX : " + Integer.toString(mouseX), 10, 60);
            g.drawString("mouseY : " + Integer.toString(mouseY), 10, 80);
        }
    
        public void drawCircle(Graphics g) {        
            g.drawOval((x - radius), (y -  radius), 
                (2 *  radius), (2 * radius));        
        }
    
        public void drawHex(Graphics g) {
            int x1;
            int y1;
            int x2;
            int y2;
    
            x1 = (int)(x + side * Math.cos(Math.PI / 3));
            y1 = (int)(y - side * Math.sin(Math.PI / 3));
            x2 = (int)(x + side);
            y2 = y;
            g.drawLine(x1, y1, x2, y2);
    
            x1 = x2;
            y1 = y2;
            x2 = (int)(x + side * Math.cos(Math.PI / 3));
            y2 = (int)(y + side * Math.sin(Math.PI / 3));
            g.drawLine(x1, y1, x2, y2);
    
            x1 = x2;
            y1 = y2;
            x2 = (int)(x - side * Math.cos(Math.PI / 3));
            y2 = (int)(y + side * Math.sin(Math.PI / 3));
            g.drawLine(x1, y1, x2, y2);
    
            x1 = x2;
            y1 = y2;
            x2 = (int)(x - side);
            y2 = y;
            g.drawLine(x1, y1, x2, y2);
    
            x1 = x2;
            y1 = y2;
            x2 = (int)(x - side * Math.cos(Math.PI / 3));
            y2 = (int)(y - side * Math.sin(Math.PI / 3));
            g.drawLine(x1, y1, x2, y2);
    
            x1 = x2;
            y1 = y2;
            x2 = (int)(x + side * Math.cos(Math.PI / 3));
            y2 = (int)(y - side * Math.sin(Math.PI / 3));
            g.drawLine(x1, y1, x2, y2);
        }
    
        public void mouseDragged(MouseEvent e) {
        }
    
        public void mouseMoved(MouseEvent e) {    
            mouseX = e.getX();
            mouseY = e.getY();
            repaint();
        }
    }
    Last edited by Croolsby; 12-18-2011 at 11:45 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You're just not listening to it. You have the mouse listener set up, but you have no way to actually react to it. In init() you can add this.addMouseMotionListener(this);, and it will work. It'll flicker though.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OH! of course! haha it's so obvious now. It's been a while since I've last used java. Thanks!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Croolsby View Post
    OH! of course! haha it's so obvious now. It's been a while since I've last used java. Thanks!
    Yep you bet. I can show you a way to prevent the flickering as well if you want.

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh thanks for the offer. I'm guessing you're talking about double buffering? I think I got
    that concept down. I just got rid of the double buffering in the code I posted since the problem persisted even when I removed the double buffering. But I added the double buffering back in and now it works without flickering, too.

    I do have a specific question about a certain aspect of animation, however. But I think it should be in it's own thread.
    Last edited by Croolsby; 12-19-2011 at 03:57 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
    •