#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Changing the attribute of an instance object of a class that's declared in another pa


    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:

    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:
    Code:
    synchronized void rotateClockwise() {
        currentPiece.shape = multiplyMatrix(shape);
    
        //gives me an error
        updateLocation();
    }
    the constructor (all three methods are in the same class):
    Code:
        public Piece(int shape[][]) {
        super(shape);
        currentX = 7;
        currentY = 2;
        updateLocation();
    }
    this method is in another class and it contains the instance object whose attribute i want to modify:

    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();
        }
    }
    createPiece method (I want to access the shape attribute):
    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);
        }
    }
    entire code can be found here (without certain modifications):

    http://mathcs.slu.edu/~fritts/cse131/labs/lab9/index.html

    Update:

    I found out that super calls this constructor in Grid:

    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);
    }
    Now, I tried this:

    Code:
    synchronized void rotateClockwise() {
    Grid.contents = multiplyMatrix(Grid.contents);
    updateLocation();
    
    }
    It gives me:

    non-static method getContents() cannot be referenced from a static context
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/23845-changing-attribute-instance-object-class-thats-declared-another-package-whose-variable-constructed-its-parent-class.html
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Where is getContents() found?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    I can't even make the method required for that. It won't compile when I do.

IMN logo majestic logo threadwatch logo seochat tools logo