I am making a tool to visualize meshes contained into a file type. I made a class for my UserInterface, this class is based on a frame that has a panel and some other components.
Then into my project I extend SimpleApp and configure it to render into the panel. When i add a box into simpleInitApp as a test it renders it ok, but when i select a subfile of my Jtree and create the meshes contained in it I have some problems.
As i didn´t saw none of the meshes created in method fillRootNodeWithMeshes I put a box into it to test if it works. The result is that statsview seems frozen and the box doesn´t appear until i click over the panel, then the box appears and the statsview disappears.
The problem of not seeing the meshes I suppose is because the cam doesn´t focus them so i need to find a way to make the cam look to the meshes and I need to know what I made wrong to cause that the box only appears after a click while the statsview disappears.
Here is my code:
[java]import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.eclipse.swt.widgets.Tree;
import org.omg.CORBA.INITIALIZE;
import com.jme3.app.SettingsDialog.SelectionListener;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
public class MyFileEditor extends SimpleApplication implements TreeSelectionListener {
JFileChooser fileChooser;
UserInterfaceWindow uiWindow;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
MyFileEditor myFileEditor= new MyFileEditor();
myFileEditor.uiWindow= new UserInterfaceWindow();
myFileEditor.uiWindow.tree.addTreeSelectionListener(myFileEditor);
AppSettings settings= new AppSettings(true);
settings.setHeight(myFileEditor.uiWindow.panel.getHeight());
settings.setWidth(myFileEditor.uiWindow.panel.getWidth());
myFileEditor.setSettings(settings);
myFileEditor.createCanvas();
JmeCanvasContext ctx = (JmeCanvasContext) myFileEditor.getContext();
ctx.setSystemListener(myFileEditor);
Dimension dim = new Dimension(myFileEditor.uiWindow.panel.getWidth(),myFileEditor.uiWindow.panel.getHeight());
ctx.getCanvas().setPreferredSize(dim);
myFileEditor.uiWindow.panel.add(ctx.getCanvas());
myFileEditor.startCanvas();
}
});
}
public void setUpFileChooser(){
fileChooser= new JFileChooser();
fileChooser.setDialogTitle("Select File");
FileNameExtensionFilter iffcdfFilter= new FileNameExtensionFilter("IFF/CDF Files","iff","CDF Files","cdf");
fileChooser.addChoosableFileFilter(iffcdfFilter);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileFilter(iffcdfFilter);
}
@Override
public void simpleInitApp() {
// TODO Auto-generated method stub
flyCam.setDragToRotate(true);
/*Box b = new Box(1, 1, 1); // create cube shape
Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
geom.setMaterial(mat); // set the cube's material
rootNode.attachChild(geom); */
System.out.println("simpleInitApp: "+rootNode.getChildren().toString());
}
//Gets the meshes list from a 3dmodel subfile and attach them to the rootnode
public void fillRootNodeWithMeshes(Model3DSubFile model3dSubFile,CustomRanAccFile iffRandAccFile) throws IOException{
List<Mesh> meshList=IffUtils.createJme3MeshPartsListFrom3DSubfile(model3dSubFile, iffRandAccFile);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md"); // create
mat.setColor("Color", ColorRGBA.Gray);
List<Geometry> geomList= new ArrayList<Geometry>();
//Creates the Geometrys List
int i=0;
Geometry geom;
for(Mesh mesh: meshList){
geom=new Geometry("Part"+i,mesh);
i+=1;
geom.setMaterial(mat);
geomList.add(geom);
}
//Create a node to manage all the parts as a group
Node modelNode= new Node("Model3dNode");
//Attach the Geometrys to the root node
for(Geometry geometry: geomList){
modelNode.attachChild(geometry);
}
System.out.println("modelNode world translation: "+modelNode.getWorldTranslation().toString());
//Clear the rootNode end guiNode from other results
guiNode.detachAllChildren();
//Atach the Model3DNode to the rootNode
rootNode.attachChild(modelNode);
Box b = new Box(1, 1, 1); // create cube shape
Geometry geom2 = new Geometry("Box", b); // create cube geometry from the shape
geom2.setMaterial(mat); // set the cube's material
rootNode.attachChild(geom2);
System.out.println("fillRootNodeWithMeshes: "+rootNode.getChildren().toString());
}
/* (non-Javadoc)
* @see com.jme3.app.SimpleApplication#simpleUpdate(float)
*/
@Override
public void simpleUpdate(float tpf) {
// TODO Auto-generated method stub
super.simpleUpdate(tpf);
}
public void valueChanged(TreeSelectionEvent e) {
// TODO Auto-generated method stub
//Clear the scene
for(Spatial spatial:rootNode.getChildren()){
if(spatial.getName()=="Model3dNode"){
rootNode.detachChild(spatial);
}
}
//Returns the last path element of the selection.
//This method is useful only when the selection model allows a single selection.
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
uiWindow.tree.getLastSelectedPathComponent();
if (node == null)
//Nothing is selected.
return;
Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
IffBaseSubFile subFile = (IffBaseSubFile)nodeInfo;
//if is an instance of Model3dSubfile draws the model
if(subFile instanceof Model3DSubFile){
try {
fillRootNodeWithMeshes((Model3DSubFile)subFile,uiWindow.inputRandAccFile);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(subFile instanceof TextureInfoSubFile){//if it is instance of TextureModelSubFile draws it
}
}
}
}
[/java]