Search code examples
javagraphics2dcrossword

Java Crossword Application - what package to use?


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.


Solution

  • 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