⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wzqdisplayable.java~9~

📁 j2me源代码
💻 JAVA~9~
字号:
package wzq;

import javax.microedition.lcdui.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class WzqDisplayable extends Canvas implements CommandListener {

    int nWhiteWidth; //width of the white space
    int nCanvasW, nCanvasH;  //the width and height of the canvas
    int nChessWidth;  //the width of chesses
    int nChessMapLength, nMapGrid, nGridWidth;  //the length of chessmap,number of mapgrid and width of grid
    int nMapX, nMapY;   //the x,y value of the map origin position
    int nSelectX, nSelectY; //the x,y value of the position box
    boolean bIsPlayer1;  //is player1 or not
    Chesses chess[][];
    boolean bNewGame; //start a new game or not

    Command cmdExit, cmdRestart;


    public WzqDisplayable() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }

        bNewGame = true;
        nWhiteWidth=10;
        nCanvasW=getWidth()-nWhiteWidth;
        nCanvasH=getHeight()-nWhiteWidth;
        nMapGrid=15;
        chess=new Chesses[nMapGrid+1][nMapGrid+1];
        if(nCanvasW>nCanvasH){
            nChessMapLength=nCanvasH-nCanvasH%nMapGrid;
            nMapX=(nCanvasW-nChessMapLength)/2+nWhiteWidth/2;
            nMapY=(nCanvasH%nMapGrid)/2+nWhiteWidth/2;
        }
        else{
            nChessMapLength=nCanvasW-nCanvasW%nMapGrid;
            nMapX=(nCanvasW%nMapGrid)/2+nWhiteWidth/2;
            nMapY=(nCanvasH-nChessMapLength)/2+nWhiteWidth/2;
        }
        nGridWidth=nChessMapLength/nMapGrid;
        nChessWidth=nGridWidth-1;
        nSelectX=nSelectY=nMapGrid/2;
        bIsPlayer1=true;
    }
    protected void paintMap(Graphics g){
        g.setColor(128,128,128);
        for(int i=0;i<nMapGrid;i++)
            for(int j=0;j<nMapGrid;j++){
                g.drawRect(nMapX+j*nGridWidth,
                           nMapY+i*nGridWidth,
                           nGridWidth,
                           nGridWidth);
            }
    }

    protected void paintSelectBox(Graphics g){
        g.setColor(0,0,255);
        g.drawRect(nMapX+nSelectX*nGridWidth-nGridWidth/2,
                nMapY+nSelectY*nGridWidth-nGridWidth/2,
             nGridWidth,
          nGridWidth);
    }

    protected void paintChesses(Graphics g){
        for(int i=0;i<=nMapGrid;i++){
           for(int j=0;j<=nMapGrid;j++){
               if(chess[i][j]!=null){
                   if(chess[i][j].bIsPlayer1)
                       g.setColor(255,255,255);
                   else
                       g.setColor(255,0,0);
                   g.fillArc(nMapX+j*nGridWidth-nChessWidth/2,
                           nMapY+i*nGridWidth-nChessWidth/2,
                           nChessWidth,nChessWidth,0,360);
               }
           }
       }
    }

    protected void paintPlayer(Graphics g,boolean bIsPlayer1){
        if(bIsPlayer1)
            g.setColor(255,255,255);
        else
            g.setColor(255,0,0);

        g.drawRect(1,1,getWidth()-2,getHeight()-2);
        g.drawRect(2,2,getWidth()-4,getHeight()-4);
        g.drawRect(3,3,getWidth()-6,getHeight()-6);
    }


    private void jbInit() throws Exception {
        // Set up this Displayable to listen to command events
        cmdExit = new Command("退出游戏",Command.EXIT,0);
        cmdRestart = new Command("重新开始游戏", Command.SCREEN, 0);
        addCommand(cmdRestart);
        addCommand(cmdExit);
        setCommandListener(this);

        // add the Exit command
        //addCommand(new Command("Exit", Command.EXIT, 1));
    }

    public void commandAction(Command command, Displayable displayable) {
        /** @todo Add command handling code */
        if (command.getCommandType() == Command.EXIT) {
            // stop the MIDlet
            WzqMIDlet.quitApp();
        }
    }

    protected void paint(Graphics g) {
        /** @todo Add paint codes */
        g.setColor(64,64,64);
        g.fillRect(0, 0, getWidth(), getHeight());
        paintPlayer(g,bIsPlayer1);
        paintMap(g);
        paintSelectBox(g);
        paintChesses(g);

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -