File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing a Character Grid

 
Darrell Morello
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic