aspose file tools*
The moose likes Beginning Java and the fly likes Tile constraints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tile constraints" Watch "Tile constraints" New topic
Author

Tile constraints

joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello everyone.

i have produced a simple grid of 4x4 squares (im starting simple and working up). Each tile in the grid has an identifier (tile1, tile2, tile3 etc). Based on a random number, one of the four corner tiles (tile1,tile4,tile13,tile16) gets a blue background.

The program then changes the adjacent tiles to a yellow background. This has been done in a very long winded approach using statements. For example, if tile1 is blue, i have had to type out code for each adjacent tile (ie tile2,tile5,tile6)to make them yellow. If i was to use a larger grid it would be ridiculous in my opinion, especially with the randomness and number of blue tiles.

I was wondering if anyone could guide me as i wish to know how to code my program so that when a button is pressed, each tile is checked one by one, and any tile that is adjacent to a (Color.blue) tile is changed to (Color.yellow). I believe this could be done in a few lines of code, and attributed to the whole grid, rather than having to type out code for each tile.

I was hoping for a written example, or what method to use i.e. what keywords should i look for in my java texts, google search or jdk online guides. i hope u understand my question. if needs be, i can post some code.

Thankyou,

Joseph.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

What type of data structure are you using to represent your grid? A two dimensional array is often visually represented as a table, like a spreadsheet. (Two dimensional arrays are sometimes referred to as "an array of arrrays".) As a tile is changed to a blue tile, keep track of it and its postion in the 2D Array (in an List for example). Then you can simply iterate through that list, and, if you use a two dimensional array to reprsent all tiles, it would be an easy matter to calculate the adjoining tiles for each blue tile. Try doing some research on two dimensional arrays.

This idea is just after some very quick thought on the matter. Someone else may be able to recommend something different.
[ February 20, 2005: Message edited by: Mark Vedder ]
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hi and thanks for reply.

my method for the tiles is very simple (im nearly ashamed to say what it is lol - but i am a beginner). i have used text areas and xY layout, with 20x20 size, so the color is via tile1.getBackground(Color.blue). i hope i can later give them images as backgrounds. The emphasis on this program is not on graphics (they dont matter at all - its to do with algorithms). im sure there is a way with statements like loops(?) to go through each tile one by one (i++?) and make the tile color affect other tile colors. i just dont know what i should be looking at exactly.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
A very simple method would be to iterate, or loop, across your grid. In English, you could do this as "Iterate over each row, and within each row, column-by-column." In Java this looks like the following.In your case, at each grid point (square) you want to examine the surrounding square colors to choose what to do with the current one. You have "row" and "col" as indices into your grid, so use them in expressions:As you can see from my comments, not all expressions will be valid across the board, so you'll need to use if tests to avoid accessing an invalid location.
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello.

thanks for your time and reply david, it looks interesting.
i understand most of how it works. i have some questions though - ill try to make it easy for you to answer them, even with a yes or no (so i can figure it out, and know im not going down a blind alley!).

From what i see, your code basically sets out a 20x20 grid, and uses a loop in that if the number of rows or colums is less than 20, it creates a new square until the maximum is reached. This is much faster than manually sizing and positioning dozens of textAreas.

when you stated:

"int square = grid[row][col]" - i am not 100% sure on this. I guess that this allocates an identifiable object to one of the 20x20 squares.
so i could use:

"int tile1 = grid[1][1];
int tile2 = grid[1][2]; etc.."

now that the "tiles" are identified, what dictates their size? my prefered size was 20x20each (using textarea it was (20, 20)). ***Is there a method to include the size of each tile?*** i guess it would maybe be dependant on the jPanel the grid is within.

As for the algorithm ***Do i place algorithm after all the blue tiles are created, and then apply it?***

again, thankyou foryour time and help. You have given me something to work on :-)

Joseph.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I assumed a bit too much and may have confused you further. Let me back up a step.

First, given your latest post I'm guessing that you have created several GUI objects (JTextAreas) to represent your tiles and board. Further, you aren't representing the board using a non-GUI data structure "behind the scenes," is that correct?

