Rotating objects in a grid (GUI, JAVA SWING)

醉酒当歌 提交于 2019-12-02 19:35:17

问题


Ok, so I am trying to do this tutorial: http://mathcs.slu.edu/~fritts/cse131/labs/lab9/index.html

But I don't know how to rotate objects

synchronized void moveDown() {

}

Is there any defined method for that or do I have to implement my own code? I thought about changing the shape of the object, but that would imply I have to change my current object every time, which might be a little complicated to implement.

The method that calls moveDown:

public void keyPressed(KeyEvent event) {
        int key = event.getKeyCode();
        switch (key) {
        case KeyEvent.VK_UP:  // up arrow
        case KeyEvent.VK_KP_UP:
            currentPiece.rotateCounterclockwise();
            break;
        case KeyEvent.VK_DOWN:  // down arrow
        case KeyEvent.VK_KP_DOWN:
            currentPiece.rotateClockwise();
            break;
        case KeyEvent.VK_LEFT:  // left arrow
        case KeyEvent.VK_KP_LEFT:
            currentPiece.moveLeft();
            break;
        case KeyEvent.VK_RIGHT:  // right arrow
        case KeyEvent.VK_KP_RIGHT:
            currentPiece.moveRight();
            break;
        case KeyEvent.VK_SPACE:  //  space bar
            currentPiece.drop();
        }
    }

I was about to add L1a, L1b, L1c, etc. Isn't there any other way?

    public static final int[][] L1 =
    {{1,1},
     {0,1},
     {0,1}
    };

public static final int[][] L2 =
    {{0,1},
     {0,1},
     {1,1}
    };      

public static final int[][] T =
    {{0,1},
     {1,1},
     {0,1}
    };

public static final int[][] BOX =
    {{1,1},
     {1,1}
    };

public static final int[][] BAR =
    {{1,1,1,1}
    };      

public static final int[][] STEP1 =
    {{1,0},
     {1,1},
     {0,1}
    };  

public static final int[][] STEP2 =
    {{0,1},
     {1,1},
     {1,0}
    };

来源:https://stackoverflow.com/questions/14867570/rotating-objects-in-a-grid-gui-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!