Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5

12182011, 07:20 AM #1
 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; 12182011 at 10:45 PM.
12182011, 02:28 PM
#2
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 16,987
 Thanks
 4
 Thanked 2,660 Times in 2,629 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.
12182011, 10:39 PM
#3
 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!
12192011, 02:23 AM
#4
12192011, 02:54 AM
#5
 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; 12192011 at 02:57 AM.