📄 package-summary.html
字号:
<span class="example_modifier">private</span> <span class="example_class">Image</span> iImage;
<span class="example_comment">/**
* Construct the Displayable.
*/</span>
<span class="example_modifier">public</span> <span class="example_class">MyCanvas</span>() {
<span class="example_comment">// set up this Displayable to listen to command events</span>
setCommandListener(<span class="example_reserved">new</span> <span class="example_class">CommandListener</span>() {
<span class="example_modifier">public</span> <span class="example_type">void</span> commandAction(<span class="example_class">Command</span> c, <span class="example_class">Displayable</span> d) {
<span class="example_control">if</span> (c.getCommandType() == <span class="example_class">Command</span>.<span class="example_constant">EXIT</span>) {
<span class="example_comment">// exit the MIDlet</span>
<span class="example_class">MIDletMain</span>.quitApp();
}
}
});
<span class="example_control">try</span> {
init();
}
<span class="example_control">catch</span>(<span class="example_class">Exception</span> e) {
e.printStackTrace();
}
}
<span class="example_comment">/**
* Component initialization.
*/</span>
<span class="example_modifier">private</span> <span class="example_type">void</span> init() <span class="example_proclamation">throws</span> <span class="example_class">Exception</span> {
<span class="example_comment">// add the Exit command</span>
addCommand(<span class="example_reserved">new</span> <span class="example_class">Command</span>(<span class="example_string">"Exit"</span>, <span class="example_class">Command</span>.<span class="example_constant">EXIT</span>, 1));
<span class="example_comment">// get the singleton Graphics3D instance</span>
iG3D = <span class="example_class">Graphics</span>3D.getInstance();
<span class="example_comment">// create a camera</span>
iCamera = <span class="example_reserved">new</span> <span class="example_class">Camera</span>();
iCamera.setPerspective( 60.0f, <span class="example_comment">// field of view</span>
(<span class="example_type">float</span>)getWidth()/ (<span class="example_type">float</span>)getHeight(), <span class="example_comment">// aspectRatio</span>
1.0f, <span class="example_comment">// near clipping plane</span>
1000.0f ); <span class="example_comment">// far clipping plane</span>
<span class="example_comment">// create a light</span>
iLight = <span class="example_reserved">new</span> <span class="example_class">Light</span>();
iLight.setColor(0xffffff); <span class="example_comment">// white light</span>
iLight.setIntensity(1.25f); <span class="example_comment">// overbright</span>
<span class="example_comment">// init some arrays for our object (cube)</span>
<span class="example_comment">// Each line in this array declaration represents a triangle strip for</span>
<span class="example_comment">// one side of a cube. The only primitive we can draw with is the</span>
<span class="example_comment">// triangle strip so if we want to make a cube with hard edges we</span>
<span class="example_comment">// need to construct one triangle strip per face of the cube.</span>
<span class="example_comment">// 1 * * * * * 0</span>
<span class="example_comment">// * * *</span>
<span class="example_comment">// * * *</span>
<span class="example_comment">// * * *</span>
<span class="example_comment">// 3 * * * * * 2</span>
<span class="example_comment">// The ascii diagram above represents the vertices in the first line</span>
<span class="example_comment">// (the first tri-strip)</span>
<span class="example_type">short</span>[] vert = {
10, 10, 10, -10, 10, 10, 10,-10, 10, -10,-10, 10, <span class="example_comment">// front</span>
-10, 10,-10, 10, 10,-10, -10,-10,-10, 10,-10,-10, <span class="example_comment">// back</span>
-10, 10, 10, -10, 10,-10, -10,-10, 10, -10,-10,-10, <span class="example_comment">// left</span>
10, 10,-10, 10, 10, 10, 10,-10,-10, 10,-10, 10, <span class="example_comment">// right</span>
10, 10,-10, -10, 10,-10, 10, 10, 10, -10, 10, 10, <span class="example_comment">// top</span>
10,-10, 10, -10,-10, 10, 10,-10,-10, -10,-10,-10 }; <span class="example_comment">// bottom</span>
<span class="example_comment">// create a VertexArray to hold the vertices for the object</span>
<span class="example_class">VertexArray</span> vertArray = <span class="example_reserved">new</span> <span class="example_class">VertexArray</span>(vert.length / 3, 3, 2);
vertArray.set(0, vert.length/3, vert);
<span class="example_comment">// The per-vertex normals for the cube; these match with the vertices</span>
<span class="example_comment">// above. Each normal is perpendicular to the surface of the object at</span>
<span class="example_comment">// the corresponding vertex.</span>
<span class="example_type">byte</span>[] norm = {
0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,
0, 0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127,
-127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0,
127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0,
0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,
0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127, 0 };
<span class="example_comment">// create a vertex array for the normals of the object</span>
<span class="example_class">VertexArray</span> normArray = <span class="example_reserved">new</span> <span class="example_class">VertexArray</span>(norm.length / 3, 3, 1);
normArray.set(0, norm.length/3, norm);
<span class="example_comment">// per vertex texture coordinates</span>
<span class="example_type">short</span>[] tex = {
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1 };
<span class="example_comment">// create a vertex array for the texture coordinates of the object</span>
<span class="example_class">VertexArray</span> texArray = <span class="example_reserved">new</span> <span class="example_class">VertexArray</span>(tex.length / 2, 2, 2);
texArray.set(0, tex.length/2, tex);
<span class="example_comment">// the length of each triangle strip </span>
<span class="example_type">int</span>[] stripLen = { 4, 4, 4, 4, 4, 4 };
<span class="example_comment">// create the VertexBuffer for our object</span>
<span class="example_class">VertexBuffer</span> vb = iVb = <span class="example_reserved">new</span> <span class="example_class">VertexBuffer</span>();
vb.setPositions(vertArray, 1.0f, <span class="example_literal">null</span>); <span class="example_comment">// unit scale, zero bias</span>
vb.setNormals(normArray);
vb.setTexCoords(0, texArray, 1.0f, <span class="example_literal">null</span>); <span class="example_comment">// unit scale, zero bias</span>
<span class="example_comment">// create the index buffer for our object (this tells how to</span>
<span class="example_comment">// create triangle strips from the contents of the vertex buffer).</span>
iIb = <span class="example_reserved">new</span> <span class="example_class">TriangleStripArray</span>( 0, stripLen );
<span class="example_comment">// load the image for the texture</span>
iImage = <span class="example_class">Image</span>.createImage( <span class="example_string">"/texture.png"</span> );
<span class="example_comment">// create the Image2D (we need this so we can make a Texture2D)</span>
<span class="example_class">Image</span>2D image2D = <span class="example_reserved">new</span> <span class="example_class">Image</span>2D( <span class="example_class">Image</span>2D.<span class="example_constant">RGB</span>, iImage );
<span class="example_comment">// create the Texture2D and enable mipmapping</span>
<span class="example_comment">// texture color is to be modulated with the lit material color</span>
<span class="example_class">Texture</span>2D texture = <span class="example_reserved">new</span> <span class="example_class">Texture</span>2D( image2D );
texture.setFiltering(<span class="example_class">Texture</span>2D.<span class="example_constant">FILTER_NEAREST</span>,
<span class="example_class">Texture</span>2D.<span class="example_constant">FILTER_NEAREST</span>);
texture.setWrapping(<span class="example_class">Texture</span>2D.<span class="example_constant">WRAP_CLAMP</span>,
<span class="example_class">Texture</span>2D.<span class="example_constant">WRAP_CLAMP</span>);
texture.setBlending(<span class="example_class">Texture</span>2D.<span class="example_constant">FUNC_MODULATE</span>);
<span class="example_comment">// create the appearance</span>
iAppearance = <span class="example_reserved">new</span> <span class="example_class">Appearance</span>();
iAppearance.setTexture(0, texture);
iAppearance.setMaterial(iMaterial);
iMaterial.setColor(<span class="example_class">Material</span>.<span class="example_constant">DIFFUSE</span>, 0xFFFFFFFF); <span class="example_comment">// white</span>
iMaterial.setColor(<span class="example_class">Material</span>.<span class="example_constant">SPECULAR</span>, 0xFFFFFFFF); <span class="example_comment">// white</span>
iMaterial.setShininess(100.0f);
iBackground.setColor(0xf54588); <span class="example_comment">// set the background color</span>
}
<span class="example_comment">/**
* Paint the scene.
*/</span>
<span class="example_modifier">protected</span> <span class="example_type">void</span> paint(<span class="example_class">Graphics</span> g) {
<span class="example_comment">// Bind the Graphics of this Canvas to our Graphics3D. The</span>
<span class="example_comment">// viewport is automatically set to cover the entire clipping</span>
<span class="example_comment">// rectangle of the Graphics object. The parameters indicate</span>
<span class="example_comment">// that z-buffering, dithering and true color rendering are</span>
<span class="example_comment">// enabled, but antialiasing is disabled.</span>
iG3D.bindTarget(g, <span class="example_literal">true</span>,
<span class="example_class">Graphics</span>3D.<span class="example_constant">DITHER</span> |
<span class="example_class">Graphics</span>3D.<span class="example_constant">TRUE_COLOR</span>);
<span class="example_comment">// clear the color and depth buffers</span>
iG3D.clear(iBackground);
<span class="example_comment">// set up the camera in the desired position</span>
<span class="example_class">Transform</span> transform = <span class="example_reserved">new</span> <span class="example_class">Transform</span>();
transform.postTranslate(0.0f, 0.0f, 30.0f);
iG3D.setCamera(iCamera, transform);
<span class="example_comment">// set up a "headlight": a directional light shining</span>
<span class="example_comment">// from the direction of the camera</span>
iG3D.resetLights();
iG3D.addLight(iLight, transform);
<span class="example_comment">// update our transform (this will give us a rotating cube)</span>
iAngle += 1.0f;
iTransform.setIdentity();
iTransform.postRotate(iAngle, <span class="example_comment">// rotate 1 degree per frame</span>
1.0f, 1.0f, 1.0f); <span class="example_comment">// rotate around this axis</span>
<span class="example_comment">// Render our cube. We provide the vertex and index buffers</span>
<span class="example_comment">// to specify the geometry; the appearance so we know what</span>
<span class="example_comment">// material and texture to use; and the transform to tell</span>
<span class="example_comment">// where to render the object</span>
iG3D.render(iVb, iIb, iAppearance, iTransform);
<span class="example_comment">// flush</span>
iG3D.releaseTarget();
}
}</pre><div class="example_title">(2) Immediate mode example MIDlet: Class MIDletMain.
</div><pre class="example"> <span class="example_proclamation">import</span> javax.microedition.midlet.*;
<span class="example_proclamation">import</span> javax.microedition.lcdui.*;
<span class="example_proclamation">import</span> java.util.*;
<span class="example_modifier">public</span> <span class="example_proclamation">class</span> <span class="example_class">MIDletMain</span> <span class="example_proclamation">extends</span> <span class="example_class">MIDlet</span>
{
<span class="example_modifier">static</span> <span class="example_class">MIDletMain</span> instance;
<span class="example_class">MyCanvas</span> displayable = <span class="example_reserved">new</span> <span class="example_class">MyCanvas</span>();
<span class="example_class">Timer</span> iTimer = <span class="example_reserved">new</span> <span class="example_class">Timer</span>();
<span class="example_comment">/**
* Construct the midlet.
*/</span>
<span class="example_modifier">public</span> <span class="example_class">MIDletMain</span>() {
<span class="example_reserved">this</span>.instance = <span class="example_reserved">this</span>;
}
<span class="example_comment">/**
* Main method.
*/</span>
<span class="example_modifier">public</span> <span class="example_type">void</span> startApp() {
<span class="example_class">Display</span>.getDisplay(<span class="example_reserved">this</span>).setCurrent(displayable);
iTimer.schedule( <span class="example_reserved">new</span> <span class="example_class">MyTimerTask</span>(), 0, 40 );
}
<span class="example_comment">/**
* Handle pausing the MIDlet.
*/</span>
<span class="example_modifier">public</span> <span class="example_type">void</span> pauseApp() {
}
<span class="example_comment">/**
* Handle destroying the MIDlet.
*/</span>
<span class="example_modifier">public</span> <span class="example_type">void</span> destroyApp(<span class="example_type">boolean</span> unconditional) {
}
<span class="example_comment">/**
* Quit the MIDlet.
*/</span>
<span class="example_modifier">public</span> <span class="example_modifier">static</span> <span class="example_type">void</span> quitApp() {
instance.destroyApp(<span class="example_literal">true</span>);
instance.notifyDestroyed();
instance = <span class="example_literal">null</span>;
}
<span class="example_comment">/**
* Our timer task for providing animation.
*/</span>
<span class="example_proclamation">class</span> <span class="example_class">MyTimerTask</span> <span class="example_proclamation">extends</span> <span class="example_class">TimerTask</span> {
<span class="example_modifier">public</span> <span class="example_type">void</span> run() {
<span class="example_control">if</span>( displayable != <span class="example_literal">null</span> ) {
displayable.repaint();
}
}
}
}</pre><div class="example_title">(3) Retained mode example MIDlet.
</div><pre class="example"> <span class="example_proclamation">import</span> javax.microedition.midlet.<span class="example_class">MIDlet</span>;
<span class="example_proclamation">import</span> javax.microedition.midlet.<span class="example_class">MIDletStateChangeException</span>;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -