Paul Pearson

+ Follow
since Jan 15, 2008
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Paul Pearson

Ok here it is. Thanks for the appreciation!

It's tricky to explain exactly how it works, but parts of it make use of the cross product and dot (also called scalar) product of two vectors. These can be looked up on wikipedia.

For the rotation, imagine the vertices as lying on a sphere with centre the same as the centre of the cube. A point on or near the cube on the screen corresponds to a point (well really two points!) on the sphere. For a mouse drag from a vertex, the program works out how the sphere has been rotated and then rotates all vertices accordingly.

13 years ago
Thanks David!
Just finished a fully commented final version that responds correctly to window resizing, and with all the unused variables and methods removed. Give me a shout if anyone wants it.
13 years ago
Thanks Justin and David!

David, I think your solution could well work. In the end though, I completed the program by implementing the interfaces MouseInputListener and MouseWheelListener on the class with the variables I needed to access. I'm so new to Java that I wasn't aware you could implement two interfaces on the same class, before I did some research.

In fact the cube does rotate and translate. It doesn't just translate the 2D image, it translates the representation of the cube in 3D space and draws it exactly as you would see it, in perspective. It assumes the viewer's eye is in the middle of the window according to the original window size (and 2200 pixels distance from the screen), so if you resize the window your eye is probably in the wrong place. I'm quite proud of the program so I'm posting the full source code below. It's made up of four classes, so it's probably best if you save them in four separate files, called respectively,, and Look for the comments in caps to see where to separate the files. Most of the code isn't properly commented though I'm afraid. I'm sorry if this isn't the accepted place to post long source code. I'm very new here!

I think there are bits of code here that aren't needed (used for testing purposes). I could also save memory by cutting down on variables (lots of variables helped to get it clear in my head), and it may not be the most efficient way of doing things, but the vector maths is pretty sound (I'm a mathematician). You can't beat that feeling you get when you try a piece of code and it works! I'd be interested to see what people think. Please post comments or questions.

Finally here's how you get it to work:
Originally you see just the face of the cube that is nearest you.
Click and drag anywhere on the cube (or window) to translate it.
Roll the mouse wheel to translate the cube towards or away from you.
But best of all, click and drag on any of the vertices to rotate the cube. That's the bit that took the most thought and effort!

13 years ago
I'm writing a program using Java2D which displays a 3D cube and responds to mouse clicks and movement.

I've got a JFrame containing a JComponent. This JComponent possesses variables holding information like the coordinates of cube vertices and such like. I want to detect mouse clicks and mouse movements on this component, so I've extended the MouseAdapter class and added an instance to the JComponent. So far so good - it detects clicks and everything else fine.

But within the mouseClicked() method that I'm overriding, I want to access
the coordinate variables in the JComponent class, so that I can tell, for
example, if a mouse click is close to a cube vertex.

I can see a couple of ways I could get round this, for example implementing the MouseListener interface on the JComponent class itself instead, or else making the variables that I want access to static. But isn't there another way that is less of a fudge, which allows me to keep the listener as a separate class?

13 years ago