drawtable.java

来自「Java 开发的俄罗斯方块游戏」· Java 代码 · 共 57 行

JAVA
57
字号
import java.awt.*;

public class DrawTable extends Panel {
	
	int width, height;//空间的宽和高
	int distance ;//单元块之间的距离
	int localWith , localHeight;//单元块的宽和高
	Image img;//采用双缓冲技术防止屏幕闪烁,img和bg均为此目的而定义
	Graphics bg;

	public DrawTable(){		
		width = 300;
		height = 400;
		distance = 20;
		localWith = 18;
		localHeight = 18;
		img=createImage(width,height); 

		
		setLayout(null);
		setSize(this.width, this.height);
	}
	
	//update()是双缓冲的实例
	public void update(Graphics g){
		
		if(img == null){
			img = createImage(width, height);
			bg = img.getGraphics();
		}
		else{
			bg = img.getGraphics();
		}
		paint(bg);
		g.drawImage (img, 0, 0, this);
	}	

	public void paint(Graphics g){

		g.setColor(Color.white);
		g.fillRect(0,0, width, height);

		for(int i = 0; i < Table.getX(); i ++){
			for(int j = 0; j < Table.getY(); j ++){
				if(Table.mytable [i][j] == 1){
					g.setColor(Color.black);
					g.fillRect(	distance *j ,  distance *i, localWith, localHeight);
				}
				else{
					g.setColor(Color.lightGray);
					g.fillRect( distance *j , distance *i, localWith, localHeight);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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