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

📄 fivecanvas.java

📁 j2me上的FIR游戏 从原代码光盘上复制
💻 JAVA
字号:

import java.io.IOException;
import javax.microedition.lcdui.*;

public class FiveCanvas extends Canvas
    implements CommandListener
{
    public static int BLACK = 0;
    public static int WHITE = 0xffffff;
    public static int RED = 0xff0000;
    public static int GREEN = 65280;
    public static int BLUE = 255;
    public static int WOOD = 0xffd020;
    private FiveMIDlet midFive;
    private Reg reg;
    private Command cmdStop;
    private Command cmdNew;
    private Command cmdUndo;
    private FiveLogic logic;
    private int borderSize;
    private boolean computerFirst;
    private int degree;
    private int cavWidth;
    private int cavHeight;
    private int cWidth;
    private int cHeight;
    private Font font;
    private int fWidth;
    private int fHeight;
    private int borderX;
    private int borderY;
    private int borderLength;
    private int gridLength;
    private int stoneLength;
    private int statusY;
    private String status;
    private int statusColor;
    private boolean statusUpSide;
    private final Image imgStatus[] = new Image[4];
    private int statusImage;
    public static int THINK = 0;
    public static int SMILE = 1;
    public static int CRY = 2;
    public static int NONE = 3;
    private boolean isColor;

    static 
    {
        BLACK = 0;
        WHITE = 0xffffff;
        RED = 0xff0000;
        GREEN = 65280;
        BLUE = 255;
        WOOD = 0xffd020;
        THINK = 0;
        SMILE = 1;
        CRY = 2;
        NONE = 3;
    }
    public FiveCanvas(FiveMIDlet m, Reg r)
    {
        midFive = m;
        reg = r;
        cmdNew = new Command("\u91CD\u73A9", 4, 2);
        cmdStop = new Command("\u9000\u51FA", 2, 1);
        cmdUndo = new Command("悔棋", 1, 3);
        addCommand(cmdNew);
        addCommand(cmdStop);
        setCommandListener(this);
        cavWidth = getWidth();
        cavHeight = getHeight();
        status = "";
        statusColor = 0;
        statusUpSide = true;
        for(int i = 0; i < 4; i++)
            imgStatus[i] = Image.createImage(1, 1);

        try
        {
            imgStatus[0] = Image.createImage("/Think.png");
        }
        catch(IOException _ex) { }
        try
        {
            imgStatus[1] = Image.createImage("/Smile.png");
        }
        catch(IOException _ex) { }
        try
        {
            imgStatus[2] = Image.createImage("/Cry.png");
        }
        catch(IOException _ex) { }
        statusImage = 3;
        isColor = Display.getDisplay(midFive).numColors() > 2;
    }

    private void calcSize()
    {
        font = Font.getDefaultFont();
        fWidth = font.charWidth('\u5B57');
        fHeight = font.getHeight();
        statusUpSide = cavHeight > cavWidth;
        if(statusUpSide)
        {
            cWidth = cavWidth;
            cHeight = cavHeight - fHeight;
        } else
        {
            cWidth = cavWidth - fWidth;
            cHeight = cavHeight;
        }
        borderLength = cWidth > cHeight ? cHeight : cWidth;
        gridLength = borderLength / borderSize;
        borderLength = gridLength * borderSize;
        borderX = (cWidth - borderLength) / 2;
        borderY = (cHeight - borderLength) / 2;
        if(statusUpSide)
            borderY += fHeight;
        stoneLength = gridLength - 2;
    }

    public void paint(Graphics g)
    {
        g.setColor(0xffffff);
        g.fillRect(0, 0, cavWidth, cavHeight);
        if(isColor)
        {
            g.setColor(0x41f22d);
            g.fillRect(borderX, borderY, borderLength, borderLength);
        }
        g.setColor(isColor ? 255 : 0);
        int y;
        for(int r = 0; r < borderSize; r++)
        {
            int x1 = borderX + gridLength / 2;
            int x2 = (x1 + borderLength) - gridLength;
            y = borderY + r * gridLength + gridLength / 2;
            g.drawLine(x1, y, x2, y);
        }

        int x;
        for(int c = 0; c < borderSize; c++)
        {
            x = borderX + c * gridLength + gridLength / 2;
            int y1 = borderY + gridLength / 2;
            int y2 = (y1 + borderLength) - gridLength;
            g.drawLine(x, y1, x, y2);
        }

        int computerColor;
        int manColor;
        if(computerFirst)
        {
            computerColor = 0;
            manColor = 0xffffff;
        } else
        {
            computerColor = 0xffffff;
            manColor = 0;
        }
        Dot triedDot = logic.triedDot();
        int triedRow = triedDot.row;
        int triedCol = triedDot.col;
        for(int r = 0; r < borderSize; r++)
        {
            for(int c = 0; c < borderSize; c++)
                if(r != triedRow || c != triedCol)
                {
                    int stone = logic.stones()[r][c];
                    if(stone != 0)
                    {
                        x = xByCol(c) - stoneLength / 2;
                        y = yByRow(r) - stoneLength / 2;
                        g.setColor(stone == 1 ? computerColor : manColor);
                        g.fillArc(x, y, stoneLength, stoneLength, 0, 360);
                        g.setColor(0);
                        g.drawArc(x, y, stoneLength, stoneLength, 0, 360);
                    }
                }

        }

        Dot lastDot = logic.lastDot();
        int lastRow = lastDot.row;
        int lastCol = lastDot.col;
        int cLast;
        if(isColor)
        {
            cLast = 0xff0000;
        } else
        {
            cLast = 0;
            switch(logic.stones()[lastRow][lastCol])
            {
            case 1: // '\001'
                cLast = manColor;
                break;

            case 2: // '\002'
                cLast = computerColor;
                break;
            }
        }
        g.setColor(cLast);
        x = xByCol(lastCol) - 3;
        y = yByRow(lastRow) - 3;
        g.drawRect(x, y, 6, 6);
        g.setFont(font);
        g.setColor(isColor ? statusColor : 0);
        if(statusUpSide)
        {
            g.drawImage(imgStatus[statusImage], 0, 0, 20);
            x = imgStatus[statusImage].getWidth();
            g.drawString(status, x, 0, 20);
        } else
        {
            x = cWidth + fWidth;
            g.drawImage(imgStatus[statusImage], x, 0, 24);
            x = cWidth + fWidth / 2;
            y = imgStatus[statusImage].getHeight();
            for(int i = 0; i < status.length(); i++)
            {
                char c = status.charAt(i);
                g.drawChar(c, x, y, 17);
                y += fHeight;
            }

        }
    }

    private int xByCol(int col)
    {
        return borderX + col * gridLength + gridLength / 2;
    }

    private int yByRow(int row)
    {
        return borderY + row * gridLength + gridLength / 2;
    }

    protected void keyPressed(int keyCode)
    {
        if(!logic.gameEnd() && !logic.thinking())
        {
            int bs = borderSize;
            Dot lastDot = logic.lastDot();
            int r = lastDot.row;
            int c = lastDot.col;
            repaintAt(r, c);
            int ga = getGameAction(keyCode);
            if(ga == 2 || keyCode == 52)
            {
                if(--c < 0)
                    c = bs - 1;
                lastDot.setRowCol(r, c);
                repaintAt(r, c);
            } else
            if(ga == 5 || keyCode == 54)
            {
                if(++c >= bs)
                    c = 0;
                lastDot.setRowCol(r, c);
                repaintAt(r, c);
            } else
            if(ga == 1 || keyCode == 50)
            {
                if(--r < 0)
                    r = bs - 1;
                lastDot.setRowCol(r, c);
                repaintAt(r, c);
            } else
            if(ga == 6 || keyCode == 56)
            {
                if(++r >= bs)
                    r = 0;
                lastDot.setRowCol(r, c);
                repaintAt(r, c);
            } else
            if(ga == 8 || keyCode == 53)
                logic.manGo(r, c);
        }
    }

    protected void keyRepeated(int keyCode)
    {
        keyPressed(keyCode);
    }

    protected void pointerPressed(int x, int y)
    {
        if(!logic.gameEnd() && !logic.thinking())
        {
            int row = (y - borderY) / gridLength;
            int col = (x - borderX) / gridLength;
            logic.manGo(row, col);
        }
    }

    public void commandAction(Command c, Displayable s)
    {
        if(!logic.thinking())
            if(c == cmdStop)
                midFive.backHome();
            else
            if(c == cmdNew)
                newStage();
            else
            if(c == cmdUndo && !logic.undo())
                setStatus("\u4E0D\u80FD\u6094\u68CB!");
    }

    public void setOptions(int borderSize, boolean computerFirst, int degree)
    {
        this.borderSize = borderSize;
        this.computerFirst = computerFirst;
        this.degree = degree;
    }

    public boolean newStage()
    {
        addCommand(cmdUndo);
        addCommand(cmdStop);
        removeCommand(cmdNew);
        calcSize();
        logic = new FiveLogic(this, borderSize, computerFirst, degree);
        if(computerFirst)
            logic.computerGo();
        else
            setStatus("请下子\u8BF7\u4E0B\u5B50:");
        repaint();
        return true;
    }

    public void notifyGameEnd()
    {
        addCommand(cmdNew);
        addCommand(cmdStop);
        removeCommand(cmdUndo);
    }

    public void setStatus(String s)
    {
        setStatus(s, 0, 3);
    }

    public void setStatus(String s, int color, int image)
    {
        status = s;
        statusColor = color;
        statusImage = image;
        int x;
        int y;
        int w;
        int h;
        if(statusUpSide)
        {
            x = 0;
            y = 0;
            w = cavWidth;
            h = fHeight;
        } else
        {
            x = cWidth;
            y = 0;
            w = fWidth;
            h = cavHeight;
        }
        repaint(x, y, w, h);
    }

    public void repaintAt(int row, int col)
    {
        int pX = borderX + (col - 1) * gridLength;
        int pY = borderY + (row - 1) * gridLength;
        int pW = gridLength * 2;
        int pH = pW;
        repaint(pX, pY, pW, pH);
    }

}

⌨️ 快捷键说明

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