< Prev
Next >

Resizing an Applet

We can set the size of an applet window by specifying the values of its attributes width and height in the <applet> tag. After setting of size for an applet, we can still override or alter this size of an applet window by calling the resize() method of Applet class.

Signature of resize() method

public void resize(int newWidth, int newHeight)
This method resizes the applet window by setting its width to newWidth and height to newHeight.

    Resizing an applet window using resize() method.

    • In the next code, we have initially set the size of the applet window to have a width and height of 100.
    • Next, we are passing two parameters to applet i.e. NewWidth and NewHeight. We are reading these two parameters by calling getParameter() method of Applet class.
    • Finally, we are passing the parameters values to resize() method to resize the applet window to a new width and height of 400.

    Note : getParameter() method returns the value of parameter passed to an applet in the form of a String object. We will have to convert this String value to an int, before we could pass it to resize() method, because resize()method takes only int arguments.


    Resizing an applet by overriding its original size

    import java.awt.*;
    import java.applet.*;
    <applet code="Applet10" width="100" height="100">
    <param name="StatusBar" value="Have a good day">
    <param name="NewWidth" value="400">
    <param name="NewHeight" value="400">
    public class Applet10 extends Applet 
    String statusBar;
    String newHT;
    String newWD;
    public void init()
    statusBar= getParameter("StatusBar");
    newWD = getParameter("NewWidth");
    newHT = getParameter("NewHeight");
    public void paint(Graphics g)
    g.drawString("Reading the parameters passed to this applet to resize its window -", 20, 20);
    //Setting a message at the status bar of our applet.
    //Display messages about new width and height to our applet
    g.drawString("New width of applet : "+ Integer.parseInt(newWD), 20, 60);
    g.drawString("New height of applet : "+ Integer.parseInt(newHT), 20, 80);
    //Resizing our applet
    resize(Integer.parseInt(newWD), Integer.parseInt(newHT)); //resizing the applet
    g.drawString("StatusBar message -" + statusBar, 20, 100);


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


    Where is the name of java file that contains the code of an applet.
    Right after running the applet program using appletviewer, a new applet window is displayed to us -

    As you may see in the output, our applet window has been resized to a new bigger width and height of 400 each, as compared to the initial mentioned width and height of 100 in the <applet> tag at the beginning of our file.

    Please share this article -


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition