J2ME 拼图游戏 快速开发 全过程 之代码祥解(6 )——欢迎界面、游戏结束后的等待界面与增加用户名字界面

欢迎界面,如图:


代码:

/*
 * Created on 2004-6-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.edu.xtu.tilepuzzle.ui;


import cn.edu.xtu.tilepuzzle.GameDB;
import java.io.IOException;


import javax.microedition.lcdui.*;


/**
 * @author P2800
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class WelcomeUI extends Canvas
{

    public int width;
    public int height;
    //private Image image;
    //private String message = DataBase.author;
    private String message = GameDB.author;
    private String school = GameDB.school;


    public WelcomeUI()
    {
    this.setFullScreenMode(true);
        width = this.getWidth();
        height = this.getHeight();
        //System.out.println(width+","+height);
        //image = createImage("/welcome.png");
        
    }
    
    public Image createImage(String name)
    {
        Image image = null;
        try
        {
            image = Image.createImage(name);
        }
        catch(IOException e)
        {
           
        }
        return image;
    }


    public void setMessage(String message)
    {
        this.message = message;
        this.repaint();
    }


    public void setSchool(String school)
    {
        this.school = school;
        this.repaint();
    }


    /*
     * (non-Javadoc)
     * 
     * @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
     */
    protected void paint(Graphics g)
    {
    //System.out.println("WelcomeUI 开始绘图 ......");
        // TODO Auto-generated method stub
        //arg0.setColor(128, 128, 255);
        //arg0.drawRect(5, 5, width - 10, height - 10);
        /*
         if(image!=null)
        {
            arg0.drawImage(image,(width-image.getWidth())/2,
                    (height-image.getHeight())/2,Graphics.TOP|Graphics.LEFT);
        }
        */
    g.setColor(255, 255, 190);
g.fillRect(0, 0, width, height);

    g.setColor(255,0,0);
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE  ));
        if (message != null)
        {
            g.drawString(message, width / 2, height / 3, Graphics.BOTTOM | Graphics.HCENTER);
        }
        if (school != null)
        {
            g.drawString(school, width / 2, height * 2 / 3, Graphics.BOTTOM | Graphics.HCENTER);
        }
        
    }


}


游戏结束后的等待界面,如图:


代码:

package cn.edu.xtu.tilepuzzle.ui;




import java.util.*;
import javax.microedition.lcdui.*;


import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;


public class WaitCanvasUI extends Canvas implements CommandListener{


MainUIController mainUIController;
    Display display;
    BoardModel boardModel;
    Displayable displayable;
    
private int mCount, mMaximum;
private int mInterval;


private int mWidth, mHeight, mX, mY, mRadius;
private String mMessage;
Command ok;
Command cancel;


public WaitCanvasUI(MainUIController mainUIController,BoardModel boardModel,Display display,Displayable db) {
this.boardModel=boardModel;
this.display=display;
this.displayable=db;
this.mainUIController=mainUIController;
mCount = 0;
mMaximum = 36;
mInterval = 100;


mWidth = getWidth();
mHeight = getHeight();


// Calculate the radius.
int halfWidth = (mWidth - mRadius) / 2;
int halfHeight = (mHeight - mRadius) / 2;
mRadius = Math.min(halfWidth, halfHeight);


// Calculate the location.
mX = halfWidth - mRadius / 2;
mY = halfHeight - mRadius / 2;

ok=new Command("添加",Command.OK,1);
cancel=new Command("返回",Command.CANCEL,0);
this.addCommand(ok);
this.addCommand(cancel);
setCommandListener(this);
}

public void setFlag(boolean run){
// Create a Timer to update the display.
if (run) {
TimerTask task = new TimerTask() {
public void run() {
mCount = (mCount + 1) % mMaximum;
repaint();
}
};
Timer timer = new Timer();


timer.schedule(task, 0, mInterval);
}
}


public void paint(Graphics g) {
int theta = -(mCount * 360 / mMaximum);


// Clear the whole screen.
g.setColor(255, 255, 190);
g.fillRect(0, 0, mWidth, mHeight);       


// Now draw the pinwheel.
g.setColor(128, 128, 255);
g.drawArc(mX, mY, mRadius, mRadius, 0, 360);
g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
g.fillArc(mX, mY, mRadius, mRadius, theta + 270, 90);


// Draw the message, if there is a message.
if (mMessage != null) {
g.drawString(mMessage, mWidth / 2, mHeight, Graphics.BOTTOM
| Graphics.HCENTER);
}

g.setColor(255,0,0);

        g.setFont(GameDB.MONOSPACE_BOLD_LARGE_Font);
        //绘制一个手机全屏幕填充颜色的矩形
        //g.fillRect(0,0,getWidth(),getHeight());
        //System.out.println("胜利时屏幕宽度= "+getWidth());
        //System.out.println("恭喜你,过关了!,字符串宽度="+tempFont.stringWidth("恭喜你,过关了!"));
       
        g.drawString((boardModel.cheated ? "CHEATER!" : "恭喜你,过关了!"), (getWidth()-GameDB.MONOSPACE_BOLD_LARGE_Font.stringWidth("恭喜你,过关了!"))/2 , getHeight()*1/12 , Graphics.LEFT | Graphics.TOP);
      
        g.setFont(GameDB.MONOSPACE_BOLD_MEDIUM_Font);            
        //System.out.println("共用时:"+boardModel.sumtTime);
        String timeString="共用时:"+BoardModel.getTimeStringByS(boardModel.sumTime);
        g.drawString(timeString,(getWidth()- GameDB.MONOSPACE_BOLD_MEDIUM_Font.stringWidth(timeString))/2, getHeight()*2/12,Graphics.LEFT | Graphics.TOP);
        
        g.setFont(GameDB.MONOSPACE_PLAIN_MEDIUM_Font); 
        String messageString="最否将你的名字添加到游戏排名?";
        g.drawString(messageString,(getWidth()- GameDB.MONOSPACE_PLAIN_MEDIUM_Font.stringWidth(messageString))/2, getHeight()*10/12,Graphics.LEFT | Graphics.TOP);
}


public void commandAction(Command c, Displayable arg1) {
if(c==ok){
//System.out.println("OK");
AddUserNameUI addUserNameUI=new AddUserNameUI(mainUIController, boardModel, display, displayable);
display.setCurrent(addUserNameUI);
            //mainUIController.handleCOMMAND_ID(menuIdx);
        }else if(c==cancel){
        //System.out.println("CANCEL");
        this.display.setCurrent(mainUIController.mainMenuUI);
            //mainUIController.handleCOMMAND_ID(6);
        }

}

}

增加用户名字界面,如图:


代码:

package cn.edu.xtu.tilepuzzle.ui;


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;


import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
import cn.edu.xtu.tilepuzzle.model.RecordStoreOperations;


public class AddUserNameUI extends Canvas implements CommandListener{
    MainUIController mainUIController;
    Display display;
    BoardModel boardModel;
    Displayable db;
    Image image;
    TextBox nameTextBox;
    
    private Command ok;
    private Command cancel;
    
    public AddUserNameUI(MainUIController mainUIController,BoardModel boardModel,Display display,Displayable db) {
        //this.setFullScreenMode(true);
        this.boardModel=boardModel;
        this.display=display;
        this.db=db;
        this.mainUIController=mainUIController;
        init();
    }
    
    public void init(){
        
        nameTextBox=new TextBox("请输入你的名字",null,255,TextField.ANY);
        
        ok = new Command("确定", Command.OK, 1);
        cancel = new Command("退出", Command.CANCEL, 0);
        
        nameTextBox.addCommand(ok);
        nameTextBox.addCommand(cancel);
        nameTextBox.setCommandListener(this);
        
    }
    
    protected void paint(Graphics g) {
        this.display.setCurrent(nameTextBox);
    }
    
    public void commandAction(Command c, Displayable db) {
        if(c==ok){
            try {
                String strString=nameTextBox.getString()+";"+(boardModel.columns+"x"+boardModel.rows).toString()+";"+boardModel.sumTime/1000;
                RecordStoreOperations.addStrToRecordStroe(strString ,GameDB.recordStorePeopleResult);
                mainUIController.showPeopleInfoListUI.init();
                mainUIController.showPeopleInfoListUI.repaint();
                this.display.setCurrent(mainUIController.mainMenuUI);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        }else if(c==cancel){
            this.display.setCurrent(mainUIController.mainMenuUI);
            //mainUIController.handleCOMMAND_ID(6);
            //System.out.println("Cancel");
        }
    }
    
}

posted @ 2012-04-14 14:21  love25444  阅读(7)  评论(0)    收藏  举报