【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-Popup.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package popup;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class Popup implements Runnable
{
public static final char[][] ALT_OK;
public static final char[][] ALT_CANCEL;
public static final char[][] ALT_YES_NO;
public static final char[][] ALT_OK_CANCEL;
public static final char[][] ALT_YES_NO_CANCEL;
static
{
ALT_OK = new char[1][];
ALT_OK[0] = "Ok".toCharArray();
ALT_CANCEL = new char[1][];
ALT_CANCEL[0] = "Cancel".toCharArray();
ALT_YES_NO = new char[2][];
ALT_YES_NO[0] = "YES".toCharArray();
ALT_YES_NO[1] = "NO".toCharArray();
ALT_OK_CANCEL = new char[2][];
ALT_OK_CANCEL[0] = ALT_OK[0];
ALT_OK_CANCEL[1] = ALT_CANCEL[0];
ALT_YES_NO_CANCEL = new char[3][];
ALT_YES_NO_CANCEL[0] = ALT_YES_NO[0];
ALT_YES_NO_CANCEL[1] = ALT_YES_NO[1];
ALT_YES_NO_CANCEL[2] = ALT_CANCEL[0];
}
protected char[] text;
protected byte alternatives;
protected char[][] altTexts;
protected byte timeOut;
protected byte timeOutAlt;
protected byte curAlt;
protected PopupListener listener;
protected volatile boolean active = false;
protected int w;
protected int h;
protected int[][] breakTextData;
protected int visibleLines;
protected int curLine;
protected int maxLine;
protected int yoffset;
protected long endTime;
protected Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_LARGE);
protected int fontHeight = font.getHeight();
protected int borderColor = 0xdd0000;
protected int backgroundColor = 0xcc440000;
protected int textColor = 0xffffff;
protected int alternativeColor = 0xff2200;
protected int selectedAlternativeColor = 0xffffff;
protected static int[] rgbData;
protected static final int OFFSET_POPUP = 8;
protected static final int OFFSET_TEXT = 2;
protected static final int OFFSET_ALT = 4;
protected static final int SB_WIDTH = 5;
protected static final char[] TEXTBREAKS =
{ ' ', '?', ';', ',', '.', '!',
':', '-', '=', '(', ')', '[', ']' };
protected static final char NEWLINE = '\n';
public Popup() {}
public void init(char[] text, char[][] altTexts, byte timeOut,
byte defaultAlt, byte timeOutAlt, PopupListener listener, int width,
int height)
{
this.text = text;
this.altTexts = altTexts;
if (altTexts != null)
{
alternatives = (byte) altTexts.length;
}
else
{
alternatives = 0;
}
this.timeOut = timeOut;
this.timeOutAlt = timeOutAlt;
this.listener = listener;
curAlt = defaultAlt;
w = width - (OFFSET_POPUP << 1);
h = height - (OFFSET_POPUP << 1);
active = true;
if (timeOut > 0)
{
endTime = System.currentTimeMillis() + (timeOut * 1000);
}
else if (alternatives > 0)
{
endTime = 0;
}
else
{
endTime = System.currentTimeMillis();
}
visibleLines =
Math.max(1, ((h - (OFFSET_TEXT << 1)) / fontHeight) - 1);
int w = this.w - (OFFSET_TEXT << 1) - (SB_WIDTH << 1);
curLine = 0;
breakTextData = breakString(text, w);
yoffset = 0;
maxLine = breakTextData.length - visibleLines + 1;
if (breakTextData.length < visibleLines)
{
int newH = breakTextData.length * fontHeight;
if (alternatives > 0)
newH += fontHeight;
newH += OFFSET_TEXT + OFFSET_ALT;
yoffset = (h - newH) >> 1;
h = newH;
}
if (rgbData == null || rgbData.length != this.w * 8)
{
rgbData = new int[this.w * 8];
for (int i = 0; i < rgbData.length; i++)
{
rgbData[i] = backgroundColor;
}
}
new Thread(this, "PopupPoll").start();
}
protected int[][] breakString(char[] text, int width)
{
int offset = 0;
int lines = 0;
int newOffset;
while (offset < text.length)
{
newOffset =
findNextBreak(text, offset, text.length - offset, width, font);
offset = newOffset;
lines++;
}
int[][] indices = new int[lines][2];
lines = 0;
offset = 0;
while (offset < text.length)
{
newOffset =
findNextBreak(text, offset, text.length - offset, width, font);
indices[lines][0] = offset;
indices[lines][1] = newOffset - offset;
lines++;
offset = newOffset;
}
return indices;
}
public int findNextBreak(char[] text, int offset, int len, int w, Font f)
{
int breakOffset = offset;
int textW = 0;
int niceB = -1;
char c;
charLoop: while (breakOffset <= offset + len && textW < w)
{
if (breakOffset == offset + len)
c = TEXTBREAKS[0];
else
c = text[breakOffset];
if (c == NEWLINE)
{
niceB = breakOffset;
break charLoop;
}
breakCharLoop:
for (int i = TEXTBREAKS.length - 1; i >= 0; i--)
{
if (c == TEXTBREAKS[i])
{
niceB = breakOffset;
break breakCharLoop;
}
}
if (breakOffset == offset + len - 1)
{
niceB