Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 APPLETS!-- And ARRAYS!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shivu




PostPosted: Sun May 11, 2008 8:54 pm   Post subject: APPLETS!-- And ARRAYS!

hey!!

i need help with 2D arrays. not particularly applets because our teacher wrote the code for appelts for us since we didn't learn them yet. So, a brief intro of what i've to do: there is a 2D array of type Color. so, when you press a certain F key (function key on the keyboard) different patterns of colours come up on the screen= in other words on the applet window...

so, my problem: when you press F5, the "columns " of the colours in the array are supposed to move one to the left. (the array is 4X4)

I don't know how to keep the colours stable, meaning that everytime i press F5, the colours of the array change. I don't know how to "store " those colours in some way so that whenever you press F5 those same colours in the columns will move one to the left.

here's my code- (it's only the part where i wrote my stuff about the shifting... and other stuff :S)

code:

 public void shiftLeft ()
      {
      // Step 5:  Cause each column on the grid to shift left.
      // The first (left most) column will shift to the last (right most) column
         int color1 = (int)(Math.random()*255);
         int color2 = (int)(Math.random()*255);
         int color3 = (int)(Math.random()*255);
     
         Color temp1= Color.white;
     
         for (int i =0; i<MAXROWS; i++) {
         //     int color1 = (int)(Math.random()*255);
         //         int color2 = (int)(Math.random()*255);
         //         int color3 = (int)(Math.random()*255);
            for (int j=0; j<MAXCOLS; j++){
               board [j][i]= new Color (color1, color2, color3);
            }
         }
         temp1 = board [0][0];
         for (int i=0; i<MAXROWS; i++){
            for (int j=0; j<MAXCOLS; j++){
               if (j==MAXCOLS-1) {
                  board [i][j] = temp1;
               }
               else if (j+1<MAXCOLS){      
                  Color temp2=board [i][j+1];
                  board [i][j]= temp2;
               }
            }
         }
         for (int i=0; i<MAXROWS; i++){
            for (int j=0; j<MAXCOLS; j++){
               System.out.println (board[i][j]);
            }
         }
      }// end of public void...



I'd really appreciate it if someone could help me with this!! thanks!!
Sponsor
Sponsor
Sponsor
sponsor
syntax_error




PostPosted: Sun May 11, 2008 9:12 pm   Post subject: Re: APPLETS!-- And ARRAYS!

Java:


    flag = new Color ((int) (Math.random () * 255), (int) (Math.random () * 255), (int) (Math.random () * 255));
    for (int i =0; i<MAXROWS; i++){
      for (int k = 0; k < MAXCOLS;  k++) {
        board [i][k] = flag;//sets each box with a random colour
      }
    }
 


the above is a cleaner way you store the get the colours in the first place

as to your quesiton, you are not storeing the colours after you did so the first that after that you are playing with the indice of that array to get the outputs that your teacher has asked for
HeavenAgain




PostPosted: Sun May 11, 2008 9:17 pm   Post subject: RE:APPLETS!-- And ARRAYS!

code:
         for (int i =0; i<MAXROWS; i++) {
         //    int color1 = (int)(Math.random()*255);
         //       int color2 = (int)(Math.random()*255);
         //       int color3 = (int)(Math.random()*255);
            for (int j=0; j<MAXCOLS; j++){
               board [j][i]= new Color (color1, color2, color3);
            }
         }
what you did here, is giving the board a whole set of new random colors (if you uncomment that part)

and so your color changes, and become so called not "stable"

just a side note, the code you have there will always give you 1 color for your whole table? no?
Shivu




PostPosted: Sun May 11, 2008 9:19 pm   Post subject: RE:APPLETS!-- And ARRAYS!

yea, so i don't know hwo to "store " those colours once they are assigned to the array, so that i can shift the coloumn left everytime F5 is pressed
HeavenAgain




PostPosted: Sun May 11, 2008 9:21 pm   Post subject: RE:APPLETS!-- And ARRAYS!

when you call to your class, first thing you access is the constructor, and you can do all your initializing there, and so you set your color there, and about that F5, you know event handling? here is a tut
Shivu




PostPosted: Sun May 11, 2008 9:23 pm   Post subject: RE:APPLETS!-- And ARRAYS!

