• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

How can I print the position in the array?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

So I have an array of JButtons in a grid 13x16. One of my objective in the array is to have it output the position that the ball is in. I have managed to get it to work but it is not in a loop so therefore I would have to write out the code for every space in the array which would take forever and I was hoping someone would be able to tell me how to loop it so that I don't have to do so? this is the code for outputting the space in square 1 of the array, is there a way of saying "IF ball moves down setText new position of the ball"?



many thanks
Ben
 
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already have examples of nested loops traversing your 2D array in your other post. Why not copy that and show us what you've done and where you're still stuck.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, i need to put one line in the nested 2d array to make it output a value per button. for example, the first button in the array should be button one, I then need it to output that the baby is in that position on the maze in a text field. This is the code for the loop, I need it so when the baby is stood in the button it outputs the baby location. the code for the loop is this:

 
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

have an array of JButtons in a grid  


Are you asking how to get the row,column location of a button that was clicked on?
One way is to store the button's location inside of the button object so it can be retrieved from the button object when it is passed to a listener.


where I need to add the new line of code


What is that for?
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added the baby image via the imageArray[y][x] to the array and I need a statement saying that if the baby location is equal to the image array location output the value of the button the baby is standing on. I hope that makes sense.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm how would I do that?
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A 2D array doesn't have a position '1', it does have a position (0,0) though.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

have an array of JButtons in a grid  


Are you asking how to get the row,column location of a button that was clicked on?
One way is to store the button's location inside of the button object so it can be retrieved from the button object when it is passed to a listener.


where I need to add the new line of code


What is that for?



that line was so you guys would know where I thought the new line would need to be put telling it to add the values
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:A 2D array doesn't have a position '1', it does have a position (0,0) though.



I know, I need it to convert that position [0][0] to a string saying 1
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how would I do that?


I know of two ways:
1) use the button component's ClientProperty methods to put and get the value
2) extend the button class and add methods to save and retrieve the desired values
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:

Carey Brown wrote:A 2D array doesn't have a position '1', it does have a position (0,0) though.



I know, I need it to convert that position [0][0] to a string saying 1



and then do that for every other button as well with the button [0][1] being 2 and [0][2] being 3
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:

Carey Brown wrote:A 2D array doesn't have a position '1', it does have a position (0,0) though.



I know, I need it to convert that position [0][0] to a string saying 1


In the context of a 2D array [0][0] or (0,0) has meaning. What does '1' mean? For that matter, what does '27' mean?
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a 16x13 grid of jbuttons with icons in them. the baby begins in the first button on the grid. there is a text field called square position. the text field needs to output the number of the square the baby is standing on as a string. the first button in the array is [0][0], i need the square position text field to output that that square is 1. the square next to the first square is [0][1], i need the code so when the baby moves into that space it would display the number 2 in the text field saying that is the second button. i need it to set every button to a value and have that value outputted into the square position textfield only when the baby stands on it.
 
Norm Radder
Master Rancher
Posts: 4188
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when the baby stands on it


What values are available when that happens?  
Are row, col (x,y) available?
Or is a reference to a button in the grid?
Or what?
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is your first mention of a TextField. Is there an array of TextFields or only one? If the text field displays '27' what does that tell you?
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the baby is img_array[X][Y].setIcon(baby);. i have it set up so when i press a button in the direction it will change the x or y value to move the baby location into a different button in the array. what I need to happen is when that location is changed (for example moving right one) it will update the text field and say "now the baby is stood in square: 2"
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This is your first mention of a TextField. Is there an array of TextFields or only one? If the text field displays '27' what does that tell you?



there is only one text field, not an array. this text field needs to output what button the baby is currently standing on.
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This is your first mention of a TextField. Is there an array of TextFields or only one? If the text field displays '27' what does that tell you?



if the text field said the number 27 the baby is stood in [11][1]
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:...and what if baby moves down 1?


if I were to move down 1 it would need to display the number 17 as the numbers should go across the rows 1-16 and when that row finishes it should move onto the next row and say 17#
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:

Carey Brown wrote:This is your first mention of a TextField. Is there an array of TextFields or only one? If the text field displays '27' what does that tell you?



if the text field said the number 27 the baby is stood in [11][1]


I think you'll find that it is [1][11] instead ([row][col] which is different than (x,y)), which makes my point, you shouldn't be asking the player to have to perform mental arithmetic in order to determine what space '27' (or anything else) is in.

What exactly does your requirements ask for?
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following your logic, you can compute the location by:
square = row * 16 + col + 1;
you shouldn't need to store it.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my requirement sheet says:
"The "Square" will be set to the current position of the baby i.e it will be in the range of 0 - 207 when the top left is 0 and the bottom right is 207"

so I need to convert the position of the baby in the grid to a number between 0 - 207.
 
Carey Brown
Bartender
Posts: 7789
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well enough.  So....
square = row * 16 + col;
you shouldn't need to store it.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey I resolved the issue. I had to create another 2d array but of integers instead of buttons and use the array to reference the babies position. And then convert the integer value to a string and output it. Thanks for the help though.
 
Ben Grabouski
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey sorry, I thought I had responded to this post but apparently not. I had solved the issue using an integer array in the same layout as the maze array then I set it up so when the baby moved it read the new integer as a string from the maze and outputted it. Thanks for the help though!
 
Marshal
Posts: 72059
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Grabouski wrote:. . . another 2d array . . . .

I am afraid that doesn't strike me as a good solution. What you now have is parallel arrays, which are very error‑prone. It would be better to create a Baby class and a Brick class, each of which has fields recording its location. You can have a move(Direction) method, and an enum for directions.You can get the Baby to test the array for whether there is a route where it is trying to go. Or, more object‑orientedly, create a Location object and a Map to link locations with bricks.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic