This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Drawing a Character Grid" Watch "Drawing a Character Grid" New topic
Author

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.

Thanks
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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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: http://aspose.com/file-tools
     
    subject: Drawing a Character Grid