• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method calls

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am wondering if EVERY method defined in Java is automatically called at least once??? Like in C++ you can define a method, but if you don't call the method then it just isn't called.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods defined most certainly do NOT all have to be used.
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the reason i asked this is because i copied an example out of my Deitel & Deitel book, that example uses this method that is defined but not called:
public void paint()
still, the compiler i guess still calls this method, that's what Deitel's overview of the simple program says. why is this? is there a list of classes and functions that all Java programs automatically import or call?
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
paint() is a GUI (Graphical User Interface) function. GUI's aren't like non-gui programs. There isn't a set flow of functions -- lots of different things could happen during runtime (mouse moving, minimizing and maximizing, typing, moving the window, etc.). The program that you write can't be expected to keep track of all this. That is why there is extensive GUI support in Java. Since I don't know if you're using Swing or AWT, I'll call the GUI library the Java GUI system.
It would take a lot of processing power to redraw the screen continuously. This may be necessary if you are making an animation. If you are just making a straitforward user interface, it's safe to say that nothing will need to be redrawn until some sort of event happens. This event could be that the window is covered by another window and exposed again, or that something is clicked on. The Java GUI system says, "Okay, I have no garentee that everthing looks right anymore, so I'll call paint()." It also call paint when the applet/application is first shown. Do I make any sense?
On the other hand your program might change something on the GUI itself that the Java GUI system won't be aware of. In that case, you must call paint() (or repaint()) to ensure that the user sees what the program thinks he/she sees.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,
Can't help on the question of "is there a list of classes and functions that all Java programs automatically import or call?"
As for the use of paint() and why the method is defined if the coding doesn't explicitly call it....
My understanding is (and correct me if I'm wrong) that any object that appears on the screen will have a paint method that draws the object on the screen. Sometimes you'll need to define a paint method. Other times this won't be necessary. Following are a couple of examples which might illustrate this idea.
Eg. of not needing to define a paint method
~~~~~~
If you want a button to appear on the screen, you don't need to define a paint method, as button inherits it's paint method from Component.
Extract from the API Component documentation.
~~~~~~~~~~~~~~~
public void paint(Graphics g)
Paints this component.
This method is called when the contents of the component should be painted in response to the component first being shown or damage needing repair... "
~~~~~~~~~~~~~~
When you make an AWT frame containing the button visible via setVisible(true), one of the things that usually happens is an invocation of repaint(), a method which calls paint(). You don't have to call paint() or even define a paint() method in this case - it's done for you.

Eg. when you'd need to define a paint method
~~~~~~~~~~
You might create a simple AWT frame and wish to display a line of text directly onto the frame (not on the title bar, but within the border of the frame itself), without using a label component. In this case, you could define a paint method and use the Graphics drawString method to display the text at a set position within the frame.
eg.
public void paint(Graphics g){
g.drawString("Text to display", 50,75);
}

Here, you need to define a paint method just to get your text to display. In this case, if you'd instead used a label component, you wouldn't need to define a paint() method, as label (like button) inherits a paint method from Component.

I'm wondering if the example of paint() in the Deitel book does something that's appears programmer defined (eg. like drawing a string, or filling a shape with colour) and so requires explicit definition of a paint method??
cheerio
rowan
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the API for the Applet class you will see several methods that are called by the browser.

Sort of like the JVM's call to public static void main( String[] args ), the browser calls:

void init()
Called by the browser or applet viewer to inform this applet that it has been loaded into the system.

void start()
Called by the browser or applet viewer to inform this applet that it should start its execution

void stop()
Called by the browser or applet viewer to inform this applet that it should stop its execution.

void destroy()
Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.

void paint(Graphics g)
Paints the container.
is inherited from the Container class. This method is called by the browser or appletviewer after the init method completes execution and the start method has started executing to draw on the applet. It is also called autometically every time the applet needs to be repainted.

"the compiler i guess still calls this method"
The compiler does not call methods. The browser or appletviewer calls these methods.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic