• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Pacman

 
Greenhorn
Posts: 4
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i already have a Pacman game who shows the output (positions of the pacman and the ghosts, score, round) in cmd, but now i have to show that in a gui, and use the keyboard to move the pacman instead of giving the final position at the begining. The logic of the ghosts, the score, etc. its done. But now i have doubts about how to show the info in a window, a gridLayout and a vector of images will be ok?

I can ask the positions of every character and the food on the map its in a vector.

Sorry for my english and thanks in advance.
 
Marshal
Posts: 70657
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Forget about vectors (nobody uses Vector any more anyway) and grid layout or any other programming terms. Start by drawing what you would like to appear on screen. You can consider how to implement it later.
 
Saloon Keeper
Posts: 7392
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would consider bypassing the GridLayout and instead provide a JPanel that you draw on by overriding the paintComponent(Graphics g) method. You could start off simply, by drawing colored circles (for example) to represent the various game items. Later you could replace the circles with small images. It's easy enough to calculate the grid placement for the various pieces. You'd have to hook this into your existing code to be able to query the row/column where pieces are to be drawn.
 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO, for the OP the GridLayout with multiple JPanels might be better than overriding paintComponent. The panels can be referred to via index (2,5) which would closely match the existing "positions"
The game items can be JLabel with the image. Since a JComponent can be added only once to the parent container, it would be very easy to "move" the item.
 
Lucas Moreno
Greenhorn
Posts: 4
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this



But i wanna know how to update the content that will be in each panel (JLabel) with a method. I have a "dataMap" where -1 is a wall, 10 is a regular pellet, 50 a power pellet, 0 a blank, and i can ask the position of pacman and ghosts with getters.



I've thought that in the Controller side i will modify the map and read it from the view side.

Thank you, everyone!
 
Lucas Moreno
Greenhorn
Posts: 4
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already finished the game, or at least the playing board. Now i want to start it from a Menu JFrame, but when i do it, the game doesnt repaint and doesnt listen to the keyboard, its like the board is not getting focus.

In prompt its working OK, repainting, listening the keyboard...





But when i try with a menu, just repaint when the game ends (pacman die)...





What is wrong?

Thank you all in advance!
 
Rancher
Posts: 3208
29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

its like the board is not getting focus.  



It probably isn't. Don't use a KeyListener to listen for the pressing of the arrow keys.

Instead you should be using Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for more information.
 
Lucas Moreno
Greenhorn
Posts: 4
Eclipse IDE Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Im looking those tutorials to change the imput system.

What about the repainting? Have an idea about why is working in TestPacman but isnt in MenuPrincipal?

This is the class Pacman that haves the method Jugar, wich im calling in TestPacman (working ok) and in MenuPrincipal (doesnt repaint).



And this is the class of the board game



Again, thank you all!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic