Results 1 to 3 of 3

Thread: Static fields

  1. #1
    riverhare is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Static fields

    Hi guys!
    My task is to develop the game. I've got an array of cells, where I should move my characters during the game.
    Every Cell have got information (of integer type) about character containing there:
    Java Code:
     class Cell {
      public int x,y;
      public static int FirstX,FirstY;
      public int WhoIsThere;
    Then in class GameLogic I have this code:
    Java Code:
    class GameLogic{
         static Cell[][] CellMassive=new Cell[7][7];
         public static void FillItForTheFirstTime() {for (int countI=0; countI<7; countI++)
         {for (int countJ=0; countJ<7;countJ++){
               if (((countI==0)||(countI==1)||(countI==5)||(countI==6))&&
                     ((countJ==0)||(countJ==1)||(countJ==5)||(countJ==6)))
             {CellMassive[countI][countJ].WhoIsThere=3;}
             else {CellMassive[2][2].WhoIsThere=1;CellMassive[2][4].WhoIsThere=1;
             if (countI>2){CellMassive[countI][countJ].WhoIsThere=0;}}
                }
            }}
         }
    And when I'm trying to run through this array to paint all of my cells...
    Java Code:
      public void paint(Graphics g){ 
            GameLogic.FillItForTheFirstTime(); 
            for (int countI=0; countI<7; countI++){
         for (int countJ=0;countJ<7;countJ++){        
            g = getGraphics(); 
            g.setColor(currColor);
            if (GameLogic.CellMassive[countI][countJ].WhoIsThere==2){
            g.fillRect(FirstX+countI*34, FirstY+countJ*34, Cell.Height, Cell.Height);}}    
        }
    }
    ...I can see only the beautiful green frame. I think, there is something wrong with "WhoIsThere" field, but I don't know, what :(

  2. #2
    riverhare is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: Static fields

    ok, don't mess, I've understood my mistakes :) Later I'll be more accurate with arrays and references

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Static fields

    In case you didn't know, you should override paintComponent, not paint. And you're calling GameLogic.FillItForTheFirstTime() every time your component repaints.

    BTW, you should look over the link in my sig. Capitalizing identifiers the way you did here can make your code confusing to experienced Java programmers.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Can non-static Inner Class have static fields ?
    By fatabass in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 09:47 PM
  2. Replies: 4
    Last Post: 07-19-2011, 01:30 PM
  3. Replies: 3
    Last Post: 03-24-2010, 12:24 AM
  4. Static fields
    By Eranga in forum Advanced Java
    Replies: 25
    Last Post: 08-29-2009, 11:06 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •