File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Haw to display painting in Panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Haw to display painting in Panel" Watch "Haw to display painting in Panel" New topic
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: 9990
    
    7

Michali,

You should be overriding the paintComponent() instead of paint()

This would be more suitable in the swing forum.
Moving.


[How to ask questions] [Donate a pint, save a life!] [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: 19544
    
  16

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Haw to display painting in Panel
 
Similar Threads
viewing applet in IE
Swing drawing
full-screen gray window problem and event handling problem
How to invoke a paint method in Frame?
string concatenation