原创作者: wuhua
阅读:1298次
评论:0条
更新时间:2011-05-26
高级UI提供了一个Alert的控件可以有弹出对话框的效果。但是大家想不想自己实现一个呢。想不想知道sun是如何让Alert工作的呢?好请看下文
设计思想是。建立一个 abstract class Dialog extends Canvas。下面的事情就让我们一步步花出来吧。
实现理念是先绘制整个Canvas然后通过图形Graphics描绘成透明,接着在整个屏幕的中间去出一块区域来,至于上面你要做什么那就是你的事情了。哈。
好看代码,
下面这段是弹出一个对话框实现
设计思想是。建立一个 abstract class Dialog extends Canvas。下面的事情就让我们一步步花出来吧。
实现理念是先绘制整个Canvas然后通过图形Graphics描绘成透明,接着在整个屏幕的中间去出一块区域来,至于上面你要做什么那就是你的事情了。哈。
好看代码,
java 代码
- /********************************************************************
- *
- * 版权说明,此程序仅供学习参考。不能用于商业
- *
- ********************************************************************/
- package org.pook.ui.form;
- import java.util.TimerTask;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import org.pook.log.Log;
- import org.pook.ui.Command;
- import org.pook.ui.SoftButton;
- import org.pook.ui.core.Platform;
- import org.pook.ui.event.CommandListener;
- import org.pook.ui.timer.TimerTaskManager;
- /**
- * 类名:Dialog.java
编写日期: 2006-9-16
- * 程序功能描述:弹出窗�?,因为足球项目要求很多的弹出窗口的形式不同�?�? 定义�?个抽象的弹出窗口,把基本的功能再这个实�?,以下的�?�么绘制则交给具体的要求
- *
Demo:
Bug:
- *
- * 程序变更日期 �?
变更作�?? �?
变更说明 �?
- *
- * @author wuhua
- */
- public abstract class Dialog extends Canvas implements Runnable {
- private static Log log = Log.getLog("Dialog");
- protected final int X = 0;
- protected final int Y = 1;
- protected final int WIDTH = 2;
- protected final int HEIGHT = 3;
- /** 显示主要部分.比如菜单的Icon,List的数据的位置 */
- protected int[] view = new int[4];
- /** Preset alternative containing OK */
- public static final int DIALOG_OK = 0;
- /** Preset alternative containing CANCEL = 1 */
- public static final int DIALOG_CANCEL = 2;
- /** Preset alternatives containing YES and NO */
- public static final int DIALOG_YES_NO = 3;
- /** Preset alternatives containing OK and CANCEL */
- public static final int DIALOG_OK_CANCEL = 4;
- /** Preset alternatives containing YES, NO, and CANCEL */
- public static final int DIALOG_YES_NO_CANCEL = 5;
- public static final int DIALOG_TEXT = 6;
- /**
- * 保存当前窗口,主要用�?�是,当前对话筐消失的时�??,要求重绘�?. 永远刷新父类
- */
- protected Panel parent;
- protected boolean hasFocus;
- /**
- * 定义超时参数
- */
- long timeOut;
- /**
- * 对话框类�?
- */
- int type;
- /** 对话框标�? */
- protected String title;
- /**
- * 把单行标题转换为多行以方便显�?
- */
- protected String[] rowTitle;
- protected int bgColor;
- protected int fontColor;
- protected Font font = Font.getDefaultFont();
- /** 用于执行消失窗口 */
- protected TimerTask task;
- protected Display display;
- protected SoftButton softButton;
- /**
- * 构�?�一个默认的标题,父类的窗�?
- *
- * @param title
- * @param parent
- */
- public Dialog(String title, Panel parent, Display display) {
- this(title, parent, display, 3000);
- }
- /**
- * 构�?�一个指定时间的构�?�窗�?
- *
- * @param title
- * @param parent
- * @param timeOut
- */
- public Dialog(String title, Panel parent, Display display, long timeOut) {
- this(title, parent, display, Dialog.DIALOG_OK, timeOut);
- }
- /**
- * 构�?�一个指定窗口类�?,时间的窗�?
- *
- * @param title
- * @param parent
- * @param type
- * @param timeOut
- */
- public Dialog(String title, Panel parent, Display display, int type,
- long timeOut) {
- setFullScreenMode(true);
- this.parent = parent;
- checkParent();
- this.timeOut = timeOut;
- this.type = type;
- this.title = title;
- this.display = display;
- softButton = new SoftButton();
- if (timeOut != 0)
- task = TimerTaskManager.getInstace().add(this, timeOut);
- //设置默认的风�?
- setStyle(0x0033FF, 0xFFFFFF);
- }
- public void setStyle(int bgColor, int fontColor) {
- this.bgColor = bgColor;
- this.fontColor = fontColor;
- }
- public void cancel() {
- //log.debug("Stop...");
- if (parent == null)
- throw new NullPointerException("Parent is not Null.");
- task.cancel();
- if(parent == null)
- return;
- display.setCurrent(parent);
- }
- public void addCommand(Command cmd) {
- this.softButton.addCommand(cmd);
- }
- public void setSoftButtonListener(CommandListener cml) {
- this.softButton.setCommandListener(cml);
- }
- public void setSoftButtonStyle(int bgColor, int fontColor){
- this.softButton.setStyle(bgColor, fontColor);
- }
- public void run() {
- cancel();
- }
- /** 绘制透明�? * */
- public void drawRGB(Graphics g) {
- int ai[] = new int[Platform.WIDTH];
- for (int j1 = 0; j1 < ai.length; j1++)
- ai[j1] = 0x90000000;
- g.drawRGB(ai, 0, 0, 0, 0, Platform.WIDTH, Platform.HEIGHT, true); // 绘制透明景色
- }
- private void checkParent() {
- if (parent == null){
- throw new NullPointerException("Parent is not null");
- }
- }
- protected void keyPressed(int keyCode) {
- softButton.onClick(keyCode);
- }
- public void setPanel(Panel panel) {
- this.parent = panel;
- }
- public void setTimeOut(long timeOut) {
- this.timeOut = timeOut;
- }
- public int getType() {
- return type;
- }
- public void setType(int type) {
- this.type = type;
- }
- }
java 代码
- /********************************************************************
- *
- * 版权说明,此程序仅供学习参考。不能用于商业
- *
- ********************************************************************/
- package org.pook.ui.form;
- import java.util.Vector;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import org.pook.Pook;
- import org.pook.log.Log;
- import org.pook.ui.Command;
- import org.pook.ui.SelectedPart;
- import org.pook.ui.core.Platform;
- import org.pook.ui.event.CommandListener;
- import org.pook.ui.util.FontUtil;
- import org.pook.ui.util.GraphicsUtil;
- import org.pook.util.StringUtil;
- /**
- * 类名:MessageDialog.java
- * 编写日期: 2006-9-26
- * 程序功能描述:
- * Demo:
- * Bug:
- *
- * 程序变更日期 :
- * 变更作者 :
- * 变更说明 :
- *
- * @author wuhua
- */
- public class MessageDialog extends TextDialog implements CommandListener{
- private static Log log = Log.getLog("MessageDialog");
- private Command ok;
- private SelectedPart plan;
- private int planHeight;
- private int contentHeight;
- private int viewSize;
- private int space;
- private Vector content;
- private int selectIndex;
- private int contentY;
- public MessageDialog(String _title,String _message, long _timeOut) {
- super(_title, Pook.MAINMENU, Pook.getDisplay(), _timeOut);
- ok = new Command("确定",Command.LEFT, Command.FIRST_PRIORITY);
- this.addCommand(ok);
- this.setSoftButtonListener(this);
- this.setMessage(_message);
- initView();
- this.setStyle(0x001D68, 0xFFFFFF);
- this.setSoftButtonStyle(0x0071BD,0xFFFFFF);
- this.setType(Dialog.DIALOG_OK);
- }
- public void setMessage(String message){
- skipMessage(message);
- }
- private void skipMessage(String message) {
- if(message == null)
- return ;
- content = new Vector();
- String[] text = StringUtil.split(message, "\n");
- for(int j =0; j < text.length; j++){
- String[] t = FontUtil.splitOnlyStringToRowString(font,text[j],view[WIDTH]-6);
- for(int i=0; i
- content.addElement(t[i]);
- }
- ///content.addElement("");
- }
- //log.debug(content.size());
- //new Plan
- }
- protected void keyPressed(int keyCode) {
- super.keyPressed(keyCode);
- this.thisUpAndDown(keyCode);
- this.repaint();
- }
- /**
- * 上下事件
- *
- * @param keyCode
- */
- private void thisUpAndDown(int keyCode) {
- if (this.content == null)
- return;
- if(this.content.size() < this.viewSize)
- return;
- switch (keyCode) {
- case Platform.KEY_UP: {
- selectIndex = selectIndex <= 0 ? content.size() - viewSize - 1
- : --selectIndex;
- // log.debug("Key");
- break;
- }
- case Platform.KEY_DOWN: {
- selectIndex = selectIndex >=content.size() - viewSize - 1 ? 0
- : ++selectIndex;
- //log.debug("Key1");
- break;
- }
- }
- }
- public void commandAction(Command cmd) {
- if(cmd == ok)
- this.cancel();
- }
- protected void paint(Graphics g) {
- parent.paint(g);
- drawRGB(g);
- this.softButton.paint(g);
- this.paintMessageDialog(g);
- }
- private void paintMessageDialog(Graphics g) {
- initView();
- g.setFont(font);
- GraphicsUtil.drawRectWithColor(g,0xFFD84F, view[X]-1,view[Y]-1,view[WIDTH]+1,view[HEIGHT]+1);
- paintTitleImpl(g);
- paintContent(g);
- paintState(g);
- }
- private void paintTitleImpl(Graphics g) {
- g.setColor(0x0071BC);
- g.fillRect(view[X],view[Y],view[WIDTH],font.getHeight() + 2);
- g.setColor(0xFFFFFF);
- g.drawString(title, view[X] + 4, view[Y] + 1,Graphics.TOP|Graphics.LEFT);
- }
- private void paintContent(Graphics g) {
- if(content.size() > viewSize ){
- this.plan = new SelectedPart();
- plan.setStyle(0xFFFFFF, 0x000000);
- this.planHeight = contentHeight/(content.size() - viewSize);
- }
- contentY = view[Y] + font.getHeight() + 4;
- g.setColor(0x001D68);
- g.fillRect(view[X],contentY,view[WIDTH],contentHeight);
- paintContentImpl(g);
- }
- private void paintContentImpl(Graphics g) {
- if(content == null)
- return ;
- int size = viewSize>content.size()?content.size():viewSize + selectIndex;
- g.setColor(0xFFFFFF);
- for(int i = selectIndex; i
- g.drawString((String) content.elementAt(i),view[X] + 2, contentY + space * (i-selectIndex), Graphics.TOP|Graphics.LEFT);
- }
- /**
- * 绘制状态条
- */
- if(plan != null){
- //log.debug("SelectIndex = " + selectIndex);
- plan.changePosition(view[WIDTH] + 4 , contentY + this.planHeight *selectIndex, 4,this.planHeight);
- plan.paint(g);
- }
- }
- private void paintState(Graphics g) {
- int height = Platform.HEIGHT - 30 - font.getHeight() - 2;
- g.setColor(0x0071BC);
- g.fillRect(view[X], height ,view[WIDTH],font.getHeight() + 2);
- g.setColor(0xFFFFFF);
- g.drawLine(view[X],height,view[WIDTH] + 10, height);
- g.setColor(0xFFFFFF)
评论 共 0 条 请登录后发表评论