| Author |
Java Tetris
|
dave dal
Greenhorn
Joined: Jan 12, 2012
Posts: 3
|
|
Hi there,
I have a question as to why JPanel's work the way this certain way. When I try and make GUI applications or games, I always try to make the centered display JPanel through a different class that extends JPanel.
I've literally just started a Tetris demo, and I decided to make a MainScreen class that extends JPanel. And I add an instance of MainScreen to the center of the original JPanel(Tetrisgame class). I first create an instance of the MainScreen JPanel called screen. I then create an instance of BlockPart called bp. And I invoke screen.addPartToScreenAt(2,2,bp) - the method's function is to add the parameter bp(instance of BlockPart) to the MainScreen JPanel at coordinates 2,2. I do this so that I can see a new JPanel - namely a BlockPart - on the MainScreen. I just want to test that I can actually change the JPanels from separate classes by attempting to display one simple BlockPart(JPanel) on the screen! Assistance?
Code that I did not provide but it irrelevant:
-Main class that creates JFrame window.
-Block contains an array of BlockParts that will eventually be a combination of blockparts that create the conventional Tetris blocks.
,
Nightingale
|
 |
Wendy Gibbons
Bartender
Joined: Oct 21, 2008
Posts: 1098
|
|
so your green panel isn't appearing on the main panel?
you store it in the array of jpanels (grid) but do you actually add it to the main panel?
|
 |
dave dal
Greenhorn
Joined: Jan 12, 2012
Posts: 3
|
|
|
I believe I do! add(grid[x][y]) should add it to the main panel.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
You add empty JPanels, yes. But in your addPartToScreenAt method you don't remove one of those JPanels and add the BlockPart. A quick and untested fix:
Also, a small improvement:
dave dal wrote:
Instead of calling setLayout(...), call super(...). Right now these constructors implicitly call super() which sets a FlowLayout. Afterwards you immediately overwrite that with the actual layout. By calling the super class constructor with a layout you skip the FlowLayout.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
dave dal
Greenhorn
Joined: Jan 12, 2012
Posts: 3
|
|
|
Wow thank you so much for your help, guys. Yes. I've been studying java, c++, and python for only a year and a half now. But these GUI concepts really help me understand many classes that I have not already learned!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You're welcome.
|
 |
 |
|
|
subject: Java Tetris
|
|
|