Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display 256 images in a 16x16 grid

 
Jan-Henrik Clausen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My target is to display 256 images in a 16x16 grid (as the subject of this topic already hints at). This grid should be positioned in a container. When clicking with the mouse on one of the images I want to know on which image the click was made, and that in a way that makes it easy to get the coordinates (0,0 being the top left image, 15,15 the bottom right). I use NetBeans and JSE 6.

As a beginner I would like to get a hint to the following preliminary thoughts I had.

a. Use labels (JLabel). Problem: You have to generate 256 of them. Not very attractive to do with NetBeans - and you have no elegant way to get the coordinates back (using the name of JLabel - JLabel3x5, JLabel15x15 - seems very awkward , using the graphic coordinates is not reliable if the window is resized). Even if you do not use NetBeans and generate the labels manually, say in an array, the coordinate problem remains.

b. Use a table (JTable), display the images in a grid and prevent the cells from being editable. As far as I got it you can define your own TableModel and include something like



If I read it right I can add a listener which catches the MouseEvent my mouse makes while clicking in a cell and get the coordinates of it (getSelectedRow (), getSelectedColumn ()).

Is there another possibility which is better suited? Using a table seems a bit cheaty to me, because you usually expect a table to be editable at least a bit and not only showing pretty pictures of rooms and tunnels and other dungeon stuff (which it would in my case).

Input would be greatly appreciated.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudius Calvus wrote:a. Use labels (JLabel). Problem: You have to generate 256 of them. Not very attractive to do with NetBeans - and you have no elegant way to get the coordinates back (using the name of JLabel - JLabel3x5, JLabel15x15 - seems very awkward , using the graphic coordinates is not reliable if the window is resized). Even if you do not use NetBeans and generate the labels manually, say in an array, the coordinate problem remains.

Drop the NetBeans graphical designer and learn to code by hand.

I'd create a JLabel[16][16], initialize each element manually (new JLabel(...)), then add it to its parent container which will have a GridLayout. This GridLayout will divide your GUI in a 16x16 grid, each cell having the same size. So if your GUI resizes, so will all of your JLabels.
Furthermore, each JLabel will have its own MouseListener for handling the clicks so the source of the mouse event is the image on which you clicked.
 
Campbell Ritchie
Marshal
Posts: 52632
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread would fit better on our GUIs forum. Moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!