File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes Drawing a Character Grid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Drawing a Character Grid" Watch "Drawing a Character Grid" New topic

Drawing a Character Grid

Darrell Morello
Ranch Hand

Joined: Apr 28, 2005
Posts: 38
Hi all

I'm trying to create Word Search type of game where words are hidden in a bunch of letters. I'd like to know whats the best way to go about it. I mean do I create a char grid. How do I draw them on the screen (I mean like using drawString(.. or something). I know how to create a line grid using drawLine but I am kind of stuck in this part, creating the grid. Any kind of resources or guidance would help a lot.

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Yes, you could use either drawString() or pack all of the characters into a single char[] and use drawChars(); it's up to you. You set the font on the Graphics before drawing the first letter, and go from there. If you can calculate the correct points for the gird lines, doing the same with a slight adjustment should work for the characters.

I recommend experimenting with it a little and see how far you can get. Come back if you get stuck and post what you have with some specific questions.

Welcome to JavaRanch!
Darrell Morello
Ranch Hand

Joined: Apr 28, 2005
Posts: 38
Thanks for the welcome

Heres what I have gone to so far...
This is just the Grid panel which will be added to the main frame.

It just manages to draw a grid and insert letters. Now I'm looking for a way to select words so that the point where I press the mouse button and releases it is matched with the starting and ending columns and rows of the word respectively. It then marks the word as found. Any ideas would help.

Thanks again.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

"Audio Slave"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it


Thanks! and welcome to the JavaRanch!


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You basically need to turn mouse events into word selection events, as it appears you've started above. As a first design I would do the following. Note that a grid cell is denoted by its row and column.
  • mousePressed(): Save starting grid cell, change background of starting cell to some highlight color. Set tracking cell to starting cell.
  • mouseMoved(): If new cell is the same as the tracking cell -- user is in the same cell as the last update, exit method without doing anything more. Otherwise, set tracking cell to new cell and set the background colors of all cells between the starting and tracking cell to the highlight color. If the line of cells from starting cell to tracking cell don't make up a valid line (aren't perfectly horizontal, vertical, or diagonal), highlight only the starting cell. Make sure you clear the backgrounds of the previously highlighted cells if they shouldn't still be highlighted.
  • mouseReleased(): Clear all highlighting. If the line of cells from starting cell to tracking cell is valid, the user has selected a word. Perform any other validation you need to do and exit.
    I agree. Here's the link:
    subject: Drawing a Character Grid
    jQuery in Action, 3rd edition