0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I figured it wont work in my solution since an interactive graphic is needed in my case/ – user1038662 Nov 25 '14 at 05:43
  • *"I figured it wont work in my solution since an interactive graphic is needed"* You figured wrong. An image displayed in a label can be every bit as 'interactive' as graphics painted in a panel. – Andrew Thompson Nov 25 '14 at 05:46
  • 1
    Im not aware of that before. I checked my code and seems feasible. I'll try that way. Thanks – user1038662 Nov 25 '14 at 05:49
0

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

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for comments, I see override getPreferredSize() is one way to enlarge the panel, but really dont know how. How can I get the dimension of the Graphics object that is going to be paint? – user1038662 Nov 25 '14 at 05:42
  • No, `Graphics` generally doesn't have a size component associated with it. You can check the current size of the component using the `getWidth` and `getHeight` methods of the component – MadProgrammer Nov 25 '14 at 05:43
  • *"How can I get the dimension of the Graphics object that is going to be paint?"* A `Graphics` object has no dimension. Are you drawing instances of `Shape`? – Andrew Thompson Nov 25 '14 at 05:44
  • I get Graphics object from the paintComponent( Graphics g ) method call and do whole bunch of things on it.. so basically I got no way to find out the overall size? – user1038662 Nov 25 '14 at 05:46
  • *"whole bunch of things"* Is that your way of saying 'not (only) `Shape` objects, but other things as well'? There are ways of finding a bounding box for other things. Please be specific - what other things are being drawn? – Andrew Thompson Nov 25 '14 at 05:49
  • Generally speaking, you should be making the decisions about what and how to render the output in advance, so based on the current state of you needs, you should be able to pre-calculate the size you need BEFORE painting starts – MadProgrammer Nov 25 '14 at 05:49
  • @MadProgrammer You are correct, doing precalc on every item is possible. I think Im on the right way following the suggestion from Andrew to replace with a image. Great thanks to all of you, cheers – user1038662 Nov 25 '14 at 06:05