• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I'm new to Java, you see...

 
Dean Angerer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not much of a Java man just yet...but I do know C and C++ very well, and I also am a very at using Macromedia Flash to program any kind of game there is...even 3D (which Flash doesn't support). I've also written a 75 page "book" (tutorial, rather) about programming games in Flash, and how to apply trigonometry, calculus, algebra, and geometry to create games (Like logarithmic level-up systems and parabolic slopes and such).

And now that I've established that I'm not a complete n00b idiot, I have a question or two about Java, regarding the life cycle of an Applet and the flow of execution in an applet. In C/C++, execution begins at main() or WINMAIN(), and runs straight down the line, executing each line of code. My Java applet doesn't seem to do this.



�Why does paint execute, even though it's below Bob()? I'm assuming it's because paint() is a method that's important ad such...?

�Also...WHERE is the entry point in a Java program? It would really help to know just where execution starts and ends.

�Why is Java just about 100% classes and OOP? Is there REALLY a need for that? C and C++ do just fine with global functions outside of classes.

�Can Java be used to create (almost) animations of sorts? Which would require one to be able to actually see an object's position be changed over a series of frames rather than having all of the code execute before you can see the object move..

Hey, sorry about all the questions, ya?

I'm new to this...so I guess it's okay if I have a few questions, right?

Anyhoo, any help would be appreciated.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

You've asked a lot of questions, and I won't answer every single one, but I'll hit the high points.

Bob() doesn't execute simply because nothing calls it. This is precisely as in C and C++, so I'm puzzled as to why you expect otherwise. Neither Java, nor C nor C++, make any promises about executing all the functions you define, not in the order they appear in the source nor in any order at all.

A Java application has a main() routine whose signature is

public static void main(String[] argv)

quite similarly to C and C++. You can launch them from the command line or by clicking on an icon, again, just as in C/C++.

But an applet isn't an application. In a way, it's like a device driver: it's a collection of entry points which some other software -- here, a web browser -- calls at well-defined times. Among those entry points are init(), start(), stop(), and destroy(), as well as paint() and update(). The browser calls init() once after an applet is created, then start() when it's fully displayed. When the web page goes away, stop() and then, eventually, destroy() are called -- or sometimes, start() again. paint() is called whenever the screen needs to be updated, and as to your question about animations in an applet: you just need to implement paint() to draw a parameterized drawing, and then create a thread to modify the parameters over time and call repaint() to ask for the screen to be repainted.

These days, applets are one obscure little corner of Java -- most Java code is in server-side applications these days, often in the form of servlets. A servlet is very like an applet in that it's a collection of entry points that a container -- here, a web server will call to satisfy web resource requests.
 
Dean Angerer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
Originally posted by Ernest Friedman-Hill:
Bob() doesn't execute simply because nothing calls it. This is precisely as in C and C++, so I'm puzzled as to why you expect otherwise. Neither Java, nor C nor C++, make any promises about executing all the functions you define, not in the order they appear in the source nor in any order at all.
--------------------------------------------------------------------------------



I guess the fact that everything's defined within a class threw me off big time...that and the fact that I had no idea where the entry point to the program is. Of course I know that Bob() won't be executed in something like:


...because it's never called.

I tried to implement the main() function into my class, but it's never executed...so...is main() not an entry point in a Java applet or program? The way it seems, having several different entry points and messages...it seems more like the execution flow of a Win32 WinProcedure() function(The one that switches its execution based on what messages it receives).

I guess that I'm still a bit in the dark about the actual FLOW of execution in a Java applet. Do the entry-point functions just execute once, and, if no other function is called or no loop is ran through, execution of code just stops, or...?

It seems like you also have to have separate Java files for each class...is this true?

Anyway...I hope these aren't dumb questions...I'm new to this..and it's just different from what I'm used to.
[ April 30, 2005: Message edited by: Dean Angerer ]
[ April 30, 2005: Message edited by: Dean Angerer ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The browser creates an object of your Applet class using a no-argument constructor.

2) The browser does some behind-the-scenes work to configure your Applet.

3) When everything's ready, the browser calls init(). Typically, this is the method where you set up your GUI, initialize data structures, etc.

4) Then the browser calls start(). If the applet needs to do ongoing work -- say, run an animation -- then in your implementation of start(), you'd want to create a new Thread, and do that ongoing work in that separate thread. start() would return, but the new Thread would keep running.

5) When the user browses away from the applet's page, the browser calls stop() on the applet. Your implementation of stop() should terminate any threads you started.

6) If the user comes back to the same page, the browser will call start() again. If, on the other hand, the user doesn't come back soon enough, the browser may decide to call destroy(). Your implementation of destroy() should free all resources, because your applet is officially dead.

Note that it's perfectly OK to have an applet that fails to define one or more of these entry points -- the default just does nothing. If you are writing a form-based GUI as an applet, for example, it won't need to do any ongoing work; any computation or form-submittal will be done in GUI event handlers.

There are a zillion Java books that go over this material, as well as online tutorials. It sounds like you're going to need something to get you thinking in an events-and-objects way. Check out our bunkhouse for book recommendations -- or head right over to Sun's online Java tutorial and start larnin' right away!
 
Ben Fischer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, each class is in a separate file. The class has methods that can be called from within the class or from outside the class. If one of the methods is called main() that will be called if the class is run as an application. Otherwise, when an object is created from a class, other than the constructor, nothing is run automatically.

Applets follow the same rules as classes without the "main" method: they just hang around waiting for something to call their methods. The difference is that there's not another java class interacting with them, but a web browser. As mentioned, the web browser calls init(), start(), stop(), destroy(), paint(), update(). The applet has no control over when those methods are called. paint() could be called repeatedly for a few minutes as the user scrolls or otherwise interacts with the web page, then not called again ever unless the applet kicks off a thread to call it periodically with the repaint() method (which simply requests the browser to call the applet's own paint() method).

You can have a main() method in your applet and run it as an application, as long as it's coded correctly. There's more work setting up the context, but some of the things may not apply to one mode of operation or the other, but it can be done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic