📄 modelmanager.java
字号:
package cn.bz.head.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.OpenType;
import java.io.File;
import javax.media.j3d.BranchGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import cn.bz.collision.Model;
import cn.bz.collision.Ramble;
import test.FileChooserTest;
public class ModelManager extends JPanel {
public static Ramble ramble;
Model model;
//生成模型所需参数
String fileNameString ;
boolean flagCollison,
flagPicking,
flagModelState ;
BranchGroup modelBranchGroup;
JFileChooser fileChooser ;
JCheckBox collisonCheckBox, // 模型是否碰撞检测
pickingCheckBox, // 模型是否拾取
modelStateCheckBox; // 模型是否是动态添加
JButton modelAdd , // 模型移除
modelRemove , // 模型添加
open , // 选择模型
save ; // 保存模型
public ModelManager() {
flagCollison = false ;
flagPicking = false ;
flagModelState = true ;
collisonCheckBox = new JCheckBox("模型是否碰撞检测") ;
pickingCheckBox = new JCheckBox("模型是否拾取") ;
modelStateCheckBox = new JCheckBox("模型是否是动态添加") ;
collisonCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(collisonCheckBox);
pickingCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(pickingCheckBox);
modelStateCheckBox.setEnabled(false);
modelStateCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(modelStateCheckBox);
// TODO Auto-generated constructor stub
modelAdd = new JButton("添加模型");
modelAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(modelAdd);
modelRemove = new JButton("删除模型");
modelRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(modelRemove);
open = new JButton("浏览");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(open);
save = new JButton("保存");
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
action(e);
}
});
add(save); //将场景保存后就可进行修改了.
setState1(true); //将除浏览外的所有按钮设为不可用
}
public void action(ActionEvent e) {
if(e.getActionCommand() == "模型是否碰撞检测"){
if(collisonCheckBox.isSelected()){
flagCollison = true ;
}
else {
flagCollison = false ;
}
}
if(e.getActionCommand().equals("模型是否拾取")){
if(pickingCheckBox.isSelected()){
flagPicking = true ;
}
else {
flagPicking = false ;
}
}
if(e.getActionCommand().equals("模型是否是动态添加")){
if(modelStateCheckBox.isSelected()){
flagModelState = true ;
}
else {
flagModelState = false ;
}
}
if (e.getActionCommand().equals("添加模型")) {
model = new Model(fileNameString, flagCollison, flagPicking, flagModelState);
modelBranchGroup = ramble.setScene(model);
setState2(false);
}
if (e.getActionCommand().equals("删除模型")) {
if (modelBranchGroup == null) {
throw new NullPointerException("此模型为空");
} else {
ramble.remove(modelBranchGroup);
}
setState1(true);
}
if(e.getActionCommand().equals("浏览")){
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
//System.out.println(System.getProperties());//当不知道系统属性都包括什么时.用此方法输出一下.找一下.user.dir表示当前目录
int rVal = fileChooser.showOpenDialog(this);
if (rVal == JFileChooser.APPROVE_OPTION) {
fileNameString = fileChooser.getSelectedFile().getName();
setState2(true);
}
if (rVal == JFileChooser.CANCEL_OPTION) {
setState1(true);
}
}
if(e.getActionCommand().equals("保存")){
}
}
/**
* 设置按钮状态.当添加模型时,模型属性可用,移除按钮不能用
* 移除模型时相反.添加后的模型不能直接修改其属性,必须先删除
* @param flag
*/
private void setState1(boolean flag){
open.setEnabled(flag);
modelAdd.setEnabled(!flag);
modelRemove.setEnabled(!flag);
collisonCheckBox.setEnabled(!flag);
pickingCheckBox.setEnabled(!flag);
}
private void setState2(boolean flag){
open.setEnabled(flag);
modelAdd.setEnabled(flag);
modelRemove.setEnabled(!flag);
collisonCheckBox.setEnabled(flag);
pickingCheckBox.setEnabled(flag);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -