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

📄 puzzlecanvas.java

📁 手機遊戲,把數字排以順序,初學人士學習用
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

class PuzzleCanvas extends Canvas implements CommandListener{

	SliderPuzzle sliderPuzzle;

	int screenWidth, screenHeight;
	int PuzzleOffset_x;
	int PuzzleOffset_y = 20;
	int CellWidth = 25;
	int CellHeight = 25;
	int CellInRow = 4;
	int CellInColumn = 4;
	private PuzzleCell[][] puzzleCell = new PuzzleCell[4][4];

	int EmptyCellLocation_i = 3;	// y direction
	int EmptyCellLocation_j = 3;	// x direction

	private Command quitCmd = new Command("Exit", Command.EXIT, 1);

	PuzzleCanvas(SliderPuzzle sliderPuzzle){
		this.sliderPuzzle = sliderPuzzle;		

		screenWidth = getWidth();
		screenHeight = getHeight();
		PuzzleOffset_x = (screenWidth-(CellWidth*CellInRow))/2;
//		System.out.println("screenWidth: " + screenWidth);
//		System.out.println("screenHeight: " + screenHeight);

		initPuzzleCells();

		addCommand(quitCmd);
		setCommandListener(this);

	}

	public void commandAction(Command c, Displayable s){
//		System.out.println(c);
		if (c == quitCmd)
		{	
//			System.out.println("quitCmd pressed.");
			sliderPuzzle.doExit();
		}
	}


	protected void paint(Graphics g){
		g.setColor(0x00000000);
		g.fillRect(0, 0, screenWidth, screenHeight);
		
		RepaintCells(g);

	}

	private void initPuzzleCells()
	{	
//		System.out.println("initPuzzleCells");

		int cnt=1;
		for(int i=0;i<=3;i++)
		{	for(int j=0;j<=3;j++,cnt++)
			{	puzzleCell[i][j] = new PuzzleCell(cnt,(String)("/Puzzle_"+cnt+".png"));
				//	System.out.println("puzzleCell[" + i + "][" + j + "]" + " Created");
			}
		}

//		for(int i=0;i<=3;i++)
//		{	for(int j=0;j<=3;j++)
//			{	System.out.println("puzzleCell[" + i + "][" + j + "]" + " = " + puzzleCell[i][j].GetIndex());
//			}
//		}
	}

	private void MoveUp()
	{	PuzzleCell TempCell;
		if (EmptyCellLocation_i != 3)
		{	TempCell = puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j];
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = 
					puzzleCell[EmptyCellLocation_i+1][EmptyCellLocation_j];
			EmptyCellLocation_i++;
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = TempCell;
		}
	}

	private void MoveDown()
	{	PuzzleCell TempCell;
		if (EmptyCellLocation_i != 0)
		{	TempCell = puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j];
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = 
					puzzleCell[EmptyCellLocation_i-1][EmptyCellLocation_j];
			EmptyCellLocation_i--;
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = TempCell;
		}
	}

	private void MoveRight()
	{	PuzzleCell TempCell;
		if (EmptyCellLocation_j != 0)
		{	TempCell = puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j];
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = 
					puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j-1];
			EmptyCellLocation_j--;
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = TempCell;
		}
	}

	private void MoveLeft()
	{	PuzzleCell TempCell;
		if (EmptyCellLocation_j != 3)
		{	TempCell = puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j];
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = 
					puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j+1];
			EmptyCellLocation_j++;
			puzzleCell[EmptyCellLocation_i][EmptyCellLocation_j] = TempCell;
		}
	}

	private void RepaintCells(Graphics g)
	{
		int cnt=1;
		for(int i=0;i<=3;i++)
		{	for(int j=0;j<=3;j++,cnt++)
			{	
//				puzzleCell[i][j].PrintCellIndex();

/*				g.drawImage(puzzleCell[i][j].GetCellImg(), 
					PuzzleOffset_x + j*25, 
					PuzzleOffset_y + i*25, 
					Graphics.TOP|Graphics.LEFT);
*/
				puzzleCell[i][j].DrawCell(g, PuzzleOffset_x + j*25, PuzzleOffset_y + i*25);
			


			}
		}
	}

	protected synchronized void keyPressed(int keyCode)
	{	int gameCode = getGameAction(keyCode);
		switch(gameCode){
			case UP:	MoveUp();
						break;
			case DOWN:	MoveDown();
						break;
			case LEFT:	MoveLeft();
						break;
			case RIGHT:	MoveRight();
						break;
		}
		repaint();
	}
		


}

⌨️ 快捷键说明

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