求大佬幫忙,小弟跪謝!

最近一直卡在一個地方,一個多周了。
做了個java3d程式,在滑鼠鍵盤聯動上面出問題了,我想讓滑鼠旋轉之后,鍵盤會按斬訓鼠旋轉后的方向繼續運動。但是我的我程式結果是滑鼠旋轉物體之后,鍵盤再按下去就會回到原來的位置。
感覺應該是在物件運用上面出錯了,但是具體出錯在哪就是找不到。
現在是才做到物件相關的地方我就卡了好久,后面還有多執行緒,mysql,redis,檔案操作,高并發,都要用在這個專案上面, 這個畢業設計都不知道什么時候能做好了,5月底之前做好6月10號就要答辯了,我好菜啊!!!
為什么要用java3d這么老的東西,我寫了個感想:https://liu-endong.blog.csdn.net/article/details/104854301
Main.java如下
package keyMouse;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.applet.Applet;
import java.awt.*;
/*
* Simple Java 3D example to display a rotating color cube.
*/
public class Main extends Applet {
// Constructor
public Main() {
// Setup a SimpleUniverse by referencing a Canvas3D
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
// 設定觀察點
Viewer viewer = new Viewer(c);
Vector3d viewPoint = new Vector3d(0.0, 1, 10.0);
Transform3D t = new Transform3D();
t.set(viewPoint);
ViewingPlatform v = new ViewingPlatform();
v.getViewPlatformTransform().setTransform(t);
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(v, viewer); // 以3d畫布實體c創建一個區域單一的查看平臺和一個觀察者物件
u.getViewingPlatform();
u.addBranchGraph(scene);
}
// Create the content branch
public BranchGroup createSceneGraph() {
// Create the root node of the content branch
BranchGroup objRoot = new BranchGroup();
// Create the TransformGroup node, which is writable to support
// animation, and add it under the root
Transform3D t3d = new Transform3D();
t3d.setScale(0.1);
Appearance app = new Appearance();
Material material = new Material();
material.setEmissiveColor(1f, 0f, 0f);
app.setMaterial(material);
Box cubeBox = new Box(5f, .1f, 5f, app); // (長,寬,高,外表)
// group.addChild(cubeBox);
TransformGroup objScale = new TransformGroup(t3d);
objScale.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objScale.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(cubeBox);
BoundingSphere bound = new BoundingSphere(new Point3d(1.0, 2.0, 3.0), 1.0);
// 定義事件物件
Key myBehavior = new Key(objScale, objRoot, bound);
myBehavior.setSchedulingBounds(bound);
objScale.addChild(myBehavior);
//滑鼠旋轉
// MouseRotate mouseRotate = new MouseRotate();
// mouseRotate.setTransformGroup(objScale);
// objRoot.addChild(mouseRotate);
// mouseRotate.setSchedulingBounds(bound);
// Compile to perform optimizations on this content branch.
objRoot.addChild(objScale);
objRoot.compile();
return objRoot;
}
// Entry main method to invoke the constructor on the eventdispatcher thread.
public static void main(String args[]) {
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// KeyMoveT2 liFangTi = new KeyMoveT2();
// }
// });
new MainFrame(new Main(), 800, 800);
}
}
Key.java如下
package keyMouse;
import java.awt.AWTEvent;
import java.awt.event.KeyEvent;
import java.util.Enumeration;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
class Key extends Behavior {
// 設定相關物件
private TransformGroup targetTG;
private Transform3D transform = new Transform3D();
private float x, y, z;
public Key(TransformGroup targetTG, BranchGroup BG, BoundingSphere BS) {
this.targetTG = targetTG;
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(targetTG);
BG.addChild(mouseRotate);
mouseRotate.setSchedulingBounds(BS);
}
@Override
public void initialize() {
wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
@Override
public void processStimulus(Enumeration criteria) {
WakeupCriterion wakeup;
AWTEvent[] events;
Vector3f position = new Vector3f();
// 獲取當前事件,判斷是否是鍵盤事件
wakeup = (WakeupCriterion) criteria.nextElement();
if (wakeup instanceof WakeupOnAWTEvent) {
events = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
// 轉化為鍵盤事件
KeyEvent keyEvent = (KeyEvent) events[0];
transform.get(position);
x = position.x; y = position.y; z = position.z;
// 判斷是不是左鍵,是則左移左旋轉
if (keyEvent.getKeyCode() == KeyEvent.VK_A) {
x += 0.1;
transform.setTranslation(new Vector3f(x, y, z));
targetTG.setTransform(transform);
}
// 判斷是不是右鍵,是則右移右旋轉
else if (keyEvent.getKeyCode() == KeyEvent.VK_D) {
x -= 0.1;
transform.setTranslation(new Vector3f(x, y, z));
targetTG.setTransform(transform);
}
// 判斷是不是前進鍵
else if (keyEvent.getKeyCode() == KeyEvent.VK_W) {
z += 0.1;
transform.setTranslation(new Vector3f(x, y, z));
targetTG.setTransform(transform);
}
// 判斷是不是后退
else if (keyEvent.getKeyCode() == KeyEvent.VK_S) {
z -= 0.1;
transform.setTranslation(new Vector3f(x, y, z));
targetTG.setTransform(transform);
}
// 判斷是不是上鍵,是則上移上旋轉
else if (keyEvent.getKeyCode() == KeyEvent.VK_SPACE) {
y -= 0.1;
transform.setTranslation(new Vector3f(x, y, z));
targetTG.setTransform(transform);
}
// 判斷是不是下鍵,是則下移下旋轉
else if (keyEvent.getKeyCode() == KeyEvent.VK_C) {
y += 0.1;
transform.setTranslation(new Vector3f(x, y, z));
targetTG.setTransform(transform);
}
}
wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
uj5u.com熱心網友回復:
有沒有大佬幫幫忙轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/99023.html
標籤:Java SE
