⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelmanager.java

📁 本项目是用JAVA3D开发的一款图形界面的3D漫游的类似引擎.至所以这么说.是因为它的部分功能还不完全.说它是引擎是因为它可以完全脱离模型文件.本引擎实现了虚拟漫游,碰撞检测,动态添加模型,以及部分纹
💻 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 + -