Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel, is it possible to make it move automatically like a pacman ghost?  RSS feed

Martin Nilsson
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am fairly new at this and its for a project.

But we are trying to make a bomberman clone.
I am programming the basic Enemy who is to roam the map were ever he can.

But recently the map was changed from images to JPanels on a frame.

And i cant in the world figure out how to get the JPanel to act like my old image. Well i cant even get it to move at all.
Is it possible to get it to move automatically?

(If you need more code it can be provided, but i have not written it.)

This is how the Panel is added to the frame. But even if i can see the panel on the created map i am still unable to get it to move.

Well the question is, is it possible to move automatically like a roaming enemy and if so please, could anyone point me towards the right direction on either where to look or do?

Rob Camick
Ranch Hand
Posts: 2753
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Learn and use proper Java naming conventions. Where in any text book or tutorial have you ever seen a variable name start with a capital letter? Don't make up your own rules. Its hard to read your code becuase a capital letter implies you are referencing a class, not a variable.

2) Why are you setting the layout on the "enemyPanel". You don't add any components to it so there is no reason to play with the layout.

3) Why are you creating a custom component to draw an image. A JLabel already does this for you when you use the setIcon() method.

4) Why are you using setVisible( true ). All Swing components are visible by default (except top level containers, like JFrame, JDialog, etc.).

5) You add the component to the frame. The frame is the one that needs a null layout so you can manually position the component

6) You can manually change the location of a component by using enemyLabel.setLocation(...);
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!