Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
import java.awt.*;
import hsa.Console;
public class Game{
    static Console c;
    public static void Wait (int time){
        try{
            Thread.sleep (time);
        }
        catch (InterruptedException e){
        }
    }
    public static class Tile{
        public int x,y,stack;
        public Tile(){
            x = 0;
            y = 0;
            stack = 0;
        }
        public Tile(int xco, int yco, int stacknum){
            x = xco;
            y = yco;
            stack = stacknum;
        }
        public void draw(Tile tile){ //To draw the tile
            if(stack>0){
                c.setColor(Color.red);
                c.fillRect(x*78+1+x,y*78+1+y,78,78); //Calculate coordinates
            }
            else{
                c.setColor(Color.blue);
                c.fillRect(x*78+1+x,y*78+1+y,78,78);
            }
        }
    }
    public static void main (String[] args){
        c = new Console ();

        for(int i=0;i<640;i+=79) c.drawLine(i,0,i,474);
        for(int i=0;i<500;i+=79) c.drawLine(0,i,632,i);
        //8x6 tiling
        Tile[][] tile = new Tile[8][6];
        for(int i=0;i<8;i++){
            for(int j=0;j<6;j++){
                tile[i][j] = new Tile();
                tile[i][j].x = i;
                tile[i][j].y = j; //Set x and y coordinates
                tile[i][j].stack = 5;
            }
        }
        Tile.draw(tile[0][0]);
    }
}

Here I have a tile of 8x6 squares using a multidimensional array. I would think that the coordinates would correspond to the correct numbers, but for some reason the coordinates seem to copy the ones created before it. Could someone tell me why this is happening and how the code should be corrected? Btw I started java so I'm not completely used to object oriented programming :P

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Your co-ordinates are declared as static:

public static int x,y,stack;

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a static class variable, which is in one fixed location in memory.

You should however remove the static modifier to have specific value for each object of Tile

Edit: From your below comment and for your better understanding, to work with draw() funtion:

Approach 1: If we wish to have draw function to be static:

public static void draw(Tile tile){ //To draw the tile
            if(stack>0){
                c.setColor(Color.red);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
                   //Calculate coordinates
            }
            else{
                c.setColor(Color.blue);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
            }
        }

You can call this function by Tile.draw(tile); where tile is an instance of Tile

Approach 2: If draw(Title tile) function is non static: you don't need to pass the tile instance at all:

public  void draw(){ //To draw the tile
            if(stack>0){
                c.setColor(Color.red);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
                   //Calculate coordinates
            }
            else{
                c.setColor(Color.blue);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
            }
        }

Then create an instance Tile title = new Title() and call title.draw()

share|improve this answer
 
However, if I remove the "static" to make it "public int x,y,stack", it says the variables in the draw function "can't be access in this static context". –  user2734517 Nov 8 at 4:09
 
If I remove all the "static" from the class Tile (class Tile, draw function), "new Tile()", "Tile.draw" seem to give an error –  user2734517 Nov 8 at 4:10
 
yes beacuse your draw function itself is static and you can't access non static variable from static context. remove the static modifier from draw function too –  Sage Nov 8 at 4:12
 
Now I get Tile.draw as an error, saying "The method "draw" is not static, and cannot be accessed in this static context.". –  user2734517 Nov 8 at 4:15
 
as i have said in the a static member is accessible by class name Class.staticFunction() but non-static member is accessible by the instance hence non-static function can not be called by Class.nonStaticFunction() –  Sage Nov 8 at 4:20
show 4 more comments

You've marked x, y, z as static so all instances of your class use the same ones, just remove the static keyword

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.