Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Haw to display painting in Panel

 
Michail Petrov
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michali,

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

This would be more suitable in the swing forum.
Moving.
 
Michail Petrov
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why i nead to extends JPanel for using paintComponent(Graphics g)
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic