<分区>
如何绘制一个简单的红色框?
<分区>
如何绘制一个简单的红色框?
最佳答案
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
Material :http://hub.jmonkeyengine.org/wiki/doku.php/jme3:intermediate:how_to_use_materials
作为替代方案,您也可以用自定义图像替换鼠标光标。
参见 http://hub.jmonkeyengine.org/forum/topic/custom-mouse-cursor-committed/
关于java - 用 JMonkey 画一个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338978/