这个就是我说的jc/*
 * Created on 2004-8-9
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package test;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;import javax.swing.JComponent;
import javax.swing.JPanel;/**
 * @author bluestone
 *负责整个游戏的主要运行部分。可以设定游戏面板的高度(height),宽度(wide),
 *行数(rows),列数(columns).rowH,rowW则代表每个方格的高度,宽度。当画图
 *的时候需要这些参数。
 *横坐标为X,纵坐标为Y,左上角坐标为(0,0),startX,startY但表每个图形出来
 *时的初始坐标。
 *usedColor,blankColor分别为方格占用和空白时的颜色。
 */
public class Terris extends JPanel { /**
 * 
 */
public Terris() {
super();
height = 300;
wide   = 150;
rows = 20;
columns = 10;
setSize(height+100,wide+100);
// setResizable(false);
rowH = height/rows;
rowW = wide/columns;
startX = 0;
startY = columns/2;
currentY = startX;
currentX = startY;
blankColor = Color.blue;
usedColor = Color.red;
// currentDone = false;
isUsed = new boolean[rows][columns];
for(int i=0;i<rows;++i)
for(int j=0;j<columns;++j)
// if(j%2 == 0)
    isUsed[i][j] = false;
// else
// isUsed[i][j] = true;
}
public void update(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setBackground(Color.gray);
for(int i=0,y=0;i<rows;++i,y+=rowH)
{
for(int j=0,x=0;j<columns;++j,x+=rowW)
{
if(isUsed[i][j])
{
g2d.setColor(usedColor);
g2d.fillRect(x+2,y+2,rowW-2,rowH-2);
g2d.drawRect(x,y,rowW-1,rowH-1);
}
else
{
g2d.setColor(blankColor);
// g2d.fillRect(x+1,y+1,rowW-1,rowH-1);
g2d.drawRect(x,y,rowW-1,rowH-1);
}
}
}
g2d.dispose();
}

public void paint(Graphics g)
{
update(g);
}

/**
 * 判断一个图形是否能够再向下移动,如果可以则返回false,否则返回true
 * @return
 */
public boolean hasDone()
{
int len = currentPic.length-1;
if(currentY+len+1 >= rows-1)
return true;
for(int i=0;i<len && i+currentY<rows;++i)
{
for(int j=0;j<currentPic[i].length;++j)
{
if(currentPic[i][j] && !currentPic[i+1][j] && isUsed[i+currentY+1][j+currentX])
return true;
}
}
for(int j=0;j<currentPic[0].length;++j)
{
if(currentPic[len][j] && isUsed[len+currentY+1][j+currentX])
return true;
}
return false;
}
/**
 * 使一个图形向下移动一格
 *
 */
public void down()
{
// if(isLegal())
// {
clearBefore();
currentY++;
fill();
// }
// else
// return;
repaint();
clearAfter();
}
/**
 * 使一个图形想左移动一格
 *
 */
public void left()
{
if(canLeft())
{
clearBefore();
currentX--;
fill();
}
else
return;
repaint();
// clearAfter();
} /**
 * 使一个图形向右移动一格
 *
 */
public void right()
{
if(currentX+1 >= columns)
return;
if(canRight())
{
clearBefore();
currentX++;
fill();
}
else
return;
repaint();
// clearAfter();
}

解决方案 »

  1.   

