1

I'm attempting to create a simple game but I'm running into trouble when drawing from multiple classes in the main class. Here is my code so far.

public static void main(String[] args) 
{
    JFrame mainGameFrame = new JFrame("Space4X");

    StarsPanel starsPanel = new StarsPanel();
    HomePlanet homePlanet = new HomePlanet();

    mainGameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainGameFrame.setResizable(true);

    mainGameFrame.getContentPane().add(homePlanet);
    mainGameFrame.getContentPane().add(starsPanel);


    mainGameFrame.pack();
    mainGameFrame.setVisible(true);

}

public void HomePlanet()
{
  HomePlanet homePlanet = new HomePlanet();

}

}

Here is my HomePlanet class,

package space4x;

import javax.swing.*;
import java.awt.*;

public class HomePlanet extends JPanel 
{


public void HomePlanet()
{

}


public void paintComponent (Graphics page)
{   
    //Draw planet
    super.paintComponent (page);
    page.setColor(Color.RED);
    page.fillRect(10,10,50,50);

}


}

I also have a class called StarsPanel that draws stars in random locations and it works perfectly, but HomePlanet for some reason won't.

Under the mainclass, if I have

mainGameFrame.getContentPane().add(starsPanel);

first instead of starsPanel, it will draw what is under the HomePlanet class but not what is under the StarPanel class.

This is driving me crazy why I can get one to work and not the other.

Please help.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Yeah, I know I need something along the lines of homePlanet.paintComponent(page); but it doesn't like the page part. – Lethologicas Mar 16 '12 at 12:25
  • @khachik: Yes it will compile. It's just that you'll have a method which has the name of the class, rather than having a constructor. – JB Nizet Mar 16 '12 at 12:26

1 Answers1

3

The content pane uses a BorderLayout by default. When you add a panel to the content pane, you add it to the center of the BorderLayout. So when you add the second panel, it replaces the first one in the center of the layout.

If you want the two panels to be painted one on top of the other, then you don't have the appropriate strategy. Create a single panel, which combines the paintings of the two panels you have at the moment.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So something along the lines of mainGameFrame.getContentPane().add(starsPanel, homePlanet); ? – Lethologicas Mar 16 '12 at 12:26
  • Alternately, create a single buffered image and set it as the icon for a label. Paint to the image. Nothing has to be extended (and nobody needs to get nailed to anything). – Andrew Thompson Mar 16 '12 at 12:27
  • No. something along the lines of mainGameFrame.getContentPane().add(starsWithHomePlanetPanel) – JB Nizet Mar 16 '12 at 12:27
  • *"something along the lines of mainGameFrame.getContentPane().add(starsPanel, homePlanet); ?"* Something along the lines of a) try it or b) RTFM! – Andrew Thompson Mar 16 '12 at 12:28
  • No need to be rude, I did try it and it doesn't work, not does JBs suggestion. – Lethologicas Mar 16 '12 at 12:30
  • *it doesn't work* is not sufficient. What's your new code? What does it do? What do you expect it to do? – JB Nizet Mar 16 '12 at 12:38
  • The new code is what I said already mainGameFrame.getContentPane().add(starsPanel, homePlanet)the result is Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null) at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:426) at javax.swing.JRootPane$1.addLayoutComponent(JRootPane.java:531) at java.awt.Container.addImpl(Container.java:1120) at java.awt.Container.add(Container.java:966) at space4x.Space4X.main(Space4X.java:31) Java Result: 1 I expected it to draw both like what I asked in the original question. – Lethologicas Mar 16 '12 at 12:40
  • I said "No" when you asked *"something along the lines of mainGameFrame.getContentPane().add(starsPanel, homePlanet); ?"*. Read the answers and comments. And read the documentation of the add() method. The second argument is NOT a second panel to add. – JB Nizet Mar 16 '12 at 12:44
  • And I said mainGameFrame.getContentPane().add(starsWithHomePlanetPanel) doesn't work and then you felt the need to be arrogant about it :) – Lethologicas Mar 16 '12 at 12:46
  • So, I repeat my question. What's your new code (the one which *doesn't work*, with the startsWithHomePlanetPanel)? What does it do? What do you expect it to do? I'm not being arrogant. I'm trying to be helpful. You are the one who doesn't read correctly, and doesn't answer the questions of the persons, like me, who help you for free. – JB Nizet Mar 16 '12 at 12:49
  • RTFM, great idea of help there buddy. Screw it, I'll go ask my mate who's not a cunt. – Lethologicas Mar 16 '12 at 12:50
  • congratulations, you've been plonked at your very first question! – JB Nizet Mar 16 '12 at 12:56