0

How to add 2 JComponent to JPanel at the same place stretched on full JPanel? I tryed to:

add(ship,null);

add(ground,null);

Community
  • 1
  • 1

1 Answers1

2

I'm guessing as to your use case, but is "ground" a background image containing JPanel? If so, then add it in such a way so that it fills the container, perhaps via BorderLayout.CENTER with the container using BorderLayout of course.

The ship, I'm guessing is a sprite that is drawn on the background. If so, consider having it not be a JPanel but rather a logical object that holds an image Sprite that can be drawn in the ground's paintComponent method.

As always the devil will be found in the details. You first.


Edit
You state in comment:

Stage is my background and it extends JPanel. Ship is a player element (JComponent). Ground (JComponent) is something which player can't touch. I wanted to have this two elements (ground and ship) like layers where the image is drawn. I need to have one point reference to check collisions.

I assume that you have a paintComponent(Graphics g) override method in your Stage object where you draw your background image.

Based on this assumption, I change my previous recommendation in that I feel that neither ground nor ship should extend JPanel since you don't really need for them to do this. You already have a JPanel on which you can do your drawing and game interaction, so why create more when they'll only clutter things up? The only exception I see is if either ground or ship need to hold other components, and if so, then yes, they should extend JPanel.

Consider:

  • What is important to both of these objects, both ground and ship, are their locations and their images.
  • Give both ground and ship a BufferedImage field that can hold their images, with getters and setters.
  • Give them x and y int position fields and width and height, with getters and setters that can be used to test for collision and can be used to move their respective sprite (image).
  • Give them a draw(Graphics g) method that draws their respective images at the x an y location when called and passed a valid Graphics instance.
  • Have Stage hold an instance of each ground and ship, and have stage's paintComponent(Graphics g) method draw both instances by calling their respective draw methods.

Edit 2
You now ask:

One more question: When should i have keyListener?

I recommend that you not use a KeyListener.

KeyListener is a low-level construct, and in general you should prefer to use higher-level constructs such as Key Bindings as they are safer and easier to use without causing side effects or having problems. For instance, it is easy to run into problems with focus issues if you use a KeyListener since it only works if the component listened to has the focus. This is easily circumvented if you used key bindings instead.

  • You can find the Key Bindings tutorial here
  • and an example of using it with animation in my answer here.
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Stage is my background and it extends JPanel. Ship is a player element (JComponent). Ground (JComponent) is something which player can't touch. I wanted to have this two elements (ground and ship) like layers where the image is drawn. I need to have one point reference to check collisions. –  May 10 '14 at 19:51
  • @user3191398: Please see edit. I feel that you'll have much greater freedom if you don't have ground and ship extend JPanel. – Hovercraft Full Of Eels May 10 '14 at 20:00
  • Ground and ship extend JComponent not JPanel. I am doing that because i can't use something like >paintTheWorld method, but i consider your recommendations. You can see my code in >Why does my Image in JComponent move if I don't change its position in method g.drawImage()?, where you answered. –  May 10 '14 at 20:20
  • I'm sticking with my recommendations: don't have ship and ground extend JComponent. You only need **one** JComponent/JPanel here, and that should be the drawing JPanel that draws everything. – Hovercraft Full Of Eels May 10 '14 at 20:23
  • One more question: When should i have keyListener? –  May 10 '14 at 20:47