| Author |
Haw to display painting in Panel
|
Michail Petrov
Ranch Hand
Joined: Sep 04, 2008
Posts: 32
|
|
Hi i start to experiment with painting and a wonder where is the problem in this code
I have a simple JFrame adn JPanel and i use
to display the painting but the frame is empty
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8439
|
|
Michali,
You should be overriding the paintComponent() instead of paint()
This would be more suitable in the swing forum.
Moving.
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Michail Petrov
Ranch Hand
Joined: Sep 04, 2008
Posts: 32
|
|
|
Why i nead to extends JPanel for using paintComponent(Graphics g)
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
Michail Petrov wrote:Why i nead to extends JPanel for using paintComponent(Graphics g)
Do you mean as opposed to overriding the paint method? In Swing the paintComponent method's role is to paint the component whereas paint's role is to paint the component, its borders and its children. If your painting doesn't involve changing the painting of the borders and the child components (and most do not), then you're far better off and much less likely to muck things up by overriding JPanel's paintComponent [corrected] method rather than its paint method.
If you mean why should I extend JPanel and paint in there rather than JFrame, well, I don't know all the reasons for this, but one is that if you do this, you loose some of the benefits of painting in Swing such as Swing's automatic double buffering (without which animations will look choppy). I'm sure that there are several other good reasons too as painting a JFrame can get quite tricky, what with all its components.
Much luck!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Because your Hello class is never included in the GUI, it's paint method never gets called.
As said before, you should extend JPanel and override its paintComponent method. And do not forget to start with a call to super.paintComponent(g).
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Haw to display painting in Panel
|
|
|