    public void rotate()
    {
    if(canRotate())
    {
    clearBefore();
         currentShap.rotate();
         setCurrentPic();
         fill();
    }
    else
    return ;
    repaint();
    // clearAfter();
    }
    public void sharpDown()
    {

    }
    /**
     * 判断一个图形是否可以向左移动
     * @return
     */
    public boolean canLeft()
    {
    if(currentX == 0)
    return false;
    int len = currentPic.length-1;
    for(int i=0;i<=len;++i)
    {
    for(int j=1;j<currentPic[i].length;++j)
    {
    if(currentPic[i][j] 
       && !currentPic[i][j-1] 
       && isUsed[i+currentY][j+currentX-1])
    return false;
    }
    }
    for(int j=0;j<currentPic.length;++j)
    {
    if(currentPic[j][0] && isUsed[j+currentY][currentX-1])
    return false;
    }
    return true;
    }
    /**
     * 判断是否可以向右移动
     * @return
     */
    public boolean canRight()
    {
    if(currentX == rows-1)
    return false;
    int len = currentPic.length-1;
    for(int i=0;i<=len;++i)
    {
    for(int j=0;j<currentPic[i].length-1;++j)
    {
    if(currentPic[i][j]
       && !currentPic[i][j+1] 
       && isUsed[i+currentY][j+currentX+1])
    return false;
    }
    }
    for(int j=0;j<currentPic.length;++j)
    {
    if(currentPic[j][currentPic[j].length-1] 
       && isUsed[j+currentY][currentX+currentPic[j].length])
    return false;
    }
    return true;
    }
    public boolean canRotate()
    {
    return true;
    }
    /**
     * 填充图形
     *
     */
    public void fill()
    {
    for(int i=0;i<currentPic.length;++i)
    for(int j=0;j<currentPic[0].length;++j)
    {
    if(currentPic[i][j])
         isUsed[i+currentY][j+currentX] = currentPic[i][j];
    }
    }
    /**
     * 清除之前的图象
     *
     */
    public void clearBefore()
    {
    for(int i=currentY;i<currentPic.length+currentY;++i)
    for(int j=currentX;j<currentPic[0].length+currentX;++j)
    {
    isUsed[i][j] = false;
    }
    }
    /**
     * 当一整行都被填充时应清除此行
     *
     */
    public void clearAfter()
    {
    int top = currentY;
    int bottom = currentY + currentPic.length;
    boolean canClear = false;
    for(int i=top;i<bottom;++i)
    {
    canClear = true;
    for(int j=0;j<columns;++j)
    {
    if(!isUsed[i][j])
    {
    canClear = false;
    break;
    }
    }
    if(canClear)
    {
    for(int l=i;l>0;--l)
    {
    for(int m=0;m<columns;++m)
    isUsed[l][m] = isUsed[l-1][m];
    }
    for(int k=0;k<columns;++k)
    isUsed[0][k] = false;
    repaint();
    }
    }
    }
    /**
     * 当此函数返回true时,表示产生图形的初始坐标已被占有,程序应结束
     * @return
     */
    public boolean failed()
    {
    return isUsed[startX][startY];
    }
    /**
     * @return Returns the blankColor.
     */
    public Color getBlankColor() {
    return blankColor;
    }
    /**
     * @param blankColor The blankColor to set.
     */
    public void setBlankColor(Color blankColor) {
    this.blankColor = blankColor;
    }
    /**
     * @return Returns the height.
     */
    public int getHeight() {
    return height;
    }
    /**
     * @param height The height to set.
     */
    public void setHeight(int height) {
    this.height = height;
    }
    /**
     * @return Returns the rowH.
     */
    public int getRowH() {
    return rowH;
    }
    /**
     * @param rowH The rowH to set.
     */
    public void setRowH(int rowH) {
    this.rowH = rowH;
    }
    /**
     * @return Returns the rowW.
     */
    public int getRowW() {
    return rowW;
    }
    /**
     * @param rowW The rowW to set.
     */
    public void setRowW(int rowW) {
    this.rowW = rowW;
    }
    /**
     * @return Returns the usedColor.
     */
    public Color getUsedColor() {
    return usedColor;
    }
    /**
     * @param usedColor The usedColor to set.
     */
    public void setUsedColor(Color usedColor) {
    this.usedColor = usedColor;
    }
    /**
     * @return Returns the wide.
     */
    public int getWide() {
    return wide;
    }
    /**
     * @param wide The wide to set.
     */
    public void setWide(int wide) {
    this.wide = wide;
    }
    /**
     * @return Returns the columns.
     */
    public int getColumns() {
    return columns;
    }
    /**
     * @param columns The columns to set.
     */
    public void setColumns(int columns) {
    this.columns = columns;
    }
    /**
     * @return Returns the rows.
     */
    public int getRows() {
    return rows;
    }
    /**
     * @param rows The rows to set.
     */
    public void setRows(int rows) {
    this.rows = rows;
    }
    /**
     * @param currentPos The currentPos to set.
     */
    public void setCurrentY(int currentPos) {
    this.currentY = currentPos;
    }
    /**
     * @param currentY The currentY to set.
     */
    public void setCurrentX(int currentY) {
    this.currentX = currentY;
    }
    /**
     * @param currentShap The currentShap to set.
     */
    public void setCurrentPic() {
    this.currentPic = currentShap.picture();
    }
    /**
     * @param currentShap The currentShap to set.
     */
    public void setCurrentShap(Shap currentShap) {
    this.currentShap = currentShap;
    // currentDone = false;
    setCurrentPic();
    setCurrentX(startY);
    setCurrentY(startX);
    }
    /**
     * @param startY The startY to set.
     */
    public void setStartX(int startX) {
    this.startX = startX;
    }
    private int height;
    private int wide;
    private int rowH;
    private int rowW;
    private int rows;
    private int columns;
    private int currentY;//left top当前图形的左上角坐标
    private int currentX;
    private int startX;
    private int startY;
    private Color blankColor;
    private Color usedColor;
    // private boolean currentDone;
    private boolean[][] isUsed;//这个面板的格
    private boolean[][] currentPic;//当前图形的具体表示
    private Shap currentShap;//当前的图形
    }
      

  2.   

    这个就是我说的jf
    /*
     * Created on 2004-11-9
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package test;import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.util.Random;import javax.swing.JFrame;/**
     * @author bluestone
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Game extends JFrame 
                            implements Runnable,KeyListener,WindowListener { private Terris terris;
    private Message message;
    private char leftkey;
    private char rightkey;
    private char rotatekey;
    private char sharpDownkey;//to be added;
    private int sleepTime;
    private int changeSleepTime;
    private int level;
    private int levelSleep;
    private boolean threadStop;
    private final int TURN_LEFT = 1;
    private final int TURN_RIGHT = 2;
    private final int ROTATE = 3;
    private final int SHARPDOWN=4;
    private Controllor control;

    public Game(){
    super();
    terris = new Terris();
    message = new Message();
    control = new Controllor();
    sleepTime = 500;
    changeSleepTime =100;
    level = 0;
    levelSleep = 20;

    threadStop = false;
    // getContentPane().setLayout(new GridLayout(1,2));
    getContentPane().add(terris);
    // getContentPane().add(control);//加了这个就不能相应键盘了

    setLeftkey('a');
    setRightkey('d');
    setRotatekey('s');

    // terris.setColumns(10);
    // terris.setRows(20);
    addKeyListener(this);
    addWindowListener(this);
    setSize(new Dimension(600,600));
    show();
    }

    public void run(){
    int i;
    Random random = new Random();
    while(!terris.failed() && !threadStop){
    i = random.nextInt(7)+1;
    terris.setCurrentShap(getShap(i));
    while(!terris.hasDone())
    {
    if(!message.isEmpty())
    {
    switch(message.getMessage())
    {
    case TURN_LEFT:
    terris.left();
    break;
    case TURN_RIGHT:
    terris.right();
    break;
    case ROTATE:
    terris.rotate();
    break;
    case SHARPDOWN:
    terris.sharpDown();
    break;
    }
    try {
    Thread.sleep(changeSleepTime);
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }
    else
    {
         terris.down();
    try {
    Thread.sleep(sleepTime-level*levelSleep);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    }
      

  3.   

    public Shap getShap(int i)
    {
    return ShapFactory.createShap(i);
    }
    /* public static void main(String[] args) {
    Game test = new Game();
    test.setSize(new Dimension(200,200));
    test.show();
    Thread desk = new Thread(test);
    desk.start();
    }*/

    private void setLeftkey(char leftkey) {
    this.leftkey = leftkey;
    }
    private void setRightkey(char rightkey) {
    this.rightkey = rightkey;
    }
    private void setRotatekey(char rotatekey) {
    this.rotatekey = rotatekey;
    }
    private void setSharpDownkey(char sharpDownkey) {
    this.sharpDownkey = sharpDownkey;
    } /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    public void keyPressed(KeyEvent e) {
    if(e.getKeyChar() == leftkey)
    {
    message.addMessage(TURN_LEFT);
    return;
    }
    if(e.getKeyChar() == rightkey)
    {
    message.addMessage(TURN_RIGHT);
    return;
    }
    if(e.getKeyChar() == rotatekey)
    {
    message.addMessage(ROTATE);
    return;
    }
    if(e.getKeyChar() == sharpDownkey)
    {
    message.addMessage(SHARPDOWN);
    return;
    }
    // TODO Auto-generated method stub

    } /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    } /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
     */
    public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
     */
    public void windowClosed(WindowEvent e) {
    threadStop = true;
    System.exit(0);
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
     */
    public void windowClosing(WindowEvent e) {
    threadStop = true;
    System.exit(0);
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
     */
    public void windowDeactivated(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
     */
    public void windowDeiconified(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
     */
    public void windowIconified(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    /* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
     */
    public void windowOpened(WindowEvent e) {
    // TODO Auto-generated method stub

    }
    }
      

  4.   

    这个就是我说的jp
    package test;import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;class Controllor extends JPanel
    {
    Controllor()
    {
    super();
    setLayout(new GridLayout(4,2));
    add(new JButton("start"));
    add(new JButton("stop"));
    add(new JButton("restart"));
    add(new JButton("wakeup"));
    add(new JLabel("scroe"));
    add(new JTextField("0",5));
    add(new JLabel("level"));
    add(new JTextField("1",2));
    setSize(150,300);
    }
    }
    郁闷啊,不能连续3次回复,又注册了一个用户名,唉!