The code I posted is for working with a non-GUI data structure of a tiled board consisting of 20 times across by 20 tiles down. Each tile was a single "int" variable as an example. In a chess board, each tile's int variable would be 0 for empty or a constant representing the color and piece type (pawn, rook, bishop, etc).
Originally posted by joseph mcgratton:
your code basically sets out a 20x20 grid, and uses a loop in that if the number of rows or colums is less than 20, it creates a new square until the maximum is reached.
Close. The first three lines merely declare and create a two-dimensional array with 20 columns and 20 rows. Each element of the array is a single integer variable initialized to zero by the JVM automatically. It nas nothing to do with Swing GUI components.

The for loop iterates over the array, row by row, column by column in each row. At each (row,column) intersection, it simply access the integer variable. The comment was to say that you could do something with that value or set the value yourself. The for loop does not create the tiles; they were created by the "int [][] grid = ..." line above (note I was missing one of the []s in my original post). But again the tiles here are simply integer values.

The code could be modified to be used with what I assume (again, caution) you have in your code. Here's what I think you have:The tile names you used are probably different, but here I've named them using the row and column on which they appear to create a 4x4 grid.

So now you'd like to be able to set all tiles adjacent to a blue tile to yellow without code like the following:Am I on track here? You'd like to avoid typing all those if tests out, especially when you get to a 20x20 grid? I can't blame you! And you can avoid it.

Modify the code I posted to have each tile in the grid array be a JTextArea reference instead of an int.Now understand that this doesn't create JTextAreas itself. Instead, it creates a two-dimensional array of JTextArea references, each initialized to null.

You could also write code to fill the grid with JTextAreas in a loop, but that will be more complex, so let's leave it for the step after this one. Sticking to the not-so-nasty 4x4 case, you'd fill the grid array with your existing JTextArea references defined in your code above:Notice that array indices start at 0 instead of 1. If an array has 4 elements, the index can range from 0 to 3. Also, when you see "grid[r][c]", remember that this is a JTextArea reference, and you can assign it to a JTextArea variable or call JTextArea methods on it.

After this code runs, the grid now references all of your built JTextAreas. Now you can use the for loop code I showed you, except for each tile you will examine the surrounding tiles' colors. To do this you'll also need the last bit of code from my previous post to make sure you don't go off the edges of the grid.

Play around with this and see what you can make of it. Start with your existing JTextArea creation code, and add to it code to declare a grid of JTextAreas and assign each of the ones you created using "new" to a tile of the grid.

From this, you could add simpler manipulations to the JTextAreas, for example set all of them to red when you click a button, or set even rows to one color and odd rows to another color. When you get this working, then move on to the more complex scenario you started with: assigning yellow to any tile next to a blue one.

The reason this last part is harder is that you have to inspect other tiles in order to affect the "current" tile in the loop. The examples I gave before only involve inspecting the "row" and "col" variables in the loop or nothing at all (first example, all red).
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hi again!

thankyou for your time and reply. I have found it very helpful and i can understand the area alot more now. You were right when u mentioned my current method - the long tedious way. You have answered my question well as i now believe i can do this and fit it into my program. You have given me enough information (java and english) to make a start at the problem and to understand what everything is doing! So thanks again - you're better than some of the books i have :-) Once i get it up in running (hopefully tommorrow) i will gladly post the result (in case someone needs something similar in the future), and if i have any further questions ill post at java ranch again. Don't worry i always spend time trying to figure out problems before posting.

thanks for your great help,

Joseph.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You're quite welcome, and thank you for the complement.

As I said, the next step you'll want to employ if you want to create larger grids is to genericize the JTextArea creation code and skip the tile11, tile12, ... variables entirely. This will be a good exercise for sure.

You'd use the same basic nested loop construct from my post, and after creating the grid array, create and assign a JTextArea to each element in the array (inside the loop). You'll need to come up with correct expressions for the x and y coordinates of them based on the row and column loop variables.

Good luck, and welcome to JavaRanch!
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello.

