views:

179

answers:

3

Hi guys,

I'm about to create a java crossword application but I am unsure of what packages to use to draw the crossword grid. I know you can manually draw grids with Graphics2D etc. but I'm not sure if this is the easiest way to do it as I'll need text fields in the grid squares.

Anyone have any suggestions as to creating the crossword grid.

A: 

Maybe use a JTable and custom renderer for the cells. That should be at least a simple solution. Of course it takes a little time to get used to JTable, but eventually it's quite simple.

fish
+1  A: 

Actually I don't think you need textfields in the grid squares but just to write down every single letter for every grid cell..

To allow editing you just catch keyboard strokes over the component you use and set crossword cells according to what the user writes.

Doing it this way would be quite easy because you can use a back 2-dimensional array that stores the whole grid, then when the user select a definition you just start filling single letters whenever keys are typed starting from the first cell of the definition.. your draw routine will need just to be able to draw the grid and center letters inside cells, nothing more..

A JTable could work but it think it's oversized for your problem, because you'll end up interfacing with a lot of things you don't need at all..

EDIT (for comment):

I did it something similar this way: you can have a cell class

class Cell
{
   boolean isBlank;
   char value;
}

with an array of cells you obtain your grid:

Cell[][] gamefield = new Cell[15][15];

then inside paint() you can easily iterate:

for (int i = 0; i < Scheme.rows; ++i)
{
  for (int j = 0; j < Scheme.cols; ++j)
  {
    g2.drawRect(i*32, j*32, 32, 32);

    if (Scheme.scheme[i][j].isBlank)
      g2.fillRect(i*32 + 3, j*32 + 3, 32 - 5, 32 - 5);
  }
}

Just because I still have a screenshot result was something like alt text

Jack
@JackThanks a lot for your reply. What would you use to draw the grid then? A collection of squares from Graphics2D?
Alex
I was thinking I could just use some image editing software to create all the possible type of grid squares and load the into a grid.
Alex
you mean all the letters? You can use __graphics.drawString()__.. you can also easily position it by getting the character width (so you can center it) with __graphics.getFontMetrics().stringWidth('A')__
Jack
Thank you, you've been very helpful!
Alex
Having some rendering issues, with my other components. What is the best layout to use that ensures the whole grid is drawn on screen (I'm currently using BoxLayout - which has been fine in the past for images)?
Alex
You should have your JPanel (or JLabel, or whatever since you are overriding the paint method) with a __setPreferredSize(new Dimension(width, height)__ specified. So when you place the grid panel where you want it should be completely shown..
Jack
Ok thanks, I'll give it a try.
Alex
A: 

Did you consider JavaFX at all? JavaFX will let you create "scene-graphs" based on vector/graphics edited using Adobe Photoshop/Inkscape etc.

If not, most simple way would be to extend JTextField such that that will just hold 1 char and will be black/or whatever color to indicate non-editable and disabled. Also, add a custom Border to indicate puzzle question number. Put everything in to a GridLayout.

ring bearer