Skip to Content

Draw Rectangle & Square in Applet Window Example

  1. /*
  2.         Draw Rectangle & Square in Applet Window Example
  3.         This java example shows how to draw rectangles and squares in an applet
  4.         window using drawRect method of Graphics class.
  5. */
  6.  
  7. /*
  8. <applet code="DrawRectanglesExample" width=200 height=200>
  9. </applet>
  10. */
  11.  
  12.  
  13. import java.applet.Applet;
  14. import java.awt.Graphics;
  15.  
  16. public class DrawRectanglesExample extends Applet{
  17.  
  18.         public void paint(Graphics g){
  19.                
  20.                 /*
  21.                  * to draw rectangle in an applet window use,
  22.                  * void drawRect(int x1,int y1, int width, int height)
  23.                  * method.
  24.                  *
  25.                  * This method draws a rectangle of specified width and
  26.                  * height at (x1,y1)
  27.                  */
  28.                
  29.                 //this will draw a rectangle of width 50 & height 100 at (10,10)
  30.                 g.drawRect(10,10,50,100);
  31.                
  32.                 /*
  33.                  * If you speficy same width and height, the drawRect method
  34.                  * will draw a square!
  35.                  */
  36.                
  37.                 //this will draw a square
  38.                 g.drawRect(100,100,50,50);
  39.         }
  40. }

Example Output