📄 jcanvas3dnavigation.java
字号:
locale = new Locale(vu);
//
// BranchGraphs
//
sceneBranch = new BranchGroup();
viewBranch = new BranchGroup();
enviBranch = new BranchGroup();
// ViewBranch
TransformGroup viewTG = new TransformGroup();
viewTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
ViewPlatform vp = new ViewPlatform();
view.attachViewPlatform(vp);
orbitBehInterim = new OrbitBehaviorInterim(jCanvas3D, viewTG, OrbitBehaviorInterim.REVERSE_ALL);
orbitBehInterim.setSchedulingBounds(globalBounds);
Transform3D homeTransform = new Transform3D();
homeTransform.setTranslation(new Vector3f(0.0f, 0.5f, 15.0f));
orbitBehInterim.setHomeTransform(homeTransform);
orbitBehInterim.setHomeRotationCenter(new Point3d(0.0, 0.0, 2.0));
DirectionalLight headLight = new DirectionalLight();
headLight.setInfluencingBounds(globalBounds);
viewTG.addChild(vp);
viewTG.addChild(orbitBehInterim);
viewTG.addChild(headLight);
viewBranch.addChild(viewTG);
// EnviBranch
Background bg = new Background();
bg.setApplicationBounds(globalBounds);
bg.setColor(new Color3f(bgColor));
enviBranch.addChild(bg);
// SceneBranch
scaleTG = new TransformGroup();
scaleTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
sceneBranch.addChild(scaleTG);
characterTG = new TransformGroup();
// Center of character
Transform3D charTransform = new Transform3D();
// Scale to a depth of 2.0f (0.2f * 10.0)
charTransform.setScale(new Vector3d(1.0f, 1.0f, 10.0f));
// Rotate around X
charTransform.setRotation(new AxisAngle4f(1.0f, 0.0f, 0.0f, -0.35f));
// Center text
charTransform.setTranslation(new Vector3f(-4.2f, -0.36f, 0.0f));
characterTG.setTransform(charTransform);
scaleTG.addChild(characterTG);
}
private void showJCanvas3D() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenDim = toolkit.getScreenSize();
Dimension dim = new Dimension(screenDim.width - 20, screenDim.width / 3);
jCanvas3D.setPreferredSize(dim);
jCanvas3D.setSize(dim);
JPanel jPanel = new JPanel(new BorderLayout());
jPanel.add(jCanvas3D, BorderLayout.CENTER);
// Off-screen Canvas3D of this lightweight JCanvas3D
offCanvas3D = jCanvas3D.getOffscreenCanvas3D();
if (offCanvas3D != null) {
// View renders into the off-screen Canvas3D
view.addCanvas3D(offCanvas3D);
// PickCanvas operates on the not visible off-screen Canvas3D
pickCanvas = new PickCanvas(offCanvas3D, sceneBranch);
pickCanvas.setMode(PickInfo.PICK_GEOMETRY);
pickCanvas.setFlags(PickInfo.NODE | PickInfo.CLOSEST_INTERSECTION_POINT);
pickCanvas.setTolerance(4.0f);
}
else {
System.out.println("JCanvas3DNavigation: OffscreenCanvas3D = null !!");
System.exit(0);
}
// GUI / User actions
int fontSize = 14;
if (screenDim.height < 1024)
fontSize = 11;
else if (screenDim.height < 1200)
fontSize = 12;
Font font = new Font("SansSerif", Font.BOLD, fontSize);
JPanel jPanelLine = new JPanel(new BorderLayout());
jPanelLine.setBackground(new Color(0.05f, 0.05f, 0.8f));
jPanelLine.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0));
JPanel jPanelActionX = new JPanel();
BoxLayout boxLayoutX = new BoxLayout(jPanelActionX, BoxLayout.X_AXIS);
jPanelActionX.setLayout(boxLayoutX);
jPanelActionX.setBackground(bgColor);
jPanelActionX.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
jPanelLine.add(jPanelActionX, BorderLayout.CENTER);
JPanel jPanelActionXX = new JPanel();
BoxLayout boxLayoutXX = new BoxLayout(jPanelActionXX, BoxLayout.X_AXIS);
jPanelActionXX.setLayout(boxLayoutXX);
jPanelActionXX.setBackground(bgColor);
jPanelActionXX.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
jPanelActionX.add(Box.createHorizontalGlue());
jPanelActionX.add(jPanelActionXX);
// Home Transform
JPanel jPanelActionHome = new JPanel();
BoxLayout boxLayoutHome = new BoxLayout(jPanelActionHome, BoxLayout.Y_AXIS);
jPanelActionHome.setLayout(boxLayoutHome);
TitledBorder tBorderHome = BorderFactory.createTitledBorder(" Home Transform ");
tBorderHome.setTitleFont(font);
tBorderHome.setTitleColor(bgColor);
tBorderHome.setBorder(BorderFactory.createLineBorder(bgColor));
jPanelActionHome.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createRaisedBevelBorder(), tBorderHome));
JButton jButtonHomeTransform = new JButton("Go Home");
jButtonHomeTransform.setFont(font);
jButtonHomeTransform.setForeground(bgColor);
jButtonHomeTransform.setAlignmentX(Component.CENTER_ALIGNMENT);
jButtonHomeTransform.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
orbitBehInterim.goHome(isHomeRotCenter);
}
});
JCheckBox jChecHomeRotCenter = new JCheckBox("Home Rotation Center");
jChecHomeRotCenter.setFont(font);
jChecHomeRotCenter.setForeground(bgColor);
jChecHomeRotCenter.setAlignmentX(Component.CENTER_ALIGNMENT);
jChecHomeRotCenter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
isHomeRotCenter = ((JCheckBox)event.getSource()).isSelected();
}
});
jPanelActionHome.add(jButtonHomeTransform);
jPanelActionHome.add(jChecHomeRotCenter);
jPanelActionXX.add(jPanelActionHome);
jPanelActionXX.add(Box.createHorizontalStrut(10));
// Center picking
JPanel jPanelActionCenter = new JPanel();
BoxLayout boxLayoutCenter = new BoxLayout(jPanelActionCenter, BoxLayout.Y_AXIS);
jPanelActionCenter.setLayout(boxLayoutCenter);
TitledBorder tBorderCenter = BorderFactory.createTitledBorder(" Rotation Center Picking ");
tBorderCenter.setTitleFont(font);
tBorderCenter.setTitleColor(bgColor);
tBorderCenter.setBorder(BorderFactory.createLineBorder(bgColor));
jPanelActionCenter.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createRaisedBevelBorder(), tBorderCenter));
JPanel jPanelPickMode = new JPanel();
BoxLayout boxLayoutPickMode = new BoxLayout(jPanelPickMode, BoxLayout.X_AXIS);
jPanelPickMode.setLayout(boxLayoutPickMode);
jPanelPickMode.setAlignmentX(Component.CENTER_ALIGNMENT);
final JRadioButton jRadioPickVertex = new JRadioButton("Pick Vertex");
final JRadioButton jRadioPickShape = new JRadioButton("Pick Shape");
jRadioPickVertex.setFont(font);
jRadioPickShape.setFont(font);
jRadioPickVertex.setForeground(bgColor);
jRadioPickShape.setForeground(bgColor);
ButtonGroup bGroup = new ButtonGroup();
bGroup.add(jRadioPickVertex);
bGroup.add(jRadioPickShape);
jRadioPickVertex.setSelected(true);
ItemListener pickModeListener = new ItemListener() {
public void itemStateChanged(ItemEvent event) {
isPickVertex = jRadioPickVertex.isSelected();
}
};
jRadioPickVertex.addItemListener(pickModeListener);
jRadioPickShape.addItemListener(pickModeListener);
jPanelPickMode.add(jRadioPickVertex);
jPanelPickMode.add(Box.createHorizontalStrut(5));
jPanelPickMode.add(jRadioPickShape);
JCheckBox jCheckLookAtCenter = new JCheckBox("Look at Center");
jCheckLookAtCenter.setFont(font);
jCheckLookAtCenter.setForeground(bgColor);
jCheckLookAtCenter.setAlignmentX(Component.CENTER_ALIGNMENT);
jCheckLookAtCenter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
isLookAtRotCenter = ((JCheckBox)event.getSource()).isSelected();
}
});
jPanelActionCenter.add(jPanelPickMode);
jPanelActionCenter.add(jCheckLookAtCenter);
jPanelActionXX.add(jPanelActionCenter);
jPanelActionXX.add(Box.createHorizontalStrut(10));
// Look At Current Center
JPanel jPanelActionLookAt = new JPanel();
BoxLayout boxLayoutLookAt = new BoxLayout(jPanelActionLookAt, BoxLayout.Y_AXIS);
jPanelActionLookAt.setLayout(boxLayoutLookAt);
TitledBorder tBorderLookAt = BorderFactory.createTitledBorder(" Center Transform ");
tBorderLookAt.setTitleFont(font);
tBorderLookAt.setTitleColor(bgColor);
tBorderLookAt.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(bgColor),
BorderFactory.createEmptyBorder(0, 4, 0, 4)));
jPanelActionLookAt.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createRaisedBevelBorder(), tBorderLookAt));
JButton jButtonLookAt = new JButton("Look at Center");
jButtonLookAt.setFont(font);
jButtonLookAt.setForeground(bgColor);
jButtonLookAt.setAlignmentX(Component.CENTER_ALIGNMENT);
jButtonLookAt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
orbitBehInterim.lookAtRotationCenter();
}
});
JPanel spacePanel = new JPanel();
spacePanel.setMaximumSize(jButtonLookAt.getPreferredSize());
spacePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
jPanelActionLookAt.add(jButtonLookAt);
jPanelActionLookAt.add(spacePanel);
jPanelActionXX.add(jPanelActionLookAt);
jPanelActionX.add(Box.createHorizontalGlue());
jPanel.add(jPanelLine, BorderLayout.SOUTH);
// JFrame
JFrame jFrame = new JFrame();
jFrame.setTitle("InteractiveMesh : JCanvas3D Navigation");
jFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jFrame.add(jPanel);
jFrame.pack();
Dimension jframeDim = jFrame.getSize();
jFrame.setLocation((screenDim.width - jframeDim.width)/2, (screenDim.height - jframeDim.height)/2);
jFrame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -