Advertisement



< Prev
Next >





Drawing in an Applet




In this article, we will show you how to load an image or draw different shapes like an oval, rectangle and a line in an applet. To perform these operations, we are going to use three methods -:




  • getCodeBase() method of Applet class.


    public URL getCodeBase()
    This method gives us the location of the directory in which our applet code file is located. This location is returned to us in terms of an object of URL class. Using this object of URL class, we can get and load any image file present in the same directory using the next two methods.





  • getImage() method of Applet class.


    public Image getImage(URL url, String name)
    This method gets the image file in the form of Image object. This image file named name is present in the directory location specified by url. This directory also contains the applet code.





  • drawImage() method of Image class.


    public drawImage(Image img, int x, int y, ImageObserver observer)
    This method draws the image referred by Image object img, at the coordinates x and y in an applet's window. We also need to pass an object of ImageObserver interface. We can do by passing this reference of our Applet class, because it implements ImageObserver interface.





  • Loading an image in an applet.


  • In the upcoming code, we are going to use all the three methods discussed above to read and load an image from a specified location.
    //Evening handling in an applet 
    
    import java.awt.*;
    import java.applet.*;
    
    /*
    <applet  code="Applet3" width=400 height=350>
    </applet>
    */
    
    public class Applet3 extends Applet
    {
    Image im;
    public void init()
    {
    System.out.println("initializing an applet");
    System.out.println("The full path to our applet file : " + getDocumentBase());
    System.out.println("Directory in which our applet file is located : " + getCodeBase());
    im= getImage(getCodeBase(),"wood.jpg"); //loading an image from the same directory in which applet file is located
    
    
    }
    
    public void start()
    {
    System.out.println("Starting an applet");
    }
    
    public void stop()
    {
    System.out.println("Stopping an applet");
    }
    
    
    public void paint(Graphics g)
    {
    System.out.println("Painting the Applet");
    g.drawString("Displaying an imagine in our applet", 0,10);
    
    g.drawImage(im,50,50,this);
    }
    }

    In order to run our applet using appletviewer, type the following command at command prompt-

    appletviewer Applet3.java

    Where Applet3.java is the name of java file that contains the code of an applet. In the code, as we are calling System.out.println() method, hence we are going to receive some output at the command promt on the execution of this applet.

    Output


    initializing an applet
    The full path to our applet file : file:/D:/Java/Applet3.java
    Directory in which our applet file is located : file:/D:/Java/
    Starting an applet
    Painting the Applet 
     
    Besides this, right after running the applet program using appletviewer a new applet window is displayed to us which contains our image- "wood.jpg" -




    Advertisement




  • Drawing shapes in an applet.


  • In the next code, we are going to draw geometrical shapes in an applet, such as an Oval, Rectangle and a line, by calling appropriate methods of Graphics class.

    import java.awt.*;
    import java.applet.*;
    
    /*
    <applet  code="Applet7" width=400 height=400>
    </applet>
    */
    
    public class Applet7 extends Applet
    {
    public void init()
    {
    System.out.println("initializing an applet");
    }
    
    
    
    public void paint(Graphics g)
    {
    System.out.println("Hello there");
    g.drawString("Drawing an oval in our applet", 0,10);
    g.drawOval(50,50,100,100);		//draws an Oval in applet window
    g.drawRect(150,150,100,100);		//draws a Rectangle in applet window
    g.drawLine(270,270,350,350);		//draws a Line in applet window
    }
    }
    


    Output-


    appletviewer Applet7.java

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


    Our applet window with the geometrical shapes drawn in it.



    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

C#, PYTHON and DJANGO


Advertisement

p