Name: Anonymous 2009-11-17 10:23
I think I found a major problem with Java's event handler but I need to make sure its not just a mistake I've made.
Im getting this error ever time a mouse motion event occurs after the program has been running for about 30 seconds.
at java.awt.AWTEventMulticaster.mouseMoved(Unknown Source)
Here is my source code:
Im getting this error ever time a mouse motion event occurs after the program has been running for about 30 seconds.
at java.awt.AWTEventMulticaster.mouseMoved(Unknown Source)
Here is my source code:
import java.awt.*; // Use graphics
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.applet.*; // Use applets
import javax.swing.*; // Use swing stuff (GUI)
import java.util.*; // Use util
public class Cube extends JApplet
{
public void init()
{
setSize(640,480);
getContentPane().add(new CubePanel());
}
public class CubePanel extends JPanel implements MouseMotionListener
{
private double x_verts[] = {-50,50,50,-50}; // Four vertices for a cube
private double y_verts[] = {50,50,-50,-50};
private double shape_save[] = {-50,50,50,-50,50,50,-50,-50}; // Original shape before transformation
private double rotation_pos = 0; // Rotation position
double xTransform = 100;
double yTransform = 10;
int yMouse = 128;
int xMouse = 128;
Color[] cIndex = {Color.yellow, Color.red, Color.blue, Color.green};
void CubePanel()
{
}
private void drawCube(Graphics page,int xpos,int ypos)
{
// Reset to original shape
for(int index = 0;index < 4;index++)
x_verts[index] = shape_save[index]; // Copy X's
for(int index = 4;index < 8;index++)
y_verts[index-4] = shape_save[index]; // Copy Y's
// Rotate all the points like so...
double temp_x,temp_y;
for(int index = 0;index < 4;index++)
{
// Rotate the square normally over the y axis in the xyz plane (I think)
temp_x = x_verts[index]*Math.cos(rotation_pos) - y_verts[index]*Math.sin(rotation_pos);
temp_y = x_verts[index]*Math.sin(rotation_pos) + y_verts[index]*Math.cos(rotation_pos);
x_verts[index] = temp_x + xTransform;
y_verts[index] = temp_y;
// And the square rotates to look 3D 2x along the x axis in the xyz plane (I think)
y_verts[index] = y_verts[index]*Math.sin(rotation_pos/2) + yTransform;
//xTransform -= .001;
//yTransform -= .001;
}
// Add a degree of rotation
rotation_pos += 0.01;
page.setColor(Color.blue);
for(int index = 0;index < 4;index++)
{
page.setColor(cIndex[index]);
if(index < 3)
{
page.drawLine((int)x_verts[index]+xpos,(int)y_verts[index]+ypos,(int)x_verts[index+1]+xpos,(int)y_verts[index+1]+ypos);
}
else
{
page.drawLine((int)x_verts[index]+xpos,(int)y_verts[index]+ypos,(int)x_verts[0]+xpos,(int)y_verts[0]+ypos);
}
}
}
public void paintComponent(Graphics page)
{
addMouseMotionListener(this);
super.paintComponent(page);
// Use a black background
setBackground(Color.black);
// Draw the cube
drawCube(page,xMouse,yMouse);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
xMouse = me.getX() - 95;
yMouse = me.getY();
}
@Override
public void mouseMoved(MouseEvent me) {
}
}
}