Newb java question

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
So I'm completely new to OOP and my professor's style of teaching just lets us loose on things... but there's so many things to account for, I can't seem to get started.

Anyway, I'm trying to draw tetris pieces and display them in a JPanel using a scroll bar. Would I have to redraw and recalculate the panel each time the bar is scrolled? Or is there an easier way to do this?
 
Oct 27, 2007
17,009
5
0
How are you drawing the piece? Using a Graphics object? If so then you can just draw the Tetris piece on the JPanel wherever you want it, and call setPreferredSize() on the JPanel to make it the size you want. Create a new ScrollViewer with the panel as a parameter and add that to a JFrame. I am fairly sure this is how it works, but I can't test it right now.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
Okay, but say I want to draw a lot of pieces. For some reason, my code only draws the last piece and nothing else. I'm using repaint in a loop and ive googled around and apparently repaint will throw out consecutive requests. I have to draw like 100+ pieces but I'm reading pieces from a file 1 at a time. I think there's a way to draw a piece on a panel and 'stick it' to a larger panel? Is making hundreds of panels a bad thing?

I could also load everything in and draw everything in 1 shot. But I don't really wanna do that.
 
Oct 27, 2007
17,009
5
0
Nested panels are fine. I'm sure some of the performance gurus will jump on me for this but I'm currently building a Java GUI for my software engineering class with about 100 JPanels on screen at a time.
 

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
if im using a jscrollpane to display a jpanel, is there more i have to do besides adding the jpanel to the scrollpane and setting the visibility? Because the scrollbars never show up.
 
Oct 27, 2007
17,009
5
0
This is a problem that occurs when drawing on a JPanel but not using any actual Swing components on it. Try using setPreferredSize(new Dimension(xSize, ySize)) on the JPanel. Unfortunately Java Swing components aren't exactly consistent with how they are sized so it may or may not work depending on the layout managers, internal components and the alignment of the planets. If setPreferredSize doesn't work try playing with the setMinimumSize and setMaximumSize methods, although I'd avoid using those two if you can help it because they're even less consistent.