Tuesday, 15 January 2013

java - Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class -



java - Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class -

ok, have bunch of classes , i'd rotate object has object shape, 2d array , that's constructed in parent class of object class. want alter attribute (shape) in method rotate() calls method multiplyarray rotation changing shape. however, realized don't have access shape, or @ to the lowest degree don't know how alter it. don't think parent class has public setshape method. anyway here's 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 (int = 0; < 4; i++) (int j = 0; j < 4; j++) (int k = 0; k < 4; k++) if (m1[i][k] * m2[k][j] > 0) { result[i][j] = 1; } else { result[i][j] = 0; } homecoming result; }

the rotate method:

synchronized void rotateclockwise() { currentpiece.shape = multiplymatrix(shape); //gives me error updatelocation(); }

the constructor (all 3 methods in same class):

public piece(int shape[][]) { super(shape); currentx = 7; currenty = 2; updatelocation(); }

this method in class , contains instance object attribute want modify:

public void keypressed(keyevent event) { int key = event.getkeycode(); switch (key) { case keyevent.vk_up: // arrow case keyevent.vk_kp_up: currentpiece.rotatecounterclockwise(); break; case keyevent.vk_down: // downwards 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 access shape attribute):

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: homecoming new piece(s); } }

entire code can found here (without modifications):

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

update:

i found out super calls constructor in grid:

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, tried this:

synchronized void rotateclockwise() { grid.contents = multiplymatrix(grid.contents); updatelocation(); }

it gives me:

non-static method getcontents() cannot referenced static context

you haven't shown code of superclass, 1 approach might create mirror of shape field in subclass, override getter , add together setter; e.g.

public class super { private shape shape; public super(shape shape) { this.shape = shape; } public shape getshape() { homecoming this.shape; } } public class sub extends super { private shape myshape; public sub(shape shape) { super(shape); this.myshape = shape; } @override public shape getshape() { homecoming this.myshape; } public void setshape(shape shape) { this.myshape = shape; } }

this approach work if super designed allow this. in particular, super must utilize getshape() fetch shape value.

(there horrible workarounds if super not designed back upwards extension here, i'm sure teacher not expecting utilize them here ....)

java swing inheritance 2d

No comments:

Post a Comment