• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to display 256 images in a 16x16 grid

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread would fit better on our GUIs forum. Moving.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic