Ok, so I have a bunch of classes and I'd like to rotate an object which has an object shape, which is a 2d array and that's constructed in the parent class of the object class. I want to change the attribute (shape) in the method rotate() which calls the method multiplyarray that does the rotation by changing the shape. However, I realized that I don't have access to shape, or at least I don't know how to change it. I don't think the parent class has a public setShape method. Anyway here's the code:
the constructor (all three methods are in the same class):HTML Code:public static int [][] multiplyMatrix(int [][] m1) { int [][] m2 = {{0,0,0,1}, {0,0,1,0}, {0,1,0,0}, {1,0,0,0}, }; int[][] result = new int[4][4]; // multiply for (int i=0; i<4; i++) for (int j=0; j<4; j++) for (int k=0; k<4; k++) if (m1[i][k] * m2[k][j] > 0) { result[i][j] = 1; } else { result[i][j] = 0; } return result; } } the rotate method: synchronized void rotateClockwise() { currentPiece.shape = multiplyMatrix(shape); //gives me an error updateLocation(); }
this method is in another class and it contains the instance object whose attribute i want to modify:HTML Code:public Piece(int shape[][]) { super(shape); currentX = 7; currentY = 2; updateLocation(); }
createPiece method (I want to access the shape attribute):HTML Code: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 found out that super calls this constructor in Grid:HTML Code:public static Piece createPiece() { int[][] s = SHAPES[(int) (Math.random()*SHAPES.length)]; switch ((int) (Math.random()*10)) { case 0: case 1: case 2: case 3: default: return new Piece(s); }
Now, I tried this:HTML Code:public Grid(int[][] contents) { this.contents = contents; Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE, getRows()*Tetris.SQUARE_SIZE); setSize(d); setPreferredSize(d); setOpaque(false); }
It gives me:HTML Code:synchronized void rotateClockwise() { Grid.contents = multiplyMatrix(Grid.contents); updateLocation(); }
non-static method getContents() cannot be referenced from a static context
entire code can be found here (without certain modifications):
CSE131 Lab 9: Tetris Game using Object-Oriented Design