um, no. we're just supposed to deal with the 2D array stuff... not with the other applet or interactive stuff, cuz our teacher did that for us.

abotu the initializing in the "constructor"?, um, wouldn't the array intilaze everytime u call it then? so, the values wouldn't be fixed anyway??
HeavenAgain




PostPosted: Sun May 11, 2008 9:27 pm   Post subject: RE:APPLETS!-- And ARRAYS!

sorry can you clarify if you press F5 meaning to refresh the webpage, or F5 as in inside your applet, that makes a difference...
Shivu




PostPosted: Sun May 11, 2008 9:28 pm   Post subject: RE:APPLETS!-- And ARRAYS!

F5 meaning inside the applet... sry
Sponsor
Sponsor
Sponsor
sponsor
syntax_error




PostPosted: Sun May 11, 2008 11:36 pm   Post subject: Re: APPLETS!-- And ARRAYS!

Java:

  public void shiftLeft (){
    for (int i = 0; i < MAXROWS; i++){
      flag = board[i][0]; //stores into temp var
      for (int k = 0; k < MAXCOLS-1 ; k++){
        board[i][k] = board[i][k+1];//moves colours inside the rows to the left
      }
      board [i][MAXCOLS-1] = flag;//puts the last colour with the first
    }
  }

is that what you are trying to do?
Shivu




PostPosted: Mon May 12, 2008 7:27 pm   Post subject: RE:APPLETS!-- And ARRAYS!

yes! that is what i'm supposed to do! Very Happy... but my problem was to figure out how to keep it so, so that when ever you press F5, this method will generate random colours and the shift to the left hwenever u press F5 everytime...

yea- lol it's a bit confusing, but I got help and used a boolean to see if the other nethod had run first or not...

and that worked!, so thank you very much for your help Very Happy!!
Shivu




PostPosted: Mon May 12, 2008 7:46 pm   Post subject: RE:APPLETS!-- And ARRAYS!

hi again!

actually this problem is a small one. Very Happy (i think)...

i was just wondering whether this code is an efficient and the "proper" way to write it and to perform the activity.

i have to make a cross across the screen... - this is part on my assignment explained before....

in this step, i have to make a cross, so like 2 diagonals across the board...

i'd really appreciate it if someone could check if this is a good way to write it... since i don't seem to find any other way... thanks!!

code:

 public void drawCross ()
      {
      // Step 4:  fill appropriate squares with same colour to form a cross on the grid
     
         int color1 = (int)(Math.random()*255);
         int color2 = (int)(Math.random()*255);
         int color3 = (int)(Math.random()*255);
                            
         int k = MAXCOLS-1;   
     
         for (int i=0; i<MAXROWS; i++){
         
            for (int j=i; j<=i; j++){
           
               board[i][j]= new Color (color1, color2,color3);
               System.out.println (board[i][j]);
           
            }   
                
            for (int j=k; j==k; j--){
           
               board[i][j]= new Color (color1, color2, color3);
               System.out.println (board[i][j]);
           
            }
                               
            k = k-1;
         
         }
     
                                
      }// end of public void...


HeavenAgain




PostPosted: Mon May 12, 2008 7:57 pm   Post subject: RE:APPLETS!-- And ARRAYS!

one for loop is all you need actually, start from one side (ie. top left corner, and bottom left corner)

go by columns. as you increase the column, think about what happens to those 2 corners (hint, they should move to their opposite corner, ie. left bottom corner to right top corner)
Shivu




PostPosted: Mon May 12, 2008 9:43 pm   Post subject: RE:APPLETS!-- And ARRAYS!

hey!! thanks for the help! Very Happy...

wow, i didn't know you could use only one loop!
syntax_error




PostPosted: Mon May 12, 2008 10:48 pm   Post subject: Re: RE:APPLETS!-- And ARRAYS!

Shivu @ Mon May 12, 2008 7:27 pm wrote:


yea- lol it's a bit confusing, but I got help and used a boolean to see if the other nethod had run first or not...


Me personally, I have done that assignment [last semester] and there really is no need for boolean at all. Because you do not need to check of the other method is run or not, that is utterly un-needed, let me explain you first have all the squares set to a colour right? and depend on the order of the 'pressing' of the F keys the pattern changes with the F key, so you do not need to check if the one before has been dont at all.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: