Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Inheriting Applet and paint() GUI Method Logic?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Wink Inheriting Applet and paint() GUI Method Logic?

    Hey all,

    I'm confused not on how to use the paint() and init() method in java, but merely how it works. I understand that when the 'Applet' class is inherited, the methods of both the Applet class and the methods of the 'Container' class are available to the inheriting class. So, bearing in mind that I
    am somewhat new to programming in general, and quite new to the terminology used, so can you explain the following questions in the simplest manner possible?

    1. This is pretty much just a question about inheritance in general. Why is it that when I inherit the 'Applet' class, it automatically
    starts a GUI window? Does it have to do with the constructor of the 'Applet' or 'Container' class? What is it about the Applet, or Container class that causes inheriting those classes to

    2. This is kind of a question about methods in general. When I say:

    public void init()
    {
        ...
    }
    I know that I'm doing something with an external method, meaning from somewhere else, but how is it different from making my own method called 'init()'? By writing the above code, am I calling on that method? Or am I simply 'making' it? It seems as though it'snever called! I've heard the explanation of 'when the program starts, so does the method', but what causes that to happen? Why!? I
    imagine the reasoning to that is similar, if not identical to the reasoning of how the 'paint(Graphics g)' method is called, which I am
    also unclear on.

    So yeah, those are the questions that I have, it's simply a matter of 'Why' and 'How'.

    Thanks in advance,

    Jake
    Last edited by Jakesta42; July 13th, 2011 at 07:56 PM.


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheriting Applet and paint() GUI Method Logic?

    1. Whenever you write "extends Applet", you're inheriting the Applet class's methods and stuff. It's because you've basically, with that phrase, turned your class into a subclass of Applet. A subclass represents an "is-a" relationship - that is, your class IS an Applet, and thus inherits all of Applet class's methods.

    2. If you were just writing some random class of your own that didn't extend anything, then writing "public void init()" would indeed be making your own method. But when your class extends Applet, it inherits all of Applet's methods, including a method called init(). This method is called whenever the webpage containing the applet is loaded. When you then write "public void init()", you aren't making your own method, but you aren't calling a method either - you are overriding the Applet class's method. That basically means that when something else calls your class's init() method, the actual method that gets called is the one you wrote, not the one you inherited.

    Init() is called when the Applet is displayed. Normally - well, who cares what it doe by default. You have overriden it so that it does whatever you wrote in your code that you want it to do.

    Paint() is the same deal. The paint() method is called whenever a GUI component is displayed. basically, that means it is called when you run the program. Normally, this might just be a white box or something. However, you can OVERRIDE the paint method by writing your own in a class that extends JPanel or JComponent or some other class that already has a paint method.

    It seems that you're a little confused about inheritance. Here's a hopefully clearer explanation:

    Say you have a class named ClassA. Then, you write a ClassB, which extends ClassA. ClassA has a method called printStuff().

    public class ClassA{
    public void printStuff(){
    System.out.println("Hello world");
    }
    }

    public class ClassB extends ClassA {
    }

    Now, in the above, ClassB is empty. However, even though there's nothing written in ClassB, it's not actually empty. It does have one method, called printStuff(), which it inherits from ClassA. Thus, if you did this:

    ClassB bbb = new ClassB();
    bbb.printStuff();

    It would work even though nothing is written in ClassB. The runner first looks for the method in the subclass (ClassB). Not finding it, it goes up one level, and, sure enough, finds it in the superclass (ClassA), and calls it.

    What if we rewrite ClassB like this?

    public class ClassB extends ClassA {
    public void printStuff(){
    System.out.println("Goodbye");
    }
    }

    Now, when you create bbb and call printStuff(), it prints "Goodbye". The method has been overridden. When the runner calls a method, it looks from the bottom up: first at the subclass, and then at the superclass. If it finds the method in the subclass, it calls it there. If not, then it calls it in the superclass. This is an important concept about inheritance.

    Why? Well, think about it: it actually does make the whole displaying graphics thing easier. When you display an applet or any graphics of any kind, a lot of stuff goes on behind the scenes to make those pictures appear that you don't have to think about. Every applet has an init() method, and it's the init() method that is called when it's displayed. If it didn't have an init() method, things would go wrong. Applet has an init() method, and therefore everything that extends Applet does too, even if you don't write one in. And if you don't want the white block or whatever the default init() method gives you, you can just override it to do whatever you want.

Similar Threads

  1. Applet paint program frame work.
    By ShaunB in forum Java Swing Tutorials
    Replies: 4
    Last Post: October 31st, 2012, 08:44 AM
  2. [SOLVED] How do you get this fountain to animate with paint method?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 48
    Last Post: May 31st, 2011, 04:00 PM
  3. Paint program adding classes to main method class
    By Maxfmc in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 15th, 2011, 07:01 PM
  4. Applet paint program frame work.
    By ShaunB in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: May 5th, 2010, 06:35 PM
  5. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM

Tags for this Thread