I got a JPanel
class and its method paintComponent()
is overridden to draw a custom chart.
Would the panel's dimension be stretched if I drew a graphic bigger than the panel?
If answer is no, how could I make it so?
I got a JPanel
class and its method paintComponent()
is overridden to draw a custom chart.
Would the panel's dimension be stretched if I drew a graphic bigger than the panel?
If answer is no, how could I make it so?
Would the panel's dimension be stretched if I drew a graphic bigger than the panel?
No it would not stretch.
..how could I make it so?
Easiest way: Instead of putting the logic of drawing the graph into the paint method, put that logic into drawing an image of the correct size, then display the image in a JLabel
.
Painting directly won't affect the size of a component, you can paint beyond the visible bounds of a component quite easily.
Generally the size of a component is determine by a combination of getPreferredSize
, getMinimumSize
and getMaximumSize
and the layout manager been used to manage the container in which the component resides.
If you want to affect the size of the panel, you should have it change the return result of (in most cases) the getPreferredSize
and invalidate
the container hierarchy.
You'd want to do this directly by overriding the getPreferredSize
method, rather then relying on setPreferredSize
, as this prevents someone else from changing it.
See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details