2014-12-07 81 views
1

如何绘制一个简单的红色框?用JMonkey绘制矩形

+0

jMonkeyEngine显示的所有内容最终都是Mesh。你的意思是一个填充框或只是轮廓? – 1000ml 2014-12-07 23:08:18

回答

3

Quad是一个预定义的网格(或形状),它具有高度,宽度并位于X/Y平面上。渲染网格需要一个Geometry,并且Material将定义它的颜色。您还必须将矩形的位置与鼠标光标的位置同步。所有这些都是必要的,你总是会得到最少量的代码。

public void simpleInitApp() { 
    // Create red transparent material 
    Material mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); 
    mat.setColor("Color", new ColorRGBA(1, 0, 0, 0.5f)); // 0.5f is the alpha value 

    // Activate the use of the alpha channel 
    mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); 

    // Create rectangle of size 10x10 
    Geometry mouseRect = new Geometry("MouseRect", new Quad(10, 10)); 
    mouseRect.setMaterial(mat); 
    guiNode.attachChild(mouseRect); 
} 

public void simpleUpdate(float tpf) { 
    // Move the rectangle to the cursor position 
    Vector2f cursor = inputManager.getCursorPosition(); 
    guiNode.getChild("MouseRect").setLocalTranslation(cursor.x, cursor.y, 0); 
} 

矩形的原点位于其左下角。您可能希望使用偏移量将光标位置处的矩形居中:setLocalTranslation(cursor.x - 5, cursor.y - 5, 0)

更多有关
形状:http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:shape
材料:http://hub.jmonkeyengine.org/wiki/doku.php/jme3:intermediate:how_to_use_materials

作为替代方案,你也可以用自定义图像替换鼠标光标。
请参阅http://hub.jmonkeyengine.org/forum/topic/custom-mouse-cursor-committed/

+0

我在底部添加了一个替代方案。也谢谢你 :) – 1000ml 2014-12-08 01:06:47