Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

paint() method is complex  RSS feed

 
Nikos kat
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, I am having real conceptual problems about how the method paint(Graphics g) is called automatically and also why is their the mysterius graphics parameter
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to object-oriented programming! It's typical in a framework (like Swing) to use existing classes (like JTextArea) or subclass to define your own (like subclassing JComponent or JPanel to do some custom graphics). In the subclassing case you may need to override methods (like paint or more likely paintComponent) to change the behaviour of objects (like changing how they render themselves, of course). What's very common in frameworks is that you *don't directly call these overridden methods*! To say they are called automatically makes it sound a bit mysterious. You are using the framework and calling some of its methods like yourFrame.setVisible(true). It shouldn't be suprising that making a top-level window visible will cause it to become visible on your screen, and that that involves components painting themselves. You don't need to call paint/paintComponent explicitly because that's the job of the framework. The whole point to a framework is that it's code you use so that you don't have to start from scratch, but you have to provide some subroutines to complete the application.

As for graphics, it represents the surface the component is painting itself upon. That may eventually be copied onto the screen, or it could be used in printint out the component or even just keeping a buffered copy in memory. That's the way the framework decided to implement painting. You job as someone who is subclassing JComponent, for example, is not to be too concetrned with the origin of graphics, but just to use it!
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally keep thinking I need to see the .java files of JCompenent or JPanel to see how it works. I guess our difficulty is we have to relax and not worry about how things are made. Just to be able to use them. But than I still wonder what is in the paint method before i override it?
roba
 
Nikos kat
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for you advice Rob and Jeff, its all good fun
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!