Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Tetris

 
dave dal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I do! add(grid[x][y]) should add it to the main panel.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dave dal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic