www.pudn.com > SampleColor.rar > Test1.java


import javax.microedition.lcdui.*; 
import javax.microedition.midlet.*; 
import java.io.*; 
import java.util.*; 
 
public class Test1 extends MIDlet implements CommandListener 
{ 
	private Command exitCommand; 
	private Command redoCommand; 
	private Command seleCommand; 
	private Command beginCommand; 
	private Command saveCommand; 
	 
	private List lbEx; 
	private Command seleItemCommand; 
	 
	private MainCanvas myCanvas; 
	 
	private int intLevel = 1; 
 
	public Test1() 
	{ 
		String [] SeleArray = {"第一级","第二级","第三级"}; 
		 
		exitCommand = new Command("退出",Command.EXIT,1); 
		redoCommand = new Command("重新玩",Command.SCREEN,2); 
		seleCommand = new Command("选择等级",Command.SCREEN,2); 
		saveCommand = new Command("保存游戏",Command.SCREEN,3); 
		myCanvas = new MainCanvas(); 
		myCanvas.addCommand(exitCommand); 
		myCanvas.addCommand(redoCommand); 
		myCanvas.addCommand(seleCommand); 
		myCanvas.addCommand(saveCommand); 
		myCanvas.setCommandListener(this); 
		 
		seleItemCommand = new Command("确定",Command.SCREEN,1); 
		lbEx = new List("选择级别:",List.EXCLUSIVE,SeleArray,null); 
		lbEx.addCommand(seleItemCommand); 
		lbEx.setCommandListener(this); 
	} 
 
	protected void startApp() throws MIDletStateChangeException 
	{ 
		Display.getDisplay(this).setCurrent(myCanvas); 
		myCanvas.drawLogo(); 
	} 
 
	protected void pauseApp() 
	{ 
	} 
 
	protected void destroyApp(boolean p1) 
	{ 
		myCanvas = null; 
	} 
 
	public void commandAction(Command c,Displayable d) 
	{ 
		if(c==exitCommand) 
		{ 
			destroyApp(false); 
			notifyDestroyed(); 
		} 
		else if(c==redoCommand) 
		{ 
			myCanvas.drawLogo(); 
		} 
		else if(c==seleCommand) 
		{ 
			Display.getDisplay(this).setCurrent(lbEx); 
		} 
		else if(c==seleItemCommand) 
		{ 
			intLevel = lbEx.getSelectedIndex()+1; 
			System.out.println(intLevel); 
			Display.getDisplay(this).setCurrent(myCanvas); 
			myCanvas.init(intLevel,false); 
			myCanvas.draw(true); 
		} 
		else if(c==saveCommand) 
		{ 
			 
		} 
	} 
 
	public class MainCanvas extends Canvas 
	{ 
		private Graphics bg; 
		private Image buf,backImg,logoImg; 
		private int Fees=0; 
		private int OldBlockNum=100; 
		private boolean ifShowMessage,hasPassed; 
						 
		private int black  = 0x000000; 
		private int white  = 0xffffff; 
		private int red    = 0xff0000; 
		private int lime   = 0x00ff00; 
		private int blue   = 0x0000ff; 
		private int yellow = 0xffff00; 
		private int cyan   = 0x00ffff; 
		private int fuchsia= 0xff00ff; 
		private int maroon = 0x800000; 
		private int green  = 0x008000; 
		private int navy   = 0x800080; 
 
		private int idxColor[] = {white,black,red,lime,blue,yellow,cyan,fuchsia, 
		maroon,green,navy}; 
 
		private byte intShow[][]; 
		private int intShowColor[][]; 
 
		private int allTop =5,allLeft =5; 
		private int Box_Color=green; 
		private int BlockHeight=8,BlockWidth=8; 
		private int curCol,curRow ; 
		private int ColorNum; 
		private int BackImgSum = 5; 
				 
		public int curLevel; 
 
		private boolean hasShowLogo = false; 
		 
		Random rand = new Random(); 
 
		public MainCanvas() 
		{ 
			int canvasWidth = getWidth(); 
			int canvasHeight= getHeight(); 
			//Create the buf 
			buf = Image.createImage(canvasWidth,canvasHeight); 
			bg = buf.getGraphics(); 
 
			intShow = new byte [10][10]; 
			intShowColor = new int [10][10]; 
			curLevel = 1; 
			init(curLevel,false); 
		} 
 
		public void init(int intLevel,boolean ifGoOn) 
		{ 
			 
			int intBackImgID = 1; 
			 
			OldBlockNum=100; 
			 
			if(ifGoOn == false) 
				Fees = 0; 
			 
			hasPassed = false; 
			 
			 
			if(intLevel==1) 
			{ 
				ColorNum=5; 
			} 
			else 
			{ 
				if(intLevel==2) 
				{ 
					ColorNum=6; 
				} 
				else 
					ColorNum=7; 
			} 
			 
			for(int i=0;i<10;i++) 
				for(int j=0;j<10;j++) 
				{ 
					intShow[i][j] = 1; 
					intShowColor[i][j] = idxColor[1+Math.abs(rand.nextInt())%ColorNum]; 
				} 
			curCol = 1; 
			curRow = 1; 
			curLevel = intLevel; 
			 
			//Creat the backImg 
			intBackImgID = 1+Math.abs(rand.nextInt())%BackImgSum; 
			try{ 
				backImg = Image.createImage("/images/A"+intBackImgID+".png"); 
			}catch(IOException e) 
			{ 
				backImg = null; 
			} 
		} 
		 
		 
 
		public void paint(Graphics g) 
		{ 
			g.drawImage(buf,0,0,Graphics.TOP|Graphics.LEFT); 
		} 
 
 
		public void saveGame() 
		{ 
			 
		} 
 
		public void drawLogo() 
		{ 
			bg.setColor(white); 
			bg.fillRect(0,0,getWidth(),getHeight()); 
			 
			try{ 
				logoImg = Image.createImage("/images/B1.png"); 
			}catch(IOException e) 
			{ 
				backImg = null; 
			} 
			if(backImg!=null) 
			{ 
				bg.drawImage(logoImg,(getWidth()-logoImg.getWidth())/2,0,Graphics.TOP|Graphics.LEFT); 
			} 
			 
			hasShowLogo = false; 
			 
			repaint(); 
		} 
 
		public void draw(boolean ifShowMsg) 
		{ 
			//draw somw Block 
			drawBackgroup(red); 
			 
			//draw backgroup 
			if(backImg!=null) 
				bg.drawImage(backImg,allLeft,allTop,Graphics.TOP|Graphics.LEFT); 
			 
			//draw blocks 
			for(int i=0;i<10;i++) 
				for(int j=0;j<10;j++) 
					if(intShow[i][j] == 1) 
						drawBlock(j+1,i+1,intShowColor[i][j],0xff00ff); 
 
			//draw moveable box 
			drawMoveBlock(curCol,curRow,Box_Color,black); 
 
			//draw Message 
			drawMessage(ifShowMsg); 
			 
			repaint(); 
		} 
 
		private void drawMessage(boolean ifShowMessage) 
		{ 
			int BlockNum = 0; 
			 
			if(ifShowMessage) 
			{ 
				BlockNum = BlockSum(); 
				Fees = Fees + (OldBlockNum - BlockNum) ; 
				OldBlockNum = BlockNum; 
			} 
			 
			bg.drawString("得分:",103,10,Graphics.TOP|Graphics.LEFT); 
			bg.drawString(""+Fees,108,40,Graphics.TOP|Graphics.LEFT); 
		} 
		 
		private void drawEndMessage(boolean ifPassed) 
		{ 
			 
		} 
		 
		private void drawBackgroup(int Color) 
		{ 
			bg.setColor(white); 
			bg.fillRect(0,0,getWidth(),getHeight()); 
 
			bg.setColor(Color); 
			bg.drawRect(0,0,BlockWidth*10+10,BlockHeight*10+10 ); 
		} 
 
		/********************* 
		 *功能:画一个方块 
		 *Col - 方块所在的列号(1-10)  Row - 方块所在的行号 (1-10) 
		 *inColor - 方块内部颜色值 
		 *outColor - 方块边框颜色值 
		 ********************/ 
		private void drawBlock(int Col,int Row,int inColor,int outColor) 
		{ 
			int x,y; 
			x = allTop + (Col-1) * BlockHeight; 
			y = allLeft + (Row-1) * BlockWidth; 
			//fill a rect 
			bg.setColor(inColor); 
			bg.fillRect(x,y,BlockWidth,BlockHeight); 
			//draw a Rect 
			bg.setColor(outColor); 
			bg.drawRect(x,y,BlockWidth,BlockHeight); 
		} 
 
