-1

I'm working on a game with a few people. This game requires us to create subclasses of JComponent, which each have their own paintComponent() method, invoked when it is added to a container. These subclasses are called Block, Goal, Spike, Ball, and Pit. Adding a single one of these components to a frame is no problem, since nothing else is being added along with it. I already added more than one JButton in the same JPanel in the main menu, and JButton also extends JComponent, so we all assumed our subclasses would work as well. However, when we try to add more than one of these (even of the same type), only one of them, if any, show up in the frame. We also track mouse clicks and mouse motion which are used for drawing lines with the window (for Balls to bounce off of), and adding a Line and another component does not work either. This so far is the only way we thought would fix our problem, but did not:

Container c = this.getContentPane();
c.setLayout(null);
Block b = new Block(200, 200);
Block b1 = new Block(220, 200);
b.setBounds(200, 200, 20, 20);
b1.setBounds(220, 200, 20, 20);
c.add(b);
c.add(b1);
c.revalidate();

This method attempts to add two Blocks in the same container. Since I set the layout of the Container to null, I can specify the exact location and size of the Block. When initializing a Block, the constructor takes in the x and y coordinate of the top left of the block, and the default size is 20x20 pixels. I also call setBounds() on both Blocks, so that they would be right next to each other, theoretically. However, when I compile and execute the code, Only the first one shows up. Any reason as to why, and how can this be fixed? Also, how would you add a Line and, a Block, for example? I have it so that creating two lines works, which creates an ArrayList of Lines, and after a new one is drawn, it is added to the ArrayList, after the previously added lines in the ArrayList are painted in the container. When I tried to add a Block and draw a Line, I could not start drawing a Line, which I presume is because even though the Block only takes up 20x20 space, it "takes up" the entire container, so that nothing else can be added.

anandp773
  • 17
  • 1
  • 6
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 08 '14 at 23:39
  • `b.setBounds(200, 200, 20, 20); b.setBounds(220, 200, 20, 20);` You realize that is setting the bounds of one component twice, as opposed to setting the bounds of two components once? – Andrew Thompson Jun 08 '14 at 23:48
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jun 09 '14 at 03:51

2 Answers2

0
however, when I compile and execute the code, Only the first one shows up

That is because you are overlapping the other one due to that you are using the same x and y axis on a absolute layout.

problem:

    Block b = new Block(200, 200);
    Block b1 = new Block(200, 200);

b1 will be on top of b thats why you can only see one Block

solution:

Use different x and y axis for both Block

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • So is it possible to use a 'different' x and y axis for each individual component being added? – anandp773 Jun 08 '14 at 23:16
  • @user2197420 change your setbounds the first param is x and the second is y. dont use the same x and y axis – Rod_Algonquin Jun 08 '14 at 23:17
  • When I completely separate the two blocks (one at (200, 200) the other at (300, 300)), nothing showed up. Not even the second one I added to the content pane showed up. – anandp773 Jun 08 '14 at 23:53
0
b.setBounds(200, 200, 20, 20);
b.setBounds(220, 200, 20, 20);

I think you have a typo. You are setting the bounds for the same component twice, so the second component still has a size of (0, 0) which means there is nothing to paint.

I think you want:

b.setBounds(200, 200, 20, 20);
b1.setBounds(220, 200, 20, 20); // note the b1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Yes, sorry, that was just a typo from typing up the question. In my code, it actually is b.setBounds(), and b1.setBounds() (with their proper arguments). – anandp773 Jun 08 '14 at 23:59
  • 2
    Don't type code. Use copy/paste so you don't have typos. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Jun 09 '14 at 00:06