📄 greetingcardscreen.java
字号:
import java.util.Vector;
import java.io.DataInputStream;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import com.nokia.mid.ui.FullCanvas;
public class GreetingCardScreen extends FullCanvas implements Runnable {
private GreetingCardPlayer cardPlayer;
private GreetingCardImage cardImage;
private String textOnScreen;
private byte state;
private int scrollCursor;
private String[] scrollTextLines;
private int bgc = 0x000040B4;
private int tfgc = 0x0000AAFF;
private int tbgc = 0;
private int thc = 0x00ffff00;
private Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
private int msgTextSeq = 0;
private String userMsg = "";
public GreetingCardScreen(GreetingCardPlayer cardPlayer, GreetingCardImage cardImage, DataInputStream dis)
{
this.cardPlayer = cardPlayer;
this.cardImage = cardImage;
state = 0;
}
public void initparam(String sMsg, String sBGC, String sTFGC, String sTBGC, String sTHC, String sFont)
{
if(sFont != null && sFont.length() > 0)
{
int iFont = Integer.parseInt(sFont);
int face = (iFont & (byte)0xC0) >> 6;
switch(face)
{
case 0:
face = Font.FACE_MONOSPACE;
break;
case 1:
face = Font.FACE_PROPORTIONAL;
break;
case 2:
face = Font.FACE_SYSTEM;
break;
}
int size = (iFont & (byte)0x30) >> 4;
switch(size)
{
case 0:
size = Font.SIZE_SMALL;
break;
case 1:
size = Font.SIZE_MEDIUM;
break;
case 2:
size = Font.SIZE_LARGE;
break;
}
int style = 0;
if((iFont & 0x01) != 0)
{
style |= Font.STYLE_PLAIN;
}
if((iFont & 0x02) != 0)
{
style |= Font.STYLE_BOLD;
}
if((iFont & 0x04) != 0)
{
style |= Font.STYLE_ITALIC;
}
if((iFont & 0x08) != 0)
{
style |= Font.STYLE_UNDERLINED;
}
}
if(sBGC != null && sBGC.length() > 0)
{
bgc = Integer.parseInt(sBGC);
}
if(sTFGC != null && sTFGC.length() > 0)
{
tfgc = Integer.parseInt(sTFGC);
}
if(sTBGC != null && sTBGC.length() > 0)
{
tbgc = Integer.parseInt(sTBGC);
}
if(sTHC != null && sTHC.length() > 0)
{
thc = Integer.parseInt(sTHC);
}
//sMsg = "\u60A8\u5C06";
if(sMsg != null && sMsg.length() > 0)
{
userMsg = sMsg;
}
}
public boolean init()
{
state = 1; // Init Page
return true;
}
private void loadScrollText()
{
if(scrollTextLines != null)
{
state = 2; // Page 3 with user message
scrollCursor = getHeight() / 2;
Thread t = new Thread(this);
t.start();
}
else
{
state = 4; // Normal Page
}
repaint();
serviceRepaints();
}
private void nextPage()
{
if(msgTextSeq == 0)// Page 2
{
cardImage.secondPage();
textOnScreen = "";
msgTextSeq++;
repaint();
serviceRepaints();
}
else if(msgTextSeq == 1) // Page 3 with user message
{
msgTextSeq++;
cardImage.thirdPage();
textOnScreen = userMsg;
scrollTextLines = wrapText(font, getWidth(), userMsg);
loadScrollText();
}
else // Page 1
{
state = 4;
msgTextSeq = 0;
textOnScreen = "";
cardImage.redofirstPage();
repaint();
serviceRepaints();
}
}
public void start()
{
System.gc();
textOnScreen = "";
cardImage.firstPage();
state = 4;
repaint();
serviceRepaints();
}
private void quit()
{
cardPlayer.exit();
}
private String[] wrapText(Font f, int allowWidth, String sText)
{
allowWidth -= 10;
char[] ca = new char[sText.length()];
sText.getChars(0, sText.length(), ca, 0);
if(f.charsWidth(ca, 0, ca.length) <= allowWidth)
{
String[] s = new String[1];
s[0] = sText;
return s;
}
Vector v = new Vector();
int textWidth = 0;
int breakIdx = -1;
for(int i = 0; i < sText.length(); i++)
{
char c = sText.charAt(i);
textWidth += f.charWidth(c);
if(c == '\n')
{
String line = sText.substring(0, i);
if(sText.length() > line.length())
sText = sText.substring(line.length()+1);
else
sText = "";
textWidth = 0;
i = -1;
v.addElement(line);
}
else if(textWidth <= allowWidth)
{
if(c == ' ' || c > 127)
{
breakIdx = i;
}
}
else
{
String line = "";
if(breakIdx < 0) {
line = sText.substring(0, i);
} else {
line = sText.substring(0, breakIdx+1);
}
if(sText.length() > line.length()) {
sText = sText.substring(line.length());
} else {
sText = "";
}
textWidth = 0;
breakIdx = i = -1;
v.addElement(line);
}
}
if(sText.length() > 0)
{
v.addElement(sText);
}
if(v.size() == 0)
{
return null;
}
String[] lines = new String[v.size()];
for(int i = 0; i < v.size(); i++)
{
lines[i] = (String)v.elementAt(i);
}
v = null;
System.gc();
return lines;
}
private void drawString(Graphics g, String s, int fgc, int bgc, int x, int y, int anchor)
{
g.setColor(bgc);
g.drawString(s, x+1, y+1, anchor);
g.drawString(s, x-1, y-1, anchor);
g.drawString(s, x+1, y, anchor);
g.drawString(s, x, y+1, anchor);
g.drawString(s, x-1, y, anchor);
g.drawString(s, x, y-1, anchor);
g.drawString(s, x+1, y-1, anchor);
g.drawString(s, x-1, y+1, anchor);
g.setColor(fgc);
g.drawString(s, x, y, anchor);
}
private void paintPlaying(Graphics g)
{
g.setColor(0);
g.setClip(0, 0, getWidth(), getHeight());
g.fillRect(0, 0, getWidth(), getHeight());
cardImage.drawCardScreen(g, getWidth(), getHeight());
int h = font.getHeight();
int y2 = getHeight() - h - 2;
int y1 = y2 - h - 2;
g.setFont(font);
if(textOnScreen != null)
{
drawString(g, textOnScreen, tfgc, tbgc, 3, y1, Graphics.TOP|Graphics.LEFT);
}
}
private void paintLoading(Graphics g)
{
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
if(cardImage.currentScreen != null)
{
g.drawImage(cardImage.currentScreen, getWidth()/2, getHeight()/2, Graphics.HCENTER|Graphics.VCENTER);
}
}
private void paintScrollText(Graphics g)
{
g.setFont(font);
g.setColor(bgc);
g.fillRect(0, 0, getWidth(), getHeight());
if(cardImage.currentScreen != null)
{
g.drawImage(cardImage.getNextPage(), getWidth()/2, getHeight()/2, Graphics.HCENTER|Graphics.VCENTER);
}
int h = font.getHeight()+2;
int y = scrollCursor;
for(int i = 0; i < scrollTextLines.length; i++)
{
if(h*2+y >= 0)
{
drawString(g, scrollTextLines[i], thc, tbgc, getWidth()/2, y, Graphics.TOP|Graphics.HCENTER);
}
if(i == scrollTextLines.length - 1 && y < 10)
{
scrollCursor = getHeight() / 2;
break;
}
y += h;
if(y > getHeight())
{
break;
}
}
}
/**
* paint
*/
public void paint(Graphics g)
{
switch(state)
{
case 0:
paintLoading(g);
break;
case 2:
paintScrollText(g);
break;
case 4:
paintPlaying(g);
break;
}
}
/**
* Called when a key is pressed.
*/
protected void keyPressed(int keyCode)
{
int action = getGameAction(keyCode);
if(keyCode == KEY_POUND || keyCode == KEY_STAR)
{
quit();
}
if(state == 4)
{
nextPage();
}
else
{
nextPage();
repaint();
}
}
public void playScrollText()
{
while(state == 2 && scrollTextLines != null)
{
repaint();
scrollCursor -= 1;
try { Thread.sleep(30); } catch(Exception e) {}
}
}
public void run()
{
if(state == 2)
{
playScrollText();
}
}
public Image resizeImage(Image image)
{
int srcWidth = image.getWidth();
int srcHeight = image.getHeight();
int newWidth = 0;
int newHeight = 0;
newWidth = getWidth() - 15;
newHeight = getHeight() - 15;
if(newWidth < 128 || newWidth < 128)
{
newWidth = srcWidth;
newHeight = srcHeight;
}
Image newImage = Image.createImage(newWidth, newHeight);
Graphics g = newImage.getGraphics();
for (int y = 0; y < newHeight; y++)
{
for (int x = 0; x < newWidth; x++)
{
g.setClip(x, y, 1, 1);
int dx = x * srcWidth / newWidth;
int dy = y * srcHeight / newHeight;
g.drawImage(image, x - dx, y - dy,
Graphics.LEFT | Graphics.TOP);
}
}
Image immutableImage = Image.createImage(newImage);
return immutableImage;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -