Advertisement



< Prev
Next >



Paint() method in Applet





In this article, we are going to understand how to print a message in an applet window by implementing the method paint() of the Applet class. Within the method paint() we will call the drawString() method to print a text message in the applet window.




when the paint() method is invoked?


The method paint() is automatically called whenever there is a need to display an applet window, this need could arise in certain situations -
  • When an applet window is brought up on the screen for the first time.
  • When an applet window is brought up on the screen from a minimized state, this leads the redrawing of the applet window and hence the paint() method is automatically called.
  • When an applet window is stretched to a new size, this leads to redrawing of the applet window to a new size and hence the paint() method is automatically called.





Signature of paint() method


public void paint(Graphics g)
The method paint() gives us access to an object of type Graphics class. Using the object of the Graphics class, we can call the drawString() method of the Graphics class to write a text message in the applet window.


Advertisement




Signature of drawString() method


public void drawString(String, int x, int y)
The method drawString() takes a String which will be printed in the applet window, starting from left-top corner at the coordinates x and y.




  • Writing a message on the applet window.


    • In the upcoming code, we are implementing the paint() method in our applet class.
    • Within the paint() method, the drawString() method is called to write a message in the applet window.
    import java.awt.*;
    import java.applet.*;
    
    /*
    <applet  code="Applet1" width=400 height=200>
    </applet>
    */
    
    public class Applet1 extends Applet
    {
    String str ="";
    
    public void init()
    {
    str="init-";
    }
    
    public void start()
    {
    str=str+"start-";
    }
    
    public void stop()
    {
    str=str+"stop-";	
    }
    
    
    public void paint(Graphics g)
    {
    str=str+"paint-";;
    g.drawString(str,40,100);
    g.drawString("Hello from the Applet.", 40,40);
    g.drawString("How are you doing?", 40, 60);
    g.drawString("We wish you a pleasant day today.", 40, 80);
    }
    
    }


    To run our applet using the appletviewer, type the following command at the command prompt -

    appletviewer Applet1.java

    Where Applet1.java is the name of java file that contains the code of an applet.


    Output


    Right after running the applet program using the appletviewer a new applet window is displayed to us -

    Figure 1


    As the applet began its execution for the first time-
    • The method init() was the first method to be called in an applet's life.
    • Next, the start() method was automatically called by the init() method.
    • Next, the paint() method was automatically called to display the applet window to the user and within the paint() method, the drawString() method is called a few times to write messages in the applet window.


    Now, when the applet window screen is minimized and brought up again, the next applet window looks like this-

    Figure 2


    As you may see in the Figure 2 of the applet window-
    • The method stop() method was called when the applet window was minimized.
    • Next, the start() method was called right after the applet window is brought up again to restart its execution.
    • Next, the paint() method was automatically called to draw the applet window to display it to the user and within the paint() method, the drawString() method is called a few times to write text messages, displaying us the order in which each applet method was called during our interaction with the applet window.




      • Please share this article -



        Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON and DJANGO


    Advertisement