public class BlocksCanvas extends Canvas implements Runnable
{
Graphics bg;
Image buf;
public BlocksCanvas()
{
......
height = getHeight();
width = getWidth();
//按屏幕大小建立缓冲对象
buf = Image.createImage(width, height);
//将缓冲对象的Graphics附给bg
bg = buf.getGraphics();
......
}
public void run()
{......
for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
{//画方块
drawBlock(x,y);
}
}
repaint();
}
private void drawBlock(int block_x, int block_y)
{
//取得方块的坐标
int x = getLeft(block_x);
int y = getTop(block_y);
//取得方块的颜色
int c= board[block_x][block_y];
bg.drawImage(imgs[c], x, y, Graphics.TOP | Graphics.LEFT);
}
public void paint(Graphics g)
{
g.drawImage(buf, 0, 0, Graphics.TOP | Graphics.LEFT);
}
} |