import java.util.Random;
import javax.microedition.lcdui.*;
public class GameCanvas extends Canvas implements Runnable,CommandListener
{
// private final int log = 0;
// private final int
int allRects[][] ;
CubeGame cubeGame;
private int score;
private FourRects activeRects;
// private FourRects oldRects;
public GameCanvas()
{
}
public GameCanvas(CubeGame cubeGame)
{
score = 0;
allRects=new int[Config.xRects][Config.yRects];
for(int i=0;i<Config.xRects;++i)
for(int j=0;j<Config.yRects;++j)
this.allRects[i][j]=0;
this.cubeGame = cubeGame;
this.setCommandListener(this);
Random random = new Random(1024);
int typeNum = random.nextInt() % 19;
if(typeNum < 0)
typeNum = -typeNum;
activeRects = new FourRects(Config.xRects/2,0,(int)System.currentTimeMillis()%19);
this.start();
}
public void activeReflectAll()
{
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
{
if(activeRects.rects[i][j]==1)
this.allRects[activeRects.xLeft+i][activeRects.yTop+j]
= 1;
}
}
public void start()
{
Thread th = new Thread(this);
th.start();
}
public void doPauseApp()
{
}
protected void paint(Graphics g)
{
// TODO Auto-generated method stub
g.setColor(255, 255, 255);
g.fillRect(0, 0, Config.screenWidth, Config.screenHight);
g.translate(Config.fieldLeft, Config.fieldTop);
g.setColor(0, 0, 255);
for(int i=0;i<Config.xRects;++i)
for(int j=0;j<Config.yRects;++j)
{
if(this.allRects[i][j]==1)
{
int left = i* Config.smallRectLength;
int top = j* Config.smallRectLength;
g.fillRect(left, top,
Config.smallRectLength, Config.smallRectLength);
}
}
this.activeRects.show(g);
GameField.show(g);
}
public void run()
{
// TODO Auto-generated method stub
while(true)
{
try
{
Thread.sleep(1000);
if(GameUtil.canMoveDown(this, this.activeRects))
this.activeRects.moveDown();
else
{
this.activeReflectAll();
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j)
{
if(this.activeRects.rects[i][j]==1)
{
int lineY = this.activeRects.yTop + j;
if(GameUtil.isLineFull(this, lineY))
{
GameUtil.removeLine(this, lineY);
++this.score;
System.out.println("Your score is: "+
String.valueOf(this.score)+"00");
}
}
}
}
this.repaint();
Random random = new Random(1024);
int typeNum = random.nextInt() % 19;
if(typeNum < 0)
typeNum = -typeNum;
activeRects = new FourRects(Config.xRects/2,0,(int)System.currentTimeMillis()%19);
}
this.repaint();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void commandAction(Command arg0, Displayable arg1)
{
// TODO Auto-generated method stub
}
protected synchronized void keyPressed(int keyCode)
{
this.repaint();
if(keyCode == 0)
return;
int gameCode = this.getGameAction(keyCode);
switch(gameCode)
{
case UP:
if(GameUtil.canChangeType(this, this.activeRects))
{
activeRects.changeType();
this.repaint();
}
break;
case LEFT:
if(GameUtil.canMoveLeft(this, this.activeRects))
{
this.activeRects.moveLeft();
this.repaint();
}
break;
case DOWN:
while(GameUtil.canMoveDown(this, this.activeRects))
{
this.activeRects.moveDown();
}
this.activeReflectAll();
for(int i=0;i<4;++i)
{
for(int j=0;j<4;++j)
{
if(this.activeRects.rects[i][j]==1)
{
int lineY = this.activeRects.yTop + j;
if(GameUtil.isLineFull(this, lineY))
{
GameUtil.removeLine(this, lineY);
++this.score;
System.out.println("Your score is: "+
String.valueOf(this.score)+"00");
}
}
}
}
this.repaint();
Random random = new Random(1024);
int typeNum = random.nextInt() % 19;
if(typeNum < 0)
typeNum = -typeNum;
activeRects = new FourRects(Config.xRects/2,0,(int)System.currentTimeMillis()%19);
break;
case RIGHT:
if(GameUtil.canMoveRight(this, this.activeRects))
{
this.activeRects.moveRight();
this.repaint();
}
break;
}
}
public void printAllElements()
{
for(int j=0;j<Config.yRects;++j)
{
for(int i=0;i<Config.xRects;++i)
{
System.out.print(this.allRects[i][j]+" ");
}
System.out.println();
}
}
}