Line is one of the built-in mesh shapes.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZnpEA2vjzWij2x52xiozx-wqEVO5hKExVOMNPsaUkHe06prGW8M6aghNUp2V3uQWbWWeJIwUDNhnZWxTSh09A6lmjtXaqKsUJK8y3SL1Uap6aeb7_p_Sx6AUxJwEYt1ok3pkRi1_dOY3X/s1600/java36+-+1.png)
Line requires the starting point and ending point, both being Vector3f objects. We can also set Line properties such as the line width.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhw4YTnhaZIHVoBQgxTnyOZnpWj2IEzcGlxCXBhBTfMgTK3wpgNFDdhdF-U1Sh3iIEqE6mu8AOyrldvppXZu3BDBLYdDgrV3Jnz_yb_XBLk0Lwhh9tccZsld4U2VpRYQksp9gCRrs8beYyv/s1600/java36+-+2.png)
// *** 1. Start (Line objects) Line line = new Line(Vector3f.ZERO,new Vector3f(1,1,0)); line.setLineWidth(5); Line line1 = new Line(Vector3f.ZERO,new Vector3f(1,-1f,0)); line1.setLineWidth(5); Line line2 = new Line(Vector3f.ZERO,new Vector3f(1,0,3)); line2.setLineWidth(3); // *** 1. End
Three Geometries are created from the line shapes. We have to use a String identifier and mesh object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmUq1NZ5-deRwhQ_fp6IW9HQNL__oGg60yIIVZX0EhsoEq8HBu8-dIpRMlgEvdqz7f8WwRLF14mhFpZar8TjSuolzT_LTLEWbkBdfw6kgHV6ZkCcrwkavV5zYqnFDjA6NXZCwlzTAD82Yd/s1600/java36+-+3.png)
// *** 2. Start (Geometry objects) Geometry gline = new Geometry("upLine",line); Geometry gline1 = new Geometry("downLine",line1); Geometry gline2 = new Geometry("horizLine",line2); // *** 2. End
The three lines are set to different colors.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhC2Al5GMHPuznbyb98LDnYQizLfxrK5VoZGqBACD8vHLgPlxDGVMexgYilE5ga4-riW6KcEQx_SgzCyi999ICHyOW0Qr03EB8Rr7QzUKzPVphEDevVxrbO3iSxjsb9Ve9pwhLBXE7T0xpS/s1600/java36+-+4.png)
// *** 3. Start (3 Materials, Different Colors) Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Blue); Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat1.setColor("Color", ColorRGBA.Red); Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat2.setColor("Color", ColorRGBA.Green); gline.setMaterial(mat); gline1.setMaterial(mat1); gline2.setMaterial(mat2); // *** 3. End
We clone the last 3 Geometries, and Scale and Translate them. If you did not scale them, it would be hard to see them.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8ST9KYehtWVrfLz86WfncN2Z5mkJc-L0p_9-CG7CK9ILAgtl-30a046cdeephRgVLPJW3iptFDuzjxqWqdhUcB4tSqoiOpApYV7a9ffvSUvTkTukrcu_y_27X_TR_iYp3jPZN0hPa6OM5/s1600/java36+-+5.png)
// *** 4. Start (clones with scale and translations) Geometry gline3 = new Geometry("line3"); Geometry gline4 = new Geometry("line4"); Geometry gline5 = new Geometry("line5"); gline3 = gline.clone(); gline3.setLocalScale(20); gline3.setLocalTranslation(x, y, 0); gline4 = gline1.clone(); gline4.setLocalScale(20); gline4.setLocalTranslation(x, y, 0); gline5 = gline2.clone(); gline5.setLocalScale(20); gline5.setLocalTranslation(x, y, 0); // *** 4. End
package mygame; import com.jme3.app.SimpleApplication; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.renderer.RenderManager; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Line; public class JMonkey36 extends SimpleApplication { public static void main(String[] args) { JMonkey36 app = new JMonkey36(); app.start(); } @Override public void simpleInitApp() { setDisplayFps(false); setDisplayStatView(false); viewPort.setBackgroundColor(ColorRGBA.White); // *** 1. Start (Line objects) Line line = new Line(Vector3f.ZERO,new Vector3f(1,1,0)); line.setLineWidth(5); Line line1 = new Line(Vector3f.ZERO,new Vector3f(1,-1f,0)); line1.setLineWidth(5); Line line2 = new Line(Vector3f.ZERO,new Vector3f(1,0,3)); line2.setLineWidth(3); // *** 1. End // *** 2. Start (Geometry objects) Geometry gline = new Geometry("upLine",line); Geometry gline1 = new Geometry("downLine",line1); Geometry gline2 = new Geometry("horizLine",line2); // *** 2. End // *** 3. Start (3 Materials, Different Colors) Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setColor("Color", ColorRGBA.Blue); Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat1.setColor("Color", ColorRGBA.Red); Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat2.setColor("Color", ColorRGBA.Green); gline.setMaterial(mat); gline1.setMaterial(mat1); gline2.setMaterial(mat2); // *** 3. End int x = settings.getWidth()/4; // 2dgui traslated int y = settings.getHeight()/4; // to (x,y) // *** 4. Start (clones with scale and translations) Geometry gline3 = new Geometry("line3"); Geometry gline4 = new Geometry("line4"); Geometry gline5 = new Geometry("line5"); gline3 = gline.clone(); gline3.setLocalScale(20); gline3.setLocalTranslation(x, y, 0); gline4 = gline1.clone(); gline4.setLocalScale(20); gline4.setLocalTranslation(x, y, 0); gline5 = gline2.clone(); gline5.setLocalScale(20); gline5.setLocalTranslation(x, y, 0); // *** 4. End rootNode.attachChild(gline); rootNode.attachChild(gline1); rootNode.attachChild(gline2); guiNode.attachChild(gline3); guiNode.attachChild(gline4); guiNode.attachChild(gline5); } @Override public void simpleUpdate(float tpf) { } @Override public void simpleRender(RenderManager rm) { } }
No comments:
Post a Comment