📄 initializersplashscreen.java
字号:
//#condition polish.midp
/*
* Created on 23-Mar-2005 at 18:00:01.
*
* Copyright (c) 2005 Robert Virkus / Enough Software
*
* This file is part of J2ME Polish.
*
* J2ME Polish is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* J2ME Polish is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with J2ME Polish; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Commercial licenses are also available, please
* refer to the accompanying LICENSE.txt or visit
* http://www.j2mepolish.org for details.
*/
// package de.enough.polish.ui.splash;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
//import de.enough.polish.ui.AccessibleCanvas;
//#if polish.usePolishGui
//import de.enough.polish.ui.Background;
//#endif
//import de.enough.polish.util.TextUtil;
/**
* <p>Provides a SplashScreen that intialises the real application in a background thread.</p>
*
* <p>Copyright (c) 2005, 2006 Enough Software</p>
* <pre>
* history
* 23-Mar-2005 - rob creation
* </pre>
* @author Robert Virkus, j2mepolish@enough.de
*/
public class InitializerSplashScreen
//#ifdef polish.classes.fullscreen:defined
//#= extends ${polish.classes.fullscreen}
//#else
extends Canvas
//#endif
implements Runnable, AccessibleCanvas
{
//#if polish.classes.ApplicationInitializer:defined
//#= private final ${ classname( polish.classes.ApplicationInitializer) } initializer;
//#else
private final ApplicationInitializer initializer;
//#endif
private boolean isInitialized;
private Displayable nextScreen;
private final Display display;
//#ifdef polish.classes.SplashView:defined
//#= private final ${ classname( polish.classes.SplashView) } view;
//#else
private final Image image;
private final String readyMessage;
private final int messageColor;
private final int backgroundColor;
//#endif
//#if false
//# private final SplashView view;
//#endif
private String message;
//#if polish.usePolishGui
private Background background;
//#endif
private boolean isStarted;
//#if !polish.classes.SplashView:defined
/**
* Creates a new InitializerSplashScreen using the internal default view.
* The message will be shown in the default font.
*
* @param display the display responsible for switching screens
* @param image the image that is shown in the center
* @param backgroundColor the background color, e.g. white: 0xFFFFFF
* @param readyMessage the message that is displayed when the application has been initialized
* @param messageColor the color for the message, e.g. black: 0
* @param initializer the application initializer that will be called in a background thread
*/
//#if polish.classes.ApplicationInitializer:defined
//#= public InitializerSplashScreen( Display display, Image image, int backgroundColor, String readyMessage, int messageColor, ${ classname( polish.classes.ApplicationInitializer) } initializer )
//#else
public InitializerSplashScreen( Display display, Image image, int backgroundColor, String readyMessage, int messageColor, ApplicationInitializer initializer )
//#endif
{
super();
//#if polish.midp2 && !(polish.Bugs.fullScreenInShowNotify || polish.Bugs.fullScreenInPaint)
super.setFullScreenMode( true );
//#endif
this.display = display;
this.image = image;
this.backgroundColor = backgroundColor;
this.readyMessage = readyMessage;
this.messageColor = messageColor;
this.initializer = initializer;
//#if false
//# this.view = null;
//#endif
}
//#endif
//#ifdef polish.classes.SplashView:defined
//#if polish.classes.ApplicationInitializer:defined
//#= public InitializerSplashScreen( Display display, ${ classname( polish.classes.SplashView) } view, ${ classname( polish.classes.ApplicationInitializer) } initializer )
//#else
//#= public InitializerSplashScreen( Display display, ${ classname( polish.classes.SplashView) } view, ApplicationInitializer initializer )
//#endif
//#if false
//# public InitializerSplashScreen( Display display, SplashView view, ApplicationInitializer initializer )
//#endif
//# {
//#if polish.midp2 && !(polish.Bugs.fullScreenInShowNotify || polish.Bugs.fullScreenInPaint)
//# super.setFullScreenMode( true );
//#endif
//# this.view = view;
//# this.display = display;
//# this.initializer = initializer;
//#if false
//# this.image = null;
//# this.backgroundColor = 0;
//# this.readyMessage = null;
//# this.messageColor = 0;
//#endif
//#
//# }
//#endif
/**
* Sets a message that is displayed immediately.
* This call is ignored when a SplashView is used.
*
* @param message the new message, null when no message should be shown.
*/
public void setMessage( String message ) {
this.message = message;
repaint();
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
*/
public void paint(Graphics g) {
//#if polish.Bugs.fullScreenInShowNotify || polish.Bugs.fullScreenInPaint
//# super.setFullScreenMode(true);
//#endif
//#ifdef polish.FullCanvasSize:defined
//#= int height = ${polish.FullCanvasHeight};
//#= int width = ${polish.FullCanvasWidth};
//#else
int height = getHeight();
int width = getWidth();
//#endif
//#ifdef polish.classes.SplashView:defined
//# this.view.paint( width, height, this.isInitialized, g);
//#else
g.setColor( this.backgroundColor );
g.fillRect( 0, 0, width, height );
g.drawImage(this.image, width/2, height/2, Graphics.HCENTER | Graphics.VCENTER );
if (this.isInitialized) {
g.setColor( this.messageColor );
Font font = Font.getDefaultFont();
String[] lines = TextUtil.wrap( this.readyMessage, font, width - 10, width - 10 );
int y = height - ( lines.length * ( font.getHeight() + 1 ) );
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
g.drawString( line, width/2, y, Graphics.TOP | Graphics.HCENTER );
y += font.getHeight() + 1;
}
} else if (this.message != null) {
g.setColor( this.messageColor );
Font font = Font.getDefaultFont();
String[] lines = TextUtil.wrap( this.message, font, width - 10, width - 10 );
int y = height - ( lines.length * ( font.getHeight() + 1 ) );
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
g.drawString( line, width/2, y, Graphics.TOP | Graphics.HCENTER );
y += font.getHeight() + 1;
}
}
//#endif
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
this.nextScreen = this.initializer.initApp();
//#if !polish.classes.SplashView:defined
if (this.readyMessage == null && this.nextScreen != null) {
this.display.setCurrent( this.nextScreen );
return;
}
//#endif
this.isInitialized = true;
repaint();
if (this.nextScreen == null) {
return;
}
} catch (Exception e) {
//#debug error
//# System.out.println("Unable to call initApp()" + e );
}
}
public void keyPressed(int keyCode) {
if (this.isInitialized && this.nextScreen != null ) {
this.display.setCurrent( this.nextScreen );
}
}
public void keyReleased(int keyCode) {
// ignore
}
public void keyRepeated(int keyCode) {
// ignore
}
//#ifdef polish.hasPointerEvents
//# public void pointerPressed(int x, int y) {
//# keyPressed( 0 );
//# }
//#endif
//#if polish.midp2
public void sizeChanged( int width, int height ) {
// ignore
}
//#endif
public void showNotify() {
if (this.isStarted) {
return;
}
this.isStarted = true;
Thread thread = new Thread( this );
thread.start();
}
public void hideNotify() {
this.isStarted = false;
}
//#if polish.usePolishGui
/**
* Retrieves the background.
* Warning: this method is only available when the J2ME Polish GUI is used, check for the polish.usePolishGui preprocessing symbol
*
* @return the background
*/
public Background getBackground() {
return this.background;
}
//#endif
//#if polish.usePolishGui
/**
* Sets the background of this splash screen.
* Warning: this method is only available when the J2ME Polish GUI is used, check for the polish.usePolishGui preprocessing symbol
*
* @param background the background to set
*/
public void setBackground(Background background) {
this.background = background;
}
//#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -