Croolsby
12-18-2011, 07: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 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();
}
}
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 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();
}
}