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

📄 threadcanvas.java

📁 我编写得JAVA ME 手机俄罗斯方块
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;


public class ThreadCanvas extends Canvas implements Runnable {

	int x,y;
	Thread thd;
	int screen_width,screen_height;
	int cur_row;
	
	int [][] zigzag1 = {
			{1,1,0,0},
			{0,1,1,0},
			{0,0,0,0},
			{0,0,0,0},
			};
	int [][] zigzag2 = {
			{0,1,0,0},
			{1,1,0,0},
			{1,0,0,0},
			{0,0,0,0},
			};
	int [][] zigzag3 = {
			{1,1,0,0},
			{0,1,1,0},
			{0,0,0,0},
			{0,0,0,0},
			};
	int [][] zigzag4 = {
			{0,1,0,0},
			{1,1,0,0},
			{1,0,0,0},
			{0,0,0,0},
			};
	
	int [][] cur_rect;
	
	int [][]  grid = new int[16][10];
	
	
	public ThreadCanvas(){
		x=this.getWidth()/2-10;
		y=0;
		cur_row = 0;
		screen_width=100;
		screen_height=160;
		for(int i=0;i<16;i++){
			for(int j=0;j<10;j++){
				grid[i][j] = 0;
			}
		}
		cur_rect = zigzag3;
		setrect(0, 5, grid, cur_rect);
		thd = new Thread(this);
		thd.start();
	}
	protected void paint(Graphics g) {
		// TODO 自动生成方法存根
		g.setColor(0);
		g.fillRect(0,0,getWidth(),getHeight());
		
		//画面板
		g.setColor(255, 0, 255);
		g.drawRect(0, 0, screen_width,screen_height);
		

		//设置方块

		
		//画网格
		for(int i = 0 ; i < 16;i++){
			for(int j=0;j<10;j++){
				if(grid[i][j]==1)
					g.drawRect(j*10,i*10, 10, 10);
			}
		}
		g.setColor(0,255,255);
		g.drawRect(x, y, 10, 10);
		g.fillRect(x, y, 10, 10);

	}
	public void run() {
		// TODO 自动生成方法存根
		while(true){
			y+=20;
			clearrect(cur_row, 5, grid, cur_rect);
			cur_row++;
			setrect(cur_row, 5, grid, cur_rect);
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
			if(y>this.getHeight()){
				y=0;
			}
			if(cur_row>16-5){
				cur_row=0;
			}
			repaint();
		}
	}
	
	public void setrect(int row,int col,int[][] tagRect,int[][] srcRect){
		for(int i=0;i<4;i++){
			for(int j = 0;j<4;j++){
				tagRect[i+row][j+col] = srcRect[i][j];
			}
		}
	}
	public void clearrect(int row,int col,int[][] tagRect,int[][] srcRect){
		for(int i=0;i<4;i++){
			for(int j = 0;j<4;j++){
				tagRect[i+row][j+col] = 0;
			}
		}
	}
	protected void keyPressed(int keyCode){
		if(keyCode == -1){
			if(cur_rect == zigzag1){
				clearrect(cur_row, 5, grid, cur_rect);
				cur_rect=zigzag2;
				setrect(cur_row, 5, grid, cur_rect);
			}
			else{
				clearrect(cur_row, 5, grid, cur_rect);
				cur_rect=zigzag1;
				setrect(cur_row, 5, grid, cur_rect);
			}
			
			repaint();
		}
		System.out.println(123);
	}

}

⌨️ 快捷键说明

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