		/********************* 
		 *画可移动的方块 
		 *Col - 方块所在的列号(1-10)  Row - 方块所在的行号 (1-10) 
		 *inColor - 方块内部颜色值 
		 *outColor - 方块边框颜色值 
		 ********************/ 
		private void drawMoveBlock(int curCol,int curRow,int inColor,int outColor) 
		{ 
			int x,y; 
			x = allTop + (curCol-1) * BlockHeight; 
			y = allLeft + (curRow-1) * BlockWidth; 
			//draw a Rect 
			bg.setColor(outColor); 
			bg.drawRect(x,y,BlockWidth,BlockHeight); 
			bg.drawRect(x-2,y-2,BlockWidth+4,BlockHeight+4); 
		} 
 
		// curRow = 0-9 , curCol = 0-9 
		private int delBlock(int curRow,int curCol,int hasDelNum) 
		{ 
		 
			int i,k; 
			int deletedNum,d1,d2,d3,d4; 
			int curColor; 
			 
			curColor = intShowColor[curRow][curCol]; 
			deletedNum = 1; 
			intShow[curRow][curCol] = 0; 
			intShowColor[curRow][curCol] = 0; 
			d1=0;d2=0;d3=0;d4=0; 
			 
			hasDelNum = hasDelNum + 1; 
			 
			k=curCol; 
			for(i=curCol+1;i<10;i++) 
			{ 
				if((intShow[curRow][i] == 1)&&(intShowColor[curRow][i]==curColor)&&(i==k+1)) 
				{ 
					k=i; 
					//deletedNum = deletedNum + delBlock(curRow,i,deletedNum); 
					d1 =  hasDelNum + delBlock(curRow,i,hasDelNum); 
				} 
				else 
				{ 
					continue; 
				} 
			} 
 
			k=curCol; 
			for(i=curCol-1;i>=0;i--) 
			{ 
				if((intShow[curRow][i] == 1)&&(intShowColor[curRow][i]==curColor)&&(i==k-1)) 
				{ 
					k=i; 
					//deletedNum = deletedNum + delBlock(curRow,i,deletedNum); 
					d2 = hasDelNum + delBlock(curRow,i,hasDelNum); 
				} 
				else 
				{ 
					continue; 
				} 
			} 
 
			k=curRow; 
			for(i=curRow+1;i<10;i++) 
			{ 
				if((intShow[i][curCol] == 1)&&(intShowColor[i][curCol]==curColor)&&(i==k+1)) 
				{ 
					k=i; 
					//deletedNum = deletedNum + delBlock(i,curCol,deletedNum); 
					d3 = hasDelNum + delBlock(i,curCol,hasDelNum); 
				} 
				else 
				{ 
					continue; 
				} 
			} 
			 
			k=curRow; 
			for(i=curRow-1;i>=0;i--) 
			{ 
				if((intShow[i][curCol] == 1)&&(intShowColor[i][curCol]==curColor)&&(i==k-1)) 
				{ 
					k=i; 
					//deletedNum = deletedNum + delBlock(i,curCol,deletedNum); 
					d4 = hasDelNum + delBlock(i,curCol,hasDelNum); 
				} 
				else 
				{ 
					continue; 
				} 
			} 
						 
			deletedNum = deletedNum + d1+d2+d3+d4; 
			 
			if(deletedNum == 1) 
			{ 
				if(hasDelNum == 1) 
				{ 
					intShow[curRow][curCol] = 1; 
					intShowColor[curRow][curCol] = curColor; 
				} 
				return 0; 
			} 
			else 
				return deletedNum; 
		} 
 
		private int BlockSum() 
		{ 
			int i,j; 
			int BlockSum1=0; 
			for(i=0;i<10;i++) 
				for(j=0;j<10;j++) 
					if(intShow[i][j]==1) 
						BlockSum1 = BlockSum1 + 1; 
			return BlockSum1; 
		} 
		 
		private boolean hasSameBlock() 
		{ 
			int i,j; 
			byte m,n; 
			 
			boolean hasBlock1 = false; 
			 
			//check rows 
			for(i=0;i<10;i++) 
				for(j=0;j<9;j++) 
					if(intShow[i][j]==intShow[i][j+1]) 
					{ 
						hasBlock1 = true; 
						continue; 
					} 
			//check cols 
			for(j=0;j<10;i++) 
				for(i=0;i<9;i++) 
					if(intShow[i][j]==intShow[i+1][j]) 
					{ 
						hasBlock1 = true; 
						continue; 
					} 
			 
			return hasBlock1; 
		} 
 
