📄 splashscreen.java
字号:
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import com.mascotcapsule.micro3d.v3.*;
/**
* Loads and then displays a 3d model on a Canvas as long as
* the variable isInitialized = false
*
* This code is part of the Tips & Tricks section at
* www.SonyEricsson.com/developer
*
* Written by J鰊s Weimarck, 2004
* Based on code from HI Corp
*/
public class SplashScreen extends Thread{
public static boolean splashIsShown;
public static boolean isInitialized;
private SplashCanvas3D splashCanvas;
private Display myDisplay;
// Varibales used for the 3D Model
private Figure figure;
private ActionTable action;
private AffineTrans camera;
private Image backgroundImage;
private FigureLayout layout;
private Effect3D effect;
private Graphics3D g3 = new Graphics3D();
AffineTrans figureTrans_x = new AffineTrans();
AffineTrans figureTrans_y = new AffineTrans();
private Timer myTimer;
AffineTrans figureTrans = new AffineTrans();
private int nowFrame = 0;
private int maxFrame = 0;
private int rotationX;
private int rotationY;
private int bgColor = 0x333377;
public SplashScreen(Display aDisplay){
myDisplay = aDisplay;
start();
}
public void run(){
try{
splashCanvas = new SplashCanvas3D (this);
init3DModel();
showSplash();
}catch(java.io.IOException e){
System.out.println(e.toString());
}
}
/**
* Shows the Splash and steps through the frames as long as the
* game initilaization is not finished
*/
public void showSplash(){
myDisplay.setCurrent(splashCanvas);
//Create a new timer that will refresh the screen with a framerate of 10 fps
myTimer = new Timer();
myTimer.scheduleAtFixedRate(new TimerTask(){
public void run() {
// Stop the Splash when initialization has finished
if(isInitialized){
myTimer.cancel();
}
splashCanvas.repaint();
nowFrame += 65536*3;
if (nowFrame>=maxFrame) {
if (maxFrame==0){
nowFrame = 0;
}else{
nowFrame %= maxFrame;
}
}
}
}, 0L, 100L);
}
/**
* Inits the 3D Model (Figure, Effects, Camera...)
*/
private void init3DModel()throws IOException{
setupFigure();
backgroundImage = Image.createImage("/background.png");
setupEffect();
initViewParams();
camera = new AffineTrans();
camera.lookAt( new Vector3D(0, 0, 2048), //position
new Vector3D(0, 0, -2048), //look at
new Vector3D(0, 4096, 0)); //up (orientation)
}
/**
* setupFigure()
*
* Loads:
* 3D model data (.mbac)
* texture (.bmp)
* actiontable (figure.mtra)
*/
private void setupFigure() throws IOException {
figure = new Figure("/box2.mbac");
Texture tx = new Texture("/box2.bmp", true);
try {
action = new ActionTable("/box2.mtra");
maxFrame = action.getNumFrames(0);
}catch (Exception e) {
System.out.println(e.getMessage());
}
figure.setTexture(tx);
}
/**
* setupEffect()
* Generate the rendering data object with light
* and normal shading
*/
private void setupEffect() throws IOException
{
Light lgt = new Light(new Vector3D(2, 3, 7),4096*80/100, 4096*20/100);
effect = new Effect3D( lgt,
Effect3D.NORMAL_SHADING,
false, // semitransparent
null); // refSpehereTexture
}
/**
* initViewParams()
* Initialize the orientation, position and size
* parameters
*/
void initViewParams(){
//Center the figure and increase the size
//Start displaying the figure
int centerX = (splashCanvas.getWidth() >> 1);
int centerY = (splashCanvas.getHeight() >> 1);
int scale = (1024*4) +300; // set the scale
rotationX = 0;
rotationY = 256;
layout = new FigureLayout();
//centering on X axis, move up on Y axis...
layout.setCenter(centerX-205, centerY+15);
// scale a bit...
layout.setScale(scale, scale);
}
/**
* Inner Class responsible for drawing the 3d model on the Canvas
*/
private class SplashCanvas3D extends Canvas
{
private SplashScreen parent;
public SplashCanvas3D(SplashScreen aSplashScreen) throws IOException {
parent = aSplashScreen;
}
/***********************************************
* paint(Graphics g)
* The paint function is called by the Timers
* repaint call.
* Resets the screen and draws the background image.
* Render the figure in the midle of the screen
************************************************/
protected void paint(Graphics g)
{
//Clear the screan and set a background color.
g.setColor(bgColor);
g.fillRect(0, 0, getWidth(), getHeight());
//Set background image
g.drawImage(backgroundImage, 0, 0, g.TOP|g.LEFT);
//Calculate the new rotation parameters.
figureTrans_x.setRotationX(rotationX);
figureTrans_y.setRotationY(rotationY);
//Calculate the product of the transformation matrix
figureTrans.mul(figureTrans_x, figureTrans_y) ;
figureTrans_x.mul(camera, figureTrans);
//Local transformation
layout.setAffineTrans(figureTrans_x);
//Update pose of figure
figure.setPosture(action, 0, nowFrame);
try
{
//Get the Graphics 3D object and render the
//figure in the center of the screen with lights.
g3.bind(g);
g3.renderFigure(figure, 0, 0, layout, effect);
//Flush to screen
g3.flush();
//Release the Graphics3D object
g3.release(g);
} catch (Exception e) {}
parent.splashIsShown=true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -