import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BlockDiagram extends Frame{
public static final int GAME_LINE = 25;
public static final int GAME_LIST = 20;
public static final int BLOCK = 20;
Wall w1 = new Wall(20,100,20,400);
Wall w2 = new Wall(40,480,200,20);
Wall w3 = new Wall(240,100,20,400);
Tetris ts = new Tetris(this,100,120);
Image offScreenImage = null;
public static void main(String[] args) {
new BlockDiagram().launchFrame(); }
private void launchFrame() {
this.setLocation(200,300);
this.setSize(GAME_LIST*BLOCK,GAME_LINE*BLOCK);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setVisible(true);
this.setResizable(false);
new Thread(new PaintThread()).start();
this.addKeyListener(new KeyMonitor());
}
public void paint(Graphics g){
Color c = g.getColor();
g.setColor(Color.white);
g.fillRect(0, 0,GAME_LIST*BLOCK,GAME_LINE*BLOCK);
g.setColor(c);
ts.draw(g);
w1.draw(g);
w2.draw(g);
w3.draw(g);
}
public void update(Graphics g){
if(offScreenImage==null){
offScreenImage = this.createImage(GAME_LIST*BLOCK,GAME_LINE*BLOCK);
}
Graphics goff=offScreenImage.getGraphics();
paint(goff);
g.drawImage(offScreenImage, 0, 0, null);
}
private class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e){
ts.keyPressed(e);
}
}
class PaintThread implements Runnable{ @Override
public void run() {
while(true){
//repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Tetris {

    int x,y;
   
BlockDiagram bd;
Direction dir = Direction.D;
Block bk = null;
public final static int XSPEED=5;
public final static int YSPEED=5;
private boolean BL=false,BU=false,BR=false,BD=false;
Tetris(BlockDiagram bd,int x,int y){
this.bd = bd;
this.x = x;
this.y = y;

bk = new Block(x,y,Direction.D);

}

public void draw(Graphics g){
if(bk!=null)
bk.draw(g);
move();
}
/*private void move() {


y = y+YSPEED; 
if(BL&&!BR&&!BD){
x = x-XSPEED;
}else if(!BL&&BR&&!BD){
x = x+XSPEED;
}else if(!BL&&!BR&&!BD){
y = y+YSPEED;
}

}*/
public void move(){
y+=YSPEED;
switch(dir){
case L:
x-=XSPEED;
break;
case U:
y-=YSPEED;
break;
case R:
x+=XSPEED;
break;
case D:
y+=YSPEED;
break;
}
}
class Block{
private int y;
private int x;
Direction direction;
int w = bd.BLOCK;
int h = bd.BLOCK;
Block(int line,int list,Direction direction){
this.y = line;
this.x = list;
this.direction=direction;
}
private void draw(Graphics g){
int number1 = (int) (Math.random()*7);
int number2 = (int) (Math.random()*2);
int number3 = (int) (Math.random()*4);
Color c = g.getColor();
g.setColor(Color.blue);
switch(number1){
case 0:
g.fillRect( x, y,2*w,2*h);  //正方形
break;
case 1:
if(number2==0){
g.fillRect( x-bd.BLOCK, y,4*w,h); //横长方形
}else{
g.fillRect( x, y,w,4*h); //纵长方形

break;
case 2:
if(number2==0){

g.fillRect( x, y,2*w,3*h); 
g.clearRect(x+bd.BLOCK, y, w, h);
g.clearRect(x, y+2*bd.BLOCK, w, h);
}else{
g.fillRect( x, y,3*w,2*h); 
g.clearRect(x, y, w, h);
g.clearRect(x+2*bd.BLOCK, y+bd.BLOCK, w, h);

break; 
case 3:
if(number2==0){

g.fillRect( x, y,2*w,3*h); 
g.clearRect(x, y, w, h);
g.clearRect(x+bd.BLOCK, y+2*bd.BLOCK, w, h);
}else{
g.fillRect( x-bd.BLOCK, y,3*w,2*h); 
g.clearRect(x-bd.BLOCK, y+bd.BLOCK, w, h);
g.clearRect(x+bd.BLOCK, y, w, h);

break; 
case 4:
if(number3==0){
g.fillRect( x, y,3*w,2*h); 
g.clearRect(x, y, w, h);
g.clearRect(x+2*bd.BLOCK, y, w, h);
}else if(number3==1){
g.fillRect( x, y,2*w,3*h); 
g.clearRect(x, y, w, h);
g.clearRect(x, y+2*bd.BLOCK, w, h);
}else if(number3==2){
g.fillRect( x, y,3*w,2*h); 
g.clearRect(x, y+bd.BLOCK, w, h);
g.clearRect(x+2*bd.BLOCK, y+bd.BLOCK, w, h);
}else{
g.fillRect( x+bd.BLOCK, y,2*w,3*h); 
g.clearRect(x+2*bd.BLOCK, y, w, h);
g.clearRect(x+2*bd.BLOCK, y+2*bd.BLOCK, w, h);
}
break;
case 5:
if(number3==0){
g.fillRect( x, y,3*w,2*h); 
g.clearRect(x, y+bd.BLOCK, w, h);
g.clearRect(x+bd.BLOCK, y+bd.BLOCK, w, h);
}else if(number3==1){
g.fillRect( x, y,2*w,3*h); 
g.clearRect(x, y, w, h);
g.clearRect(x, y+bd.BLOCK, w, h);
}else if(number3==2){
g.fillRect( x, y,3*w,2*h); 
g.clearRect(x+bd.BLOCK, y, w, h);
g.clearRect(x+2*bd.BLOCK, y, w, h);
}else{
g.fillRect( x, y,2*w,3*h); 
g.clearRect(x+bd.BLOCK, y+bd.BLOCK, w, h);
g.clearRect(x+bd.BLOCK, y+2*bd.BLOCK, w, h);
}
break;
case 6:
if(number3==0){
g.fillRect( x-bd.BLOCK, y,3*w,2*h); 
g.clearRect(x, y+bd.BLOCK, w, h);
g.clearRect(x+bd.BLOCK, y+bd.BLOCK, w, h);
}else if(number3==1){
g.fillRect( x, y,2*w,3*h); 
g.clearRect(x, y+bd.BLOCK, w, h);
g.clearRect(x, y+2*bd.BLOCK, w, h);
}else if(number3==2){
g.fillRect( x-bd.BLOCK, y,3*w,2*h); 
g.clearRect(x-bd.BLOCK, y, w, h);
g.clearRect(x, y, w, h);
}else{
g.fillRect( x, y,2*w,3*h); 
g.clearRect(x+bd.BLOCK, y, w, h);
g.clearRect(x+bd.BLOCK, y+bd.BLOCK, w, h);
}
break;
}

g.setColor(c);

}

}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
BL = true;
break;
case KeyEvent.VK_RIGHT:
BR = true;
break; 
case KeyEvent.VK_DOWN:
BD = true;
break;
case KeyEvent.VK_UP:
BU=true;
break;
}
LocateDirection();
}
public void LocateDirection(){
if(BL&&!BU&&!BR&&!BD) dir=Direction.L;
else if(!BL&&BU&&!BR&&!BD) dir=Direction.U;
else if(!BL&&!BU&&BR&&!BD) dir=Direction.R;
else if(!BL&&!BU&&!BR&&BD) dir=Direction.D;

}

}
import java.awt.Graphics;
public class Wall {
private int x,y,w,h;
Wall(int x,int y,int w,int h){
this.x=x;
this.y=y;
this.w=w;
this.h=h;
}
void draw(Graphics g){
if(h==20){
g.drawLine(x,y,x+w,y);
g.drawLine(x, y+20, x+w, y+20);

for(int i=0;i<=w;i=i+20){
g.drawLine(x+i,y,x+i,y+20);
}
}
if(w==20){
g.drawLine(x,y,x,y+h);
g.drawLine(x+20, y, x+20, y+h);

for(int i=0;i<=h;i=i+20){
g.drawLine(x,y+i,x+20,y+i);
}
}
}}
public enum Direction {
L,U,R,D
}