		private void packAll() 
		{ 
			int i; 
 
			for(i=0;i<10;i++) 
			{ 
				packCol(i); 
			} 
			for(i=0;i<10;i++) 
			{ 
				packRow(i); 
			} 
		} 
 
		//curRow = 0-9 
		private void packRow(int curRow) 
		{ 
			int i,j; 
 
			int k; 
			for(i=0;i<10;i++) 
			{ 
				k=0; 
				if(intShow[curRow][i]==0) 
				{ 
					for(j=i+1;j<10;j++) 
					{ 
						if(intShow[curRow][j]==1) 
							k=k+1; 
						intShow[curRow][j-1] = intShow[curRow][j]; 
						intShowColor[curRow][j-1] = intShowColor[curRow][j]; 
					} 
					intShow[curRow][9]=0; 
					intShowColor[curRow][9]=0; 
					if (k>0) 
						if(intShow[curRow][i]==0)i--; 
				} 
			} 
		} 
 
		//curCol = 0-9 
		private void packCol(int curCol) 
		{ 
			int i,j,k; 
			for(i=9;i>=0;i--) 
			{ 
				k=0; 
				if(intShow[i][curCol]==0) 
				{ 
					for(j=i-1;j>=0;j--) 
					{ 
						if (intShow[j][curCol]==1) 
							k=k+1; 
						intShow[j+1][curCol] = intShow[j][curCol]; 
						intShowColor[j+1][curCol] = intShowColor[j][curCol]; 
					} 
					intShow[0][curCol] = 0; 
					intShowColor[0][curCol]=0; 
 
					if(k>0) 
						if(intShow[i][curCol]==0) 
							i++; 
				} 
			} 
 
		} 
 
		protected void KeyPressed(int keyCode) 
		{ 
		} 
 
		protected void keyReleased(int keyCode) 
		{ 
			int action = getGameAction(keyCode); 
			int hasDeletedNum; 
 
			if(hasShowLogo) 
			{ 
				System.out.println("Do Now"); 
				if(!hasPassed) 
				{ 
					System.out.println("hasPassed:"+hasPassed); 
					switch(action) 
					{ 
						case UP: 	//-1 
							curRow = curRow - 1; 
							if (curRow < 1) curRow = 1; 
							ifShowMessage = false; 
							break; 
						case DOWN: 	//-2 
							curRow = curRow + 1; 
							if (curRow > 10 ) curRow = 10; 
							ifShowMessage = false; 
							break; 
						case LEFT:	//-3 
							curCol = curCol - 1; 
							if (curCol < 1) curCol = 1; 
							ifShowMessage = false; 
							break; 
						case RIGHT:	//-4 
							curCol= curCol + 1; 
							if (curCol > 10) curCol = 10; 
							ifShowMessage = false; 
							break; 
						case FIRE: 
							if(	intShow[curRow-1][curCol-1] == 1 ) 
							{ 
								hasDeletedNum = delBlock( curRow-1,curCol-1,0 ); 
								if( hasDeletedNum>0 ) 
								{ 
									packAll(); 
									ifShowMessage = true; 
								} 
								else 
									ifShowMessage = false ; 
							} 
							else 
								ifShowMessage = false ; 
							break; 
					} 
					 
					System.out.println(action); 
					 
					draw(ifShowMessage); 
					 
					if(!hasSameBlock()) 
					{ 
						System.out.println("hasSameBlock:" + hasSameBlock()); 
						if(BlockSum()<=10) 
						{ 
							//passed 
							drawEndMessage(true);			 
						} 
						else 
						{ 
							//no passed 
							drawEndMessage(false); 
						} 
						hasPassed = true; 
					} 
				} 
				else 
				{ 
					System.out.println("Do Now1"); 
					System.out.println("hasPassed:"+hasPassed); 
					init(1,true); 
					draw(true);					 
				}		 
			} 
			else 
			{ 
				System.out.println("Do Now2"); 
				hasShowLogo = true; 
				init(1,false); 
				draw(true);		 
			} 
			System.out.println("Do Now3"); 
		} 
		 
		protected void KeyRepeated(int keyCode) 
		{ 
			keyReleased(keyCode); 
		} 
 
		protected void pointerDragged(int x,int y) 
		{ 
		} 
 
		protected void pointerPressed(int x,int y) 
		{ 
		} 
 
		protected void pointerReleased(int x,int y) 
		{ 
		} 
 
	} 
 
}