简体   繁体   English

如何在java3D中使用MouseListener和MouseMotionListener旋转3D对象?

[英]How to use MouseListener and MouseMotionListener in java3D to rotate 3D object?

I m making desktop app in java swing. 我正在用Java swing制作桌面应用程序。 i made 3D image from my 2D image using PointArray[]. 我使用PointArray []从2D图像制作了3D图像。 now i want to rotate image using MouseListener and MouseMotionListener.I used MouseRotate object to rotate myImage, but it not works well for that, MouseRotate rotate image with origin(0,0,0). 现在我想使用MouseListener和MouseMotionListener旋转图像。我使用MouseRotate对象旋转myImage,但是它不适用于此,MouseRotate旋转origin(0,0,0)的图像。 but i want to rotate image using center point of image. 但是我想使用图像的中心点旋转图像。 means rotate image using center point not origin point. 表示使用中心点而不是原点旋转图像。 So, How can i do that? 那么,我该怎么做?

Hmm, it's hard to tell without code, but I think you can just set up a transformation matrix and rotate it with that. 嗯,没有代码很难说,但是我认为您可以设置一个转换矩阵并随之旋转它。 Assuming the image is facing the front of the screen, you can try something like this: 假设图像面向屏幕的前面,您可以尝试如下操作:

public void mouseDragged(MouseEvent e)
{
    int dx = e.getX() - x; //x should be a global variable
    int dy = e.getY() - y; //same applies here
    x = e.getX(); //to set x for the next update loop
    y = e.getY();
    double rotation = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
    Transform3D transform = new Transform3D();
    Matrix3d matrix = new Matrix3d();
    transformG.getTransform(transform); //assuming the TransformGroup your image is in is transformG
    transform.getRotationScale(matrix);
    transform.rotZ(rotation);
    transformG.setTransform(transform);
}

You can set up the rotation amount differently if you want, but this should give you an idea 您可以根据需要设置不同的旋转量,但这应该可以使您有所了解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM