Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 14th, 2013, 05:36 PM
wholegrain wholegrain is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 wholegrain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 54 sec
Reputation 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

Reply With Quote
  #2  
Old February 15th, 2013, 06:13 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,048 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 53 m 33 sec
Reputation Power: 346
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

Reply With Quote
  #3  
Old February 15th, 2013, 09:25 AM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,828 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 11 h 21 m 19 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
Where is getContents() found?

Reply With Quote
  #4  
Old February 15th, 2013, 03:06 PM
wholegrain wholegrain is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 wholegrain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 54 sec
Reputation Power: 0
I can't even make the method required for that. It won't compile when I do.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Changing the attribute of an instance object of a class that's declared in another pa

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap