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

📄 clcanvas.java

📁 用J2ME写的一种七彩连珠(五子连线)的手机游戏 通用程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;
import java.util.Random;

import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class ClCanvas extends Canvas implements CommandListener
{
	//小球 (Cfg.SIZE为格子数量)
	Ball[][] balls ;

	Image curPos;
	Image markPos;

	private Image imgBackGround;
	private Image imgBackGroundReal;
	
	int cx, cy;

	int mx, my; // marked x and y, -1 for no marking

	int score;
	int maxScore=0;

	int[] nextBallColors;

	Random random = new Random(System.currentTimeMillis());

	//菜单定义
    private Command cmdStop;
    private Command cmdNew;
    //private Command cmdUndo;

    private Colorlinez midletColorlinez;

    //可用显示参数
    private int cavWidth;
    private int cavHeight;
    private boolean isColor;
    //状态条信息显示位置标志 (状态条为一行信息)
    private boolean statusUpSide;//true提示在上面(false为在右面)
    //计算出的各种实际大小
    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 Ball ballImage;
    
    private boolean endOfGame;
    
    public ClCanvas(Colorlinez m) {
		midletColorlinez=m;
        cmdNew = new Command("重玩", 4, 2);//\u91CD\u73A9
        cmdStop = new Command("退出"+getHeight(), 2, 1);//\u9000\u51FA
        //cmdUndo = new Command("悔棋"+getWidth(), 1, 3); //\u6094\u68CB
        addCommand(cmdNew);
        addCommand(cmdStop);
        setCommandListener(this);

        cavWidth = getWidth();
        cavHeight = getHeight();
        recordload();
     	
	}

    //初始化
	public void init() {
		endOfGame = false;
		score=0;
		nextBallColors=null;
	      
	        //calcSize(); //根据屏幕大小,计算实际尺寸
	        balls = new Ball[Cfg.SIZE][Cfg.SIZE];
	        ballImage=new Ball();	//提示用的球
	        //ballImage.setBall_DIMSzie(stoneLength);
	        
	        isColor = Display.getDisplay(midletColorlinez).numColors() > 2;
	        //用stoneLength 代替Cfg.BALL_DIM
	    	curPos = Image.createImage(stoneLength + Cfg.EDGE * 2, stoneLength
	    			+ Cfg.EDGE * 2);

	    	markPos = Image.createImage(stoneLength + Cfg.EDGE * 2, stoneLength
	    			+ Cfg.EDGE * 2);
	    	
		getRandomBalls(Cfg.INIT_BALL_NUM); //初试为5个球在界面上
		
		//把背景显示
        try {
			imgBackGround = Image.createImage("/background.PNG");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (gridLength==Cfg.Ball_REAL_SIZE) {
			imgBackGroundReal=imgBackGround;
		} else {
		imgBackGroundReal=ImageUtil.scale(imgBackGround,gridLength,gridLength);
		}
		
		initPos(curPos, Cfg.CUR_POS_COLOR);
		initPos(markPos, Cfg.MARK_POS_COLOR);

		cx = Cfg.SIZE / 2;
		cy = Cfg.SIZE / 2;

		mx = -1;
		my = -1;
	}
    private void calcSize()
    {
    	 statusUpSide = true; //提示在上面(false为在右面)
    	 
        font = Font.getDefaultFont();
        fWidth = font.charWidth('字');//\u5B57
        fHeight = font.getHeight();
        //默认为上下, 计算实际的高度和宽度
        //statusUpSide = cavHeight > cavWidth;
		if(statusUpSide)
        {
            cWidth = cavWidth;
            cHeight = cavHeight - 2*fHeight;
        } else
        {
            cWidth = cavWidth - fWidth;
            cHeight = cavHeight;
        }
		
        borderLength = cWidth > cHeight ? cHeight : cWidth;  //
        gridLength = borderLength / Cfg.SIZE;
        borderLength = gridLength * Cfg.SIZE;
        borderX = (cWidth - borderLength) / 2;
        borderY = (cHeight - borderLength) / 2;
        if(statusUpSide)
            borderY += 2*fHeight; //2行
        stoneLength = gridLength - 4;
        //Cfg.BALL_DIM=stoneLength;
        //System.out.println("stoneLength="+stoneLength);
    }
    
    //画位置显示框
	void initPos(Image img, int color) {
		
		Graphics g = img.getGraphics();
		g.drawImage(imgBackGroundReal,0,0,Graphics.LEFT | Graphics.TOP);
		//g.setColor(Cfg.BGCOLOR);
		//g.fillRect(0, 0, img.getWidth(), img.getHeight());
		g.setColor(color);
		g.drawLine(0, 0, stoneLength + 1, 0);
		g.drawLine(0, stoneLength + 1, stoneLength + 1, stoneLength + 1);
		g.drawLine(0, 0, 0, stoneLength + 1);
		g.drawLine(stoneLength + 1, 0, stoneLength+ 1, stoneLength + 1);
	}
	//随机产生颜色,产生小球放置,并生成下一次小球的颜色
	void getRandomBalls(int num) {
		if (nextBallColors != null) {
			randomBall(nextBallColors);
		} else {
			int[] colors = randomColor(num);
			randomBall(colors);
		}
		nextBallColors = randomColor(Cfg.RUNNING_BALL_NUM);
	}

	//(从七色中)随机产生颜色
	int[] randomColor(int num) {
		int[] colors = new int[num];
		for (int i = 0; i < num; ++i) {
			colors[i] = Math.abs(random.nextInt()) % Cfg.BALL_NUM;
		}
		return colors;
	}
	//从所有格子中产生小球,(去掉已使用的)
	void randomBall(int[] colors) {
		int num = colors.length;
		//判断棋盘上是否有足够的可用位置(>num 个)
		
		for (int i = 0; i < num; ++i) {
			int pos = Math.abs(random.nextInt()) % (Cfg.SIZE * Cfg.SIZE);
			int xPos = pos % Cfg.SIZE;
			int yPos = pos / Cfg.SIZE;
			if (balls[xPos][yPos] != null) { //需要找到可以放置的球为止,当结束时,可能产生死循环,需要提前判断
				--i; 
				continue;
			}
			balls[xPos][yPos] = new Ball(colors[i],stoneLength);
		}
	}

	/**
	 * expunge the board!
	 */
	int expunge() {
		boolean[][] tobeEx = new boolean[Cfg.SIZE][Cfg.SIZE];

		// row
		for (int j = 0; j < Cfg.SIZE; ++j) {
			expungeXY(0, j, 1, 0, tobeEx);
		}

		// column
		for (int i = 0; i < Cfg.SIZE; ++i) {
			expungeXY(i, 0, 0, 1, tobeEx);
		}

		// slash
		for (int j = Cfg.EX_NUM - 1; j < Cfg.SIZE; ++j) {
			expungeXY(0, j, 1, -1, tobeEx);
		}
		for (int i = 1; i <= Cfg.EX_NUM; ++i) {
			expungeXY(i, Cfg.SIZE - 1, 1, -1, tobeEx);
		}

		// back slash
		for (int i = Cfg.SIZE - Cfg.EX_NUM; i >= 0; --i) {
			expungeXY(i, 0, 1, 1, tobeEx);
		}
		for (int j = 1; j <= Cfg.SIZE - Cfg.EX_NUM; ++j) {
			expungeXY(0, j, 1, 1, tobeEx);
		}

		int exNum = 0;
		for (int i = 0; i < Cfg.SIZE; ++i) {
			for (int j = 0; j < Cfg.SIZE; ++j) {
				if (tobeEx[i][j]) {
					balls[i][j] = null;
					++exNum;
				}
			}
		}

		return exNum;
	}

	void expungeXY(int fx, int fy, int dx, int dy, boolean[][] mark) {

		int lastId = -1;
		int total = 0;
		for (int i = fx, j = fy; 0 <= i && i < Cfg.SIZE && 0 <= j
				&& j < Cfg.SIZE; i += dx, j += dy) {

			if (balls[i][j] == null) {
				if (total >= Cfg.EX_NUM) {
					int exi = i, exj = j; // 从上一个才是真的!!
					for (int c = 0; c < total; ++c) {
						exi -= dx;
						exj -= dy;
						mark[exi][exj] = true;
					}
				}
				total = 0;
				lastId = -1;
				continue;
			}
			if (balls[i][j].id == lastId) {
				++total;
				if (total >= Cfg.EX_NUM
						&& (i + dx < 0 || i + dx >= Cfg.SIZE || j + dy < 0 || j
								+ dy >= Cfg.SIZE)) {
					int exi = i, exj = j;
					for (int c = 0; c < total; ++c) {
						mark[exi][exj] = true;
						exi -= dx;
						exj -= dy;
					}
				}
			} else {
				if (total >= Cfg.EX_NUM) {
					int exi = i, exj = j; // 从上一个才是真的!!
					for (int c = 0; c < total; ++c) {
						exi -= dx;
						exj -= dy;
						mark[exi][exj] = true;
					}
				}
				lastId = balls[i][j].id;
				total = 1;
			}
		}
	}

	public void paint(Graphics g) {
		// System.out.println("before paint: " + mx + " " + my + " " + cx + " "
		// + cy);

        
		g.setColor(Cfg.BGCOLOR);
		g.fillRect(0, 0, cavWidth, cavHeight);
		//g.fillRect(0, 0, getWidth(), getHeight());
		/*
		g.setColor(0xffffff);
        g.fillRect(0, 0, cavWidth, cavHeight);
        if(isColor)
        {
        	g.setColor(Cfg.BGCOLOR);
            g.fillRect(borderX-1, borderY-1, borderLength, borderLength);
        }
        */
		drawBoard(g);
		drawPos(g);
		drawBalls(g);
		drawScore(g);
		drawNextBalls(g);
	}
	//画提示球 首行显示 (20,0)
	void drawNextBalls(Graphics g) {
		g.drawString(" Next 3", Cfg.TIP_POS_X, 0, 0);
		for (int i = 0; i < nextBallColors.length; ++i) {
			//g.drawString("   Next 3   ", Cfg.TIP_POS_X, Cfg.TIP_POS_Y, 0);
			//int tipY = Cfg.TIP_POS_Y + Font.getDefaultFont().getHeight() + Cfg.LINE_GAP;
			int tipY =  Font.getDefaultFont().getHeight() ;//+ Cfg.LINE_GAP;
			g.drawImage(ballImage.imgs[nextBallColors[i]], 3 + Cfg.TIP_POS_X 

⌨️ 快捷键说明

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