just a post on my progress! ive spent 4 and a half hours on it and im stuck on the part you didnt mention (converting the referneces to actual text boxes). ive been reading and googling and cant seem to find anything that works - eg using loop >> add(JTextArea[row][row].. (i realise this isnt the precise syntax). ive tried about a dozen different methods to display a white JTextPanel on grid[0][0] and just get alot of errors.

here is the simple code (ive just written out basically your help, with none of my modifications):

final int NUM_ROWS = 4;
final int NUM_COLS = 4;
JTextArea [][] grid = new JTextArea[NUM_ROWS][NUM_COLS];

JTextArea t1 = new JTextArea();
JTextArea t2 = new JTextArea();
..............................
..............................
JTextArea t16 = new JTextArea();


for ( int row = 0 ; row < NUM_ROWS ; row++ ) {
for (int col = 0 ; col < NUM_COLS ; col++ ) {
grid[0][0] = t1;
grid[0][1] = t2;
grid[0][2] = t3;
................
................
grid[3][2] = t15;
grid[3][3] = t16;


i release you have spent a good bit of effort trying to help me and im sure you are a busy person so if you know of a really good article or web example i would be grateful. If the example is in the first 6 or so pages of google, i probably read and tested it, although my keywords may be different from yours.

thanks again

joseph.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The sixteen lines setting grid[#][#] equal to a tile## should be outside the for loops as they initialize the grid elements. Here's an example that sets all the backgrounds to red.The outer loop (row) is encountered first. This loop runs for each row in the grid (0 to 3, inclusive). For each row, the inner loop is runs for each column in the grid (0 to 3, inclusive). Thus the the sixteen tiles will be set in the order you assigned them: 0,0; 0,1; 0,2; 0,3; 1,0; 1,1; ... 3,3.

If you continue to get compile or runtime errors, post the code that causes the error along with the actual error. Point out the line number and complete message if it's a compiler error. If you get a runtime exception, post the stack trace. Then we can go from there.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Okay, it sounds like you're not adding them to the content pane yet. I thought you already had a grid of panels displayed and abbreviated the code a bit much. For each tile you must create a JTextArea, add it to the content pane, and set its size and boundaries. Typically this is done using a LayoutManager, and the content pane is using a BorderLayout by default. It should still allow you to specify your tile sizes directly, I think.

If you have trouble adding the JTextAreas, post to the Swing forum. You might want to run through Sun's Swing tutorials to get familiar with laying out components.

Rethinking it, you could probably get away with using JPanels instead of JTextAreas and then set them up using
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello.

ive been messing around with "nested loops" to try to get this working.
my books only have "non-grid" examples. ive got some information on the web - minesweeper and battleships and ive been looking through them (techniques such as Math.max/Math.min etc)
however the past few hours i keep getting the same problem:

In order to decide wether or not to look at adjacent cells, the system needs to first identify a cell as being blue. however, every metod ive found calls the same error (that awt cant be used with color). Should i code it so that if a tile is background blue that it is converted to a string? alot of examples use if xxx == yyy (i use if t1.getBackground(Color.blue).
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by joseph mcgratton:
however the past few hours i keep getting the same problem:
And that problem is?? Please post stack traces or compiler errors so it's not so hard to help!
In order to decide wether or not to look at adjacent cells, the system needs to first identify a cell as being blue. however, every metod ive found calls the same error (that awt cant be used with color).
I cannot imagine the compiler telling you that the AWT libraries cannot be used with color. Can you copy-n-paste the exact error message here, please?
alot of examples use if xxx == yyy (i use if t1.getBackground(Color.blue).
Ah ha! Code -- now I can help! The JavaDocs for getBackground() says it returns a Color, so to see if it's blue you should use this:Remember that just because people here are experienced doesn't make them mind readers. We need to see the errors that javac and java are giving you. It would be like telling your mechanic "my car isn't running" but not letting him look at your car. Would she be able to tell you what's wrong?
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello,

i have solved the problem , thanks again for your help,

Joseph.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tile constraints