📄 splashscreen.java
字号:
/*************************************************************
*
* The Mobile Robotics Project
* - mobilerobotics.sourceforge.net
*
* This file was created 2005-01-05
* More information in MobileRoboticsMidlet.java
*
*************************************************************/
/*
SplashScreen.java::v0.2::1
This class shows the splashScreen upon loading and any
error it might bring. There isn't that much of commentation
in this class. And I strongly advice not looking at it
when need of a code example. The class is loaded with pixel-
referenses needed to draw the lines, boxes and such things
only neccessary for our project. YOU WILL ONLY GET CONFUSED*.
A box is not a box but four lines.
In theory the only thing this class does is showing the
loading bar. This is due to a loop in run() wich moves
the bar one step to the right and updates (and loops this).
* Many of these values are multiplications and fixed by
trial and error. There is no logical explanation
why some of them are as they are.
If you want an example of drawCanvas use ControllRobot.java
or check the J2ME Gaming book.
Only thing that really is easy to change is the bar speed,
change the (int) delay.The higher it is the slower is the
speed.
If you must look in this file, for example want to use the loading
bar I suggest checking the line ~211 (a comment wich states
where the bar starts and ends)
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class SplashScreen extends GameCanvas implements Runnable {
private boolean isPlay; // Loading runs when isPlay is true
private long delay; // This will make the thread stop for a short period of time every time the bar has moved. It is to controll the bars speed. Try lowering it and see what happens.
private int currentX, currentY; // To hold current position of the 'mobileRobotics' text
private int width; // To hold screen width
private int height; // To hold screen height
// Fonts. Check J2ME Gaming book (see MobileRobticsMidlet.java for more information)
static final Font boldFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL);
static final Font regularFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
Bluetooth bluetooth;
Image logo;
Image init;
Image conn;
Image search;
Image reg;
Image error;
// Constructor and initialization
public SplashScreen(Bluetooth bluet) {
super(true);
width = getWidth();
height = getHeight();
delay = 1;
currentX = - 26 ;
bluetooth = bluet;
}
// Automatically start thread for game loop
public void start() {
System.err.println(" Inited: SplashScreen thread");
// Starting self
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop()
{
isPlay = false;
}
public void run() {
Graphics g = getGraphics();
try { logo = Image.createImage ("/logo.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { search = Image.createImage ("/search.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { init = Image.createImage ("/init.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { conn = Image.createImage ("/connecting.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
try { reg = Image.createImage ("/reg.png"); } catch ( Exception e ) { throw new RuntimeException ("Unable to load Image: "+e); }
while (isPlay == true) {
if(currentX < 84)
{
currentX = currentX + 2;
}else{
currentX = - 26;
}
drawScreen(g);
// This will make the thread stop for a short period of time every time the bar has moved. It is to controll the bars speed. Try setting 'delay' to 0 and see what happens.
try { Thread.sleep(delay); } catch (InterruptedException ie) {}
}
}
public boolean errorPressAnyKey()
{
/*
Method is called when there was an error in the bluetooth discovery.
It will loop an error message until the users press fire.
*/
Graphics g = getGraphics();
drawErrorMessage(g);
int keyStates = getKeyStates();
if ((keyStates & FIRE_PRESSED) !=0)
{
return false;
}
return true;
}
private void drawErrorMessage(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
//border
g.setColor(0xFC932C);
g.fillRect(0, 0, width, height);
g.setColor(0xffffff);
g.fillRect(1, 1, width - 2, height - 2);
// top
g.setColor(0xEBF1F7);
g.fillRect(1, 1, width - 2, 27);
g.setColor(0xADC2D6);
g.fillRect(1, 28, width - 2, 2);
g.drawImage (logo, width - 138, 1, Graphics.TOP | Graphics.LEFT);
// Text
g.setColor(0xFF0000);
g.setFont(boldFont);
g.drawString("There was an error:",5,40,Graphics.TOP|Graphics.LEFT);
g.setColor(0x627293);
g.setFont(regularFont);
g.drawString(bluetooth.bterror(),5,55,Graphics.TOP|Graphics.LEFT);
g.setColor(0x627293);
g.setFont(regularFont);
g.drawString("Press fire to continue.",5,65,Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
private void drawScreen(Graphics g) {
/**********************************
* Draws the splashScreen.
**********************************/
//border
g.setColor(0xFC932C);
g.fillRect(0, 0, width, height);
g.setColor(0xffffff);
g.fillRect(1, 1, width - 2, height - 2);
// top
g.setColor(0xEBF1F7);
g.fillRect(1, 1, width - 2, 27);
g.setColor(0xADC2D6);
g.fillRect(1, 28, width - 2, 2);
//middle-box
g.setColor(0x627293);
g.fillRect(width / 2 - 44, height / 2 - 5, 86, 11);
g.setColor(0xffffff);
g.fillRect(width / 2 - 43, height / 2 - 4, 84, 9);
g.drawImage (logo, width - 138, 1, Graphics.TOP | Graphics.LEFT);
/* The moving bar, a set of wierd IF statements to make sure the
moving bar doesn't cross over any borders and runs fine */
g.setColor( 0xB0B8C9 );
if( (currentX + 26) > 84 ) // If currentX + 26 is above 84 the bar will run over the right border, this means we must reduce it's size
{
int width2 = (currentX +26) - 84;
width2 = 26 - width2;
g.fillRect(width / 2 - 44 + currentX,height / 2 - 3,width2,7);
}else if( currentX < 1 ) { // If currentX is below 0 we must reduce our barsize otherwise it will be over the left border
int width3 = 26 + currentX;
if( (width / 2 - 44 + currentX + width3) < (width / 2 - 44) )
{
// If statement is true it means that it isn't even vissible, do nothing
}else{
// The bar is visible, but runs over the left border.
// Therefor we calculate how much over the border it is,
// and adds it to X and removes it from the width.
int leftOver = (width / 2 - 43) - (width / 2 - 44 + currentX);
g.fillRect(width / 2 - 43 + currentX + leftOver,height / 2 - 3,width3 - leftOver,7);
}
}else{
// The bar is running freely crossing no borders
g.fillRect(width / 2 - 44 + currentX,height / 2 - 3,26,7);
}
//box1
g.setColor(0x627293);
g.fillRect(width / 2 - 41, height / 2 - 17, 8, 8);
g.setColor(0xffffff);
g.fillRect(width / 2 - 40, height / 2 - 16, 6, 6);
int p1 = bluetooth.p1();
if( p1 == 1 )
{
g.drawImage (init, width / 2 - 47, height / 2 + 6, Graphics.TOP | Graphics.LEFT);
g.setColor(0x627293);
// Draw the lines around the active box
g.drawLine(width / 2 - 43, height / 2 - 18, width / 2 - 43, height / 2 - 16);
g.drawLine(width / 2 - 43, height / 2 - 19, width / 2 - 40, height / 2 - 19);
g.drawLine(width / 2 - 43, height / 2 - 11, width / 2 - 43, height / 2 - 9);
g.drawLine(width / 2 - 43, height / 2 - 8 , width / 2 - 40, height / 2 - 8);
g.drawLine(width / 2 - 43 + 11, height / 2 - 19, width / 2 - 43 + 11, height / 2 - 16);
g.drawLine(width / 2 - 43 + 8, height / 2 - 19, width / 2 - 43 + 10, height / 2 - 19);
g.drawLine(width / 2 - 43 + 11, height / 2 - 11, width / 2 - 43 + 11, height / 2 - 9);
g.drawLine(width / 2 - 43 + 8, height / 2 - 8 , width / 2 - 43 + 11, height / 2 - 8);
}else if ( p1 == 2 )
{
g.setColor(0x627293);
g.drawLine(width / 2 - 39, height / 2 - 14, width / 2 - 38, height / 2 - 12);
g.drawLine(width / 2 - 38, height / 2 - 12, width / 2 - 36, height / 2 - 15);
}
//box2
g.setColor(0x627293);
g.fillRect(width / 2 - 41 + 24, height / 2 - 17, 8, 8);
g.setColor(0xffffff);
g.fillRect(width / 2 - 40 + 24, height / 2 - 16, 6, 6);
int p2 = bluetooth.p2();
if( p2 == 1 )
{
g.drawImage (search, width / 2 - 44, height / 2 + 6, Graphics.TOP | Graphics.LEFT);
g.setColor(0x627293);
// Draw the lines around the active box
g.drawLine(width / 2 - 43 + 24, height / 2 - 18, width / 2 - 43 + 24, height / 2 - 16);
g.drawLine(width / 2 - 43 + 24, height / 2 - 19, width / 2 - 40 + 24, height / 2 - 19);
g.drawLine(width / 2 - 43 + 24, height / 2 - 11, width / 2 - 43 + 24, height / 2 - 9);
g.drawLine(width / 2 - 43 + 24, height / 2 - 8 , width / 2 - 40 + 24, height / 2 - 8);
g.drawLine(width / 2 - 43 + 11 + 24, height / 2 - 19, width / 2 - 43 + 11 + 24, height / 2 - 16);
g.drawLine(width / 2 - 43 + 8 + 24, height / 2 - 19, width / 2 - 43 + 10 + 24, height / 2 - 19);
g.drawLine(width / 2 - 43 + 11 + 24, height / 2 - 11, width / 2 - 43 + 11 + 24, height / 2 - 9);
g.drawLine(width / 2 - 43 + 8 + 24, height / 2 - 8 , width / 2 - 43 + 11 + 24, height / 2 - 8);
}else if ( p2 == 2 )
{
g.setColor(0x627293);
g.drawLine(width / 2 - 39 + 24, height / 2 - 14, width / 2 - 38 + 24, height / 2 - 12);
g.drawLine(width / 2 - 38 + 24, height / 2 - 12, width / 2 - 36 + 24, height / 2 - 15);
}
//box3
g.setColor(0x627293);
g.fillRect(width / 2 - 41 + 24 + 24, height / 2 - 17, 8, 8);
g.setColor(0xffffff);
g.fillRect(width / 2 - 40 + 24 + 24, height / 2 - 16, 6, 6);
int p3 = bluetooth.p3();
if( p3 == 1 )
{
g.drawImage (reg, width / 2 - 44, height / 2 + 6, Graphics.TOP | Graphics.LEFT);
g.setColor(0x627293);
// Draw the lines around the active box
g.drawLine(width / 2 - 43 + 24 + 24, height / 2 - 18, width / 2 - 43 + 24 + 24, height / 2 - 16);
g.drawLine(width / 2 - 43 + 24 + 24, height / 2 - 19, width / 2 - 40 + 24 + 24, height / 2 - 19);
g.drawLine(width / 2 - 43 + 24 + 24, height / 2 - 11, width / 2 - 43 + 24 + 24, height / 2 - 9);
g.drawLine(width / 2 - 43 + 24 + 24, height / 2 - 8 , width / 2 - 40 + 24 + 24, height / 2 - 8);
g.drawLine(width / 2 - 43 + 11 + 24 + 24, height / 2 - 19, width / 2 - 43 + 11 + 24 + 24, height / 2 - 16);
g.drawLine(width / 2 - 43 + 8 + 24 + 24, height / 2 - 19, width / 2 - 43 + 10 + 24 + 24, height / 2 - 19);
g.drawLine(width / 2 - 43 + 11 + 24 + 24, height / 2 - 11, width / 2 - 43 + 11 + 24 + 24, height / 2 - 9);
g.drawLine(width / 2 - 43 + 8 + 24 + 24, height / 2 - 8 , width / 2 - 43 + 11 + 24 + 24, height / 2 - 8);
}else if ( p2 == 2 )
{
g.setColor(0x627293);
g.drawLine(width / 2 - 39 + 24 + 24, height / 2 - 14, width / 2 - 38 + 24 + 24, height / 2 - 12);
g.drawLine(width / 2 - 38 + 24 + 24, height / 2 - 12, width / 2 - 36 + 24 + 24, height / 2 - 15);
}
//box4
g.setColor(0x627293);
g.fillRect(width / 2 - 41 + 24 + 24 + 24, height / 2 - 17, 8, 8);
g.setColor(0xffffff);
g.fillRect(width / 2 - 40 + 24 + 24 + 24, height / 2 - 16, 6, 6);
int p4 = bluetooth.p4();
if( p4 == 1 )
{
g.drawImage (conn, width / 2 - 44, height / 2 + 6, Graphics.TOP | Graphics.LEFT);
g.setColor(0x627293);
// Draw the lines around the active box
g.drawLine(width / 2 - 43 + 24 + 24 + 24, height / 2 - 18, width / 2 - 43 + 24 + 24 + 24, height / 2 - 16);
g.drawLine(width / 2 - 43 + 24 + 24 + 24, height / 2 - 19, width / 2 - 40 + 24 + 24 + 24, height / 2 - 19);
g.drawLine(width / 2 - 43 + 24 + 24 + 24, height / 2 - 11, width / 2 - 43 + 24 + 24 + 24, height / 2 - 9);
g.drawLine(width / 2 - 43 + 24 + 24 + 24, height / 2 - 8 , width / 2 - 40 + 24 + 24 + 24, height / 2 - 8);
g.drawLine(width / 2 - 43 + 11 + 24 + 24 + 24, height / 2 - 19, width / 2 - 43 + 11 + 24 + 24 + 24, height / 2 - 16);
g.drawLine(width / 2 - 43 + 8 + 24 + 24 + 24, height / 2 - 19, width / 2 - 43 + 10 + 24 + 24 + 24, height / 2 - 19);
g.drawLine(width / 2 - 43 + 11 + 24 + 24 + 24, height / 2 - 11, width / 2 - 43 + 11 + 24 + 24 + 24, height / 2 - 9);
g.drawLine(width / 2 - 43 + 8 + 24 + 24 + 24, height / 2 - 8 , width / 2 - 43 + 11 + 24 + 24 + 24, height / 2 - 8);
}else if ( p4 == 2 )
{
g.setColor(0x627293);
g.drawLine(width / 2 - 39 + 24 + 24, height / 2 - 14, width / 2 - 38 + 24 + 24, height / 2 - 12);
g.drawLine(width / 2 - 38 + 24 + 24, height / 2 - 12, width / 2 - 36 + 24 + 24, height / 2 - 15);
}
flushGraphics();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -