i am very confuse about java applets so please solve my questions
my question is that when we write applet then we write only methods defination not method calling so i am confuse about this that who call the methods of applets.and second is that we call the methods of other class by two ways first with object and second with class name but in applets we call some methods without object and without class name for example

setForeground(Color.cyan);
setBackground(Color.red);

these methods call without anything so how should be calling methods with this syntax.
my english is very poor so plz ignore my mistakes.
please release my confusion

Recommended Answers

All 9 Replies

my question is that when we write applet then we write only methods defination not method calling so i am confuse about this that who call the methods of applets.

Think of it this way: When you write a normal Java application you write a main method, but you never write any code that calls the main method. So who calls the main method? The Java VM does when your application is run.

Likewise when your applet is loaded in a browser, the browser's Java plug-in will load your applet class and call methods on it. It just doesn't call the main method, but rather calls the apropriate listeners whenever an event occurs.

these methods call without anything so how should be calling methods with this syntax.

They're called on the current object. It's exactly the same as writing this.setForeground(Color.cyan);. When calling a method on the current object, the this. is simply optional in Java (this is not specific to applets).

what mean by current object and we are not creating any object of any class please more elaborate this point.

The current object is the object that the method is being called on, that is the object referred to by this.

we are not creating any object of any class

The Java plug-in will create an instance of your applet class and then call the appropriate methods on that instance.

Remember also that your class extends JApplet, and you inherit all JApplet's methods and can call them directly.

JamesCherrill i understand that my class extends by JApplet but these methods are not part of JApplet these are defined in Component class not in JApplet so we need an object to call these methods but we can not do this Whyyyyyyyyyyyyyyyyyyyy

    setForeground(Color.cyan);
    setBackground(Color.red);

JApplet extends Component.

Yes indeed. In fact the complete inheritance tree for JApplet is

    java.lang.Object
        java.awt.Component
            java.awt.Container
                java.awt.Panel
                    java.applet.Applet
                        javax.swing.JApplet

... and so JApplet inhertits methods from all those classes

thanks you very much JamesCherrill and sepp2k i understand this.
one more request that plz give me a tutorial and link that give me detail about this i mean detail of built in inheritance and key concept of Applets

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.