CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2011
    Posts
    20

    Java Board Grid + Moving objects

    Hello!

    Does anyone have a tutorial or some tips on making a grid in java where it is possible to select objects via the keyboard and move them to other positions in the grid.

    And check if the move is a valid move so you have to be able to track the item on the board.

    Kind regards.
    Last edited by MaSSaSLaYeR; December 12th, 2011 at 09:20 AM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Board Grid + Moving objects

    Does anyone have a tutorial or some tips on making a grid in java
    Use GridLayout
    where it is possible to select objects via the keyboard
    Add a keyboard listener.
    and move them to other positions in the grid.
    How do you want to do this, eg are you typing in co-ordinates, using tab to move to the next location etc.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2011
    Posts
    20

    Re: Java Board Grid + Moving objects

    Quote Originally Posted by keang View Post
    How do you want to do this, eg are you typing in co-ordinates, using tab to move to the next location etc.
    Move in the grid using the arrow keys (up down left right) then when you get to a position you need to press "enter" to select it then the object should be highlighted and you should be able to move to another position that is "empty" place on the board and then press enter again.

    When you "drop" you need to check if your move is valid this means that you can't move to an empty space more then 2 block aways.

    I tried to draw it:

    http://img20.imageshack.us/img20/7320/uploadmj.png

    The "O" can only be placed in place "1" and not in "2"


    Btw all is done via JavaTV to make it extra harder
    Last edited by MaSSaSLaYeR; December 12th, 2011 at 09:42 AM.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Board Grid + Moving objects

    Sorry but I can't help as I've no idea what is available under JavaTV.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    20

    Re: Java Board Grid + Moving objects

    Quote Originally Posted by keang View Post
    Sorry but I can't help as I've no idea what is available under JavaTV.
    Is it possible to see what event/object has focus after you press enter for example?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Board Grid + Moving objects

    If the JavaTV GUI is based on the standard Java event model then yes but if not then I have no idea because, as I said, I don't know what features JavaTV has.

    You'll have to read a JavaTV tutorial to see what you can do.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Nov 2011
    Posts
    20

    Re: Java Board Grid + Moving objects

    Quote Originally Posted by keang View Post
    If the JavaTV GUI is based on the standard Java event model then yes but if not then I have no idea because, as I said, I don't know what features JavaTV has.

    You'll have to read a JavaTV tutorial to see what you can do.
    The problem is there isn't one :/

  8. #8
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Java Board Grid + Moving objects

    Quote Originally Posted by MaSSaSLaYeR View Post
    The problem is there isn't one :/
    That's funny, googling javatv the second result I got was this: The JavaTV Tutorial | www.tvwithoutborders.com; it was right after the Wikipedia page for Java TV which also has a reference to the same site.

  9. #9
    Join Date
    Nov 2011
    Posts
    20

    Re: Java Board Grid + Moving objects

    Quote Originally Posted by jcaccia View Post
    That's funny, googling javatv the second result I got was this: The JavaTV Tutorial | www.tvwithoutborders.com; it was right after the Wikipedia page for Java TV which also has a reference to the same site.
    I read that tutorial it's just a basic one, how the system works without in depth examples of for instance a board game.

  10. #10
    Join Date
    Apr 2007
    Posts
    442

    Post Re: Java Board Grid + Moving objects

    Yes you can always query the focus owner from FocusManager, there is a static getter. Note that for this work, your components need to have a KeyListener of some sorts assigned to them.

    So you could, for instance do the following in the gid component (pseudo):

    Add a listener that listens to arrow keys

    just arrow key -> move focus
    +cntrl_mask -> move button

    upon selection:
    get the right component in the grid, mark it selected

    upon move:
    find the component to which to move.
    validate that the selected can move to that components place

    I spose, there is a swap of states between these components (right?). If move is valid,
    swap contents between the components
    mark the right one as selected
    advice the components to redraw.

    In this sense, you do not need to think of actually moving the components. You just move one internal state of a component to another one. Also your components do not need to be anything other than graphical placeholders for the information they carry. The only "active piece" is the grid itself.

  11. #11
    Join Date
    Nov 2011
    Posts
    20

    Re: Java Board Grid + Moving objects

    I got a typ to implement a paint method how do I do that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured