...

View Full Version : Resolved Simple Applet Not Detecting Mouse Motion?

Croolsby
12-18-2011, 08:20 AM
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:

import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class CircleHex extends Applet implements MouseMotionListener {
private double side;
int x;
int y;

int mouseX;
int mouseY;

public void init() {
}

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("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) {
}

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();
}
}

Fou-Lu
12-18-2011, 03:28 PM
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.

Croolsby
12-18-2011, 11:39 PM
OH! of course! haha it's so obvious now. It's been a while since I've last used java. Thanks!

Fou-Lu
12-19-2011, 03:23 AM
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.

Croolsby
12-19-2011, 03:54 AM
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.