⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 panel2.java

📁 这是使用swing制作的俄罗斯方块
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				}
			    }
			    else
			    {
				map[i] = new int[]{o,o,o,o,o,o,o,o,o,o};//airline;
			    }
			    fireTableChange(PAUSE);
			}
			catch (InterruptedException ex)
			{
			}
		    }
		    fireTableChange(DEAD);
		}
	    }.start();
	    return DEAD;//|move(MOVE_NONE);
	}
	void fireTableChange(int code){
	    Object[] obj = this.listenerList.getListenerList();
	    for(int i = 0;i<obj.length;i++){
		if(obj[i] instanceof TableModelListener){
		    TableModelListener l = (TableModelListener) obj[i];
		    l.tableChanged(new TableModelEvent(this,0,0,0,code));
		}
	    }
	}
	public int move(int direction){
	    synchronized(this){
		int ret = s.move(direction, this.map);
		if ( (ret & PUNCH_GROUND) != 0 && (ret & DEAD) == 0)
		{
		    ret |= bomb();
		}
		this.fireTableChange(ret);
		return ret;
	    }
	}
	public int turn(int direction){
	    synchronized(this){
		int ret = s.turn(direction, this.map);
		this.fireTableChange(ret);
		return ret;
	    }
	}
	/**
	 * 消层
	 */
	int bomb(){
		return flash(statitic(this.map),this.map);
	}
	/**
	 * 动画线程
	 */
	Thread flash = null;
	int flash(Point[] p,int[][] map){
	    if(p.length==0){
		int ret = addShape(new Square());
		return ret;
	    }
	    layer += p.length/map[0].length;
	    flash = new MyThread(p,map);
	    flash.start();
	    return PAUSE;
	}
	class MyThread extends Thread{
	    Point[] p;
	    int[][] map;
	    public MyThread(Point[] p,int[][] map){
		this.p = p;
		this.map = map;
	    }
	    public void run(){
		    for(int k = 0;k<5;k++){
			try
			{
			    sleep(200);
			}
			catch (InterruptedException ex)
			{
			}
			for (int i = 0; i < p.length; i++)
			{
			    try{
				map[p[i].y][p[i].x] = k%2==0?BLOW:BLOCK;
			    }catch(ArrayIndexOutOfBoundsException e){
				e.printStackTrace();
			    }
			}
			fireTableChange(PAUSE);
		    }
		    blowSquare(p,map);
		    fireTableChange(0);
		    int ret = addShape(new Square());
		    fireTableChange(ret);
		    flash = null;
	    }

	}

	/**
	 * 检查要处理的点
	 * @return Point[]
	 */
	Point[] statitic(int[][] map){
	    ArrayList point = new ArrayList();
	    for(int i = 0;i<map.length;i++){
		boolean isAllBLOCK = true;
		for(int j = 0;j<map[i].length;j++){
		    if(map[i][j]!= BLOCK){
			isAllBLOCK = false;
			break;
		    }
		}
		if(isAllBLOCK){
		    for(int j = 0;j<map[i].length;j++){
			point.add(new Point(j,i));
		    }
		}
	    }
	    return (Point[]) point.toArray(new Point[point.size()]);
	}
	final int o = AIR;
	final int l = BLOCK;
	final Shape overlogo = new Shape(){
	    Point p = new Point(0,5);
	    int[][] m = {
		{o,o,l,l,l,l,l,o,o,o},
		{o,l,o,o,o,o,o,l,o,o},
		{o,l,o,o,o,o,o,o,o,o},
		{o,l,o,o,o,o,l,l,o,o},
		{o,l,o,o,o,o,o,l,o,o},
		{o,o,l,l,l,l,l,o,o,o}
	    };
	    public int[][] getMatrix(){return m;}
	    public void setLocation(Point p){this.p = p;}
	    public Point getLocation(){return p;}
	    public int turn(int direction, int[][] map){return 0;}
	    public int move(int direction, int[][] map){return 0;}
	};

	/**
	 * reset
	 */
	public void reset()
	{
	    layer = 0;
	    map = new int[map.length][map[0].length];
	    addShape(new Square());
	    this.fireTableChange(0);
	}
    }

    void jbInit() throws Exception
    {
	this.jTable1.getColumnModel().setColumnMargin(0);
	this.jTable1.setRowMargin(0);
	//this.jTable2.getColumnModel().getColumn(0).setPreferredWidth(30);
	this.setLayout(borderLayout1);
	jPanel1.setLayout(verticalFlowLayout1);
	jButton1.setText("IN_THE_AIR");
	jButton2.setText("PUNCH_WALL");
	jButton3.setText("PUNCH_GROUND");
	jButton4.setText("BOMB");
	jButton5.setText("DEAD");
	jButton6.setText("SHAPE_AGAINST");
	jButton7.setText("PAUSE");
	jButton1.setFocusable(false);
	jButton2.setFocusable(false);
	jButton3.setFocusable(false);
	jButton4.setFocusable(false);
	jButton5.setFocusable(false);
	jButton6.setFocusable(false);
	jButton7.setFocusable(false);
	jToggleButton1.setFocusable(false);
	jToggleButton2.setFocusable(false);
	jToggleButton3.setFocusable(false);
	jToggleButton4.setFocusable(false);
	jToggleButton5.setFocusable(false);
	jToggleButton6.setFocusable(false);
	jToggleButton7.setFocusable(false);
	jToggleButton8.setFocusable(false);
	jToggleButton9.setFocusable(false);
	jPanel2.setLayout(gridLayout1);
	gridLayout1.setColumns(3);
	gridLayout1.setRows(3);
	jToggleButton1.setText("");
	jToggleButton2.setText("↑");
	jToggleButton3.setText("");
	jToggleButton4.setText("←");
	jToggleButton5.setText("");
	jToggleButton6.setText("→");
	jToggleButton7.setText("");
	jToggleButton8.setText("↓");
	jToggleButton9.setText("");
	jPanel3.setLayout(borderLayout2);
	jPanel1.setPreferredSize(new Dimension(200, 0));
	jToggleButton10.setText("SPACE BAR");
	jPanel4.setLayout(gridBagLayout1);
	jLabel1.setFont(new java.awt.Font("Dialog", 1, 20));
	jLabel1.setText("LAYER:  0");
	this.add(jScrollPane1,  BorderLayout.CENTER);
	jScrollPane1.getViewport().add(jTable1, null);
	this.add(jPanel1,  BorderLayout.EAST);
	jPanel1.add(jButton1, null);
	jPanel1.add(jButton2, null);
	jPanel1.add(jButton3, null);
	jPanel1.add(jButton4, null);
	jPanel1.add(jButton5, null);
	jPanel1.add(jButton6, null);
	jPanel1.add(jButton7, null);
	jPanel2.add(jToggleButton1, null);
	jPanel2.add(jToggleButton2, null);
	jPanel2.add(jToggleButton3, null);
	jPanel2.add(jToggleButton4, null);
	jPanel2.add(jToggleButton5, null);
	jPanel2.add(jToggleButton6, null);
	jPanel2.add(jToggleButton7, null);
	jPanel2.add(jToggleButton8, null);
	jPanel2.add(jToggleButton9, null);
	jPanel1.add(jPanel4, null);
	jPanel4.add(jPanel2,  new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 61, 0));
	jPanel4.add(jToggleButton10,   new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
	jPanel1.add(jPanel3, null);
	jPanel3.add(jTable2, BorderLayout.CENTER);
	jPanel1.add(jLabel1, null);

    }

    public static void main(String[] args) {

	JFrame f = new JFrame();
	f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
	f.setSize(500,650);
	f.getContentPane().add(new Panel2(),BorderLayout.CENTER);
	f.show();
    }

    public static void println(int[][] m){
	for (int i = 0; i < m.length; i++) {
     for (int j = 0; j < m[i].length; j++) {
         System.out.print(m[i][j]+",");
     }
     System.out.println();
 }
 System.out.println("---");
    }




    public static int[][] turnLeft(int[][] m){
	int x = m.length;
	int y = m[0].length;
	int[][] n = new int[y][x];
	for(int i = 0;i<y;i++){
	    for (int j = 0; j < x; j++)
	    {
		n[i][j]=m[j][y-1-i];
	    }
	}
	return n;
    }
    public static int[][] turnRight(int[][] m){
	int x = m.length;
	int y = m[0].length;
	int[][] n = new int[y][x];
	for(int i = 0;i<y;i++){
	    for (int j = 0; j < x; j++)
	    {
		n[i][j]=m[x-1-j][i];
	    }
	}
	return n;
    }

    public static void print(int[][] m){
	for(int i = 0;i<m.length;i++){
	    for(int j = 0;j<m[i].length;j++){
		System.out.print(m[i][j]+" ");
	    }
	    System.out.println();
	}
    }

    void reflectToButton(int result){
	this.jButton1.setSelected((result&IN_THE_AIR)!=0);
	this.jButton2.setSelected((result&PUNCH_WALL)!=0);
	this.jButton3.setSelected((result&PUNCH_GROUND)!=0);
	this.jButton4.setSelected((result&BOMB)!=0);
	this.jButton5.setSelected((result&DEAD)!=0);
	this.jButton6.setSelected((result&SHAPE_AGAINST)!=0);
	this.jButton7.setSelected((result&PAUSE)!=0);
    }

    class Square implements Shape{
	int[][] m = createNewShape();
	Point p;
	/**
	 * getMatrix
	 *
	 * @return int[][]
	 */
	public int[][] getMatrix()
	{
	    return m;
	}

	/**
	 * getLocation
	 *
	 * @return Point
	 */
	public Point getLocation()
	{
	    return p==null?null:new Point(p.x,p.y);
	}

	/**
	 * setLocation
	 *
	 * @param p Point
	 */
	public void setLocation(Point p)
	{
	    if(p==null){
		this.p=null;
	    }
	    if(this.p==null){
		this.p = new Point(p.x,p.y);
	    }else{
		this.p.x = p.x;
		this.p.y = p.y;
	    }
	}


	/**
	 * turn
	 *
	 * @param direction int
	 * @param map int[][]
	 * @return int
	 */
	public int turn(int direction, int[][] map)
	{
	    int result = 0;
	    deleteShape(map,m,p);
	    int[][] turnm = null;
	    int[][] oldm = m;
	    for(int i = 0;i<4;i++){
		result = 0;
		switch (direction){
		    case TURN_LEFT:
			turnm = turnLeft(m);
			break;
		    case TURN_RIGHT:
			turnm = turnRight(m);
			break;
		}
		    this.m = turnm;
		if(isOverlap(map,turnm,p)){
		    result |= SHAPE_AGAINST;
		}else{
		    break;
		}
	    }
	    if((result&SHAPE_AGAINST)==0){
		addShape(map, turnm, p);
		this.m = turnm;
	    }else{
		this.m = oldm;
	    }
	    return result|IN_THE_AIR;
	}


	/**
	 * move
	 *
	 * @param direction int
	 * @param map int[][]
	 * @return int
	 */
	public int move(int direction, int[][] map)
	{
	    Point p = this.getLocation();
	    deleteShape(map, m, p);
	    int result = 0;
	    switch(direction){
		case MOVE_RIGHT:
		    p = new Point(p.x+1,p.y);
		    break;
		case MOVE_LEFT:
		    p = new Point(p.x-1,p.y);
		    break;
		case MOVE_DOWN:
		    p = new Point(p.x,p.y+1);
		    break;
		default:
	    }
	    if(isOverlap(map,m,p)){
		p = this.getLocation();
		addShape(map,m,p);
		result |= PUNCH_WALL;
		if(direction==MOVE_DOWN){
		    result |= PUNCH_GROUND;
		}
	    }else{
		this.p = p;
		addShape(map,m,p);
		result |= IN_THE_AIR;
	    }

	    return result;
	}



    }
    public static int[][] deleteShape(int[][] map,int[][] m,Point p){
	for(int i = 0;i<m.length;i++){
	    for(int j = 0;j<m[i].length;j++){
		if(m[i][j]==BLOCK){
		    try{
			map[i+p.y][j+p.x]=AIR;
		    }catch(ArrayIndexOutOfBoundsException e){
			System.out.println("i = "+i);
			System.out.println("j = "+j);
			e.printStackTrace();
		    }
		}
	    }
	}
	return map;
    }
    public static int[][] addShape(int[][] map,int[][] m,Point p){
	for(int i = 0;i<m.length;i++){
	    for(int j = 0;j<m[i].length;j++){
		if(m[i][j]==BLOCK){
		    try{
			map[i+p.y][j+p.x]=BLOCK;
		    }catch(ArrayIndexOutOfBoundsException e){
			System.out.println("i = "+i);
			System.out.println("j = "+j);
			e.printStackTrace();
		    }
		}
	    }
	}
	return map;
    }


    /**
     * 判断是否重叠?
     * @param map int[][]
     * @param m int[][]
     * @param p Point
     * @return boolean
     */
    public static boolean isOverlap(int[][] map,int[][] m,Point p){
	for(int i = 0;i<m.length;i++){
	    for(int j = 0;j<m[i].length;j++){
		if(m[i][j]==BLOCK){
		    int map_v = AIR;
		    try{
			if(i+p.y>=map.length&&j+p.x>=map[0].length){
			    return true;
			}
			map_v = map[i+p.y][j+p.x];
		    }catch(ArrayIndexOutOfBoundsException e){
			map_v = BLOCK;
//			System.out.println("i = "+i);
//			System.out.println("j = "+j);
//			e.printStackTrace();
			return true;
		    }
		    if(map_v==BLOCK){
			return true;
		    };
		}
	    }
	}
	return false;
    }

    public static void blowSquare(Point[] p ,int[][] map){
	for( int i = 0;i<map[map.length-1].length;i++){
	    for(int j = 0;j<map.length;j++){
		if(map[j][i]==BLOW){
		    for(int k = j;k>0;k--){
			map[k][i] = map[k-1][i];
		    }
		    map[0][i] = BLOW;
		}
	    }


	    //将BLOW排掉
	    for (int j = 0;j<map.length;j++){
		if(map[j][i]==BLOW){
		    map[j][i] = AIR;
		}else{
		    break;
		}
	    }
	}

    }



    /**
     * get a new shape
     * @return int[][]
     */
    public static int[][] createNewShape(){
	int i = (int)(r.nextDouble()*7);
	switch(i){
	    case 0:
		return new int[][]{{1,1},{1,1}};
	    case 1:
		return new int[][]{{1,1,1},{1,0,0}};
	    case 2:
		return new int[][]{{1,1,1},{0,0,1}};
	    case 3:
		return new int[][]{{1,1,0},{0,1,1}};
	    case 4:
		return new int[][]{{0,1,1},{1,1,0}};
	    case 5:
		return new int[][]{{1,1,1},{0,1,0}};
	    case 6:
	    default:
		return new int[][]{{1,1,1,1}};
	}
    }


}

⌨️ 快捷键说明

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