OOPM JAVA - Create applet to draw square with diagonal and circle enclosed in it

EXPERIMENT NO: 16

Aim: Write a program to create applet to draw square with diagonal and circle enclosed in it.

Objective: To create an applet (a small web application) for drawing the different graphical shapes by using applet and awt packages available in java. 

Outcome: Students implemented the java applet for creating a design pattern in terms of graphical shapes.

Theory:

Applet is a very special type of program that is embedded in a webpage for creating dynamic content. It runs in the browser and works at the client side.

Advantage of Applet

There are many advantages of applet. They are as follows:

  • It works at client side so it takes less response time.
  • Secured
  • It can be compiled by browsers running under many platforms, such as Linux, Windows, Mac Os etc.

Drawback of Applet

  • Plugin is required at client browser to run the applet.

The java.applet.Applet class gives 4 life cycle methods and java.awt.Component class gives 1 life cycle methods for an applet.

java.applet.Applet class

For creating an applet java.applet.Applet class should be inherited. It provides 4 life cycle methods for an applet.

  • public void init():It is used to initialized the Applet. It is invoked only once.
  • public void start(): It invokes after the init() method or browser is maximized. It helps us to start the Applet.
  • public void stop(): It is used to stop the Applet. It invokes when Applet is stop or browser is minimized.
  • public void destroy():It is used to destroy the Applet. It is invoked only once.


java.awt.Component class

The Component class provides 1 life cycle method for an applet.

  • public void paint(Graphics g):It is used to paint the Applet. It also provides Graphics class object that can be used for drawing oval, rectangle, arc etc.

How to run an Applet?

There are two ways to run an applet that is :

  1. By html file.
  2. By appletViewer tool (for testing purpose).


Algorithm:

  1. Start.
  2. Draw rectangle by choosing the co-ordinates and width and height and use appropriate function from awt package.
  3.  Draw circle by choosing the co-ordinates and width and height and use appropriate function from awt package.
  4. Draw line by choosing the co-ordinates and use appropriate function from awt package..
  5. Stop. 


Code : 

import java.applet.Applet;

import java.awt.Graphics;

public class MyApplet extends Applet {

    public void paint(Graphics g)

    {

        g.drawRect(100,50,100,100);

        g.drawOval(100,50,100,100);

        g.drawLine(100,50, 200, 150);

        g.drawLine(200,50, 100, 150);

    }  

     /* 

     <applet code="MyApplet.class" width="300" height="300"> 

     </applet> 

    */   

}


Output : 

Create applet to draw square with diagonal and circle enclosed in it

Observations and learning: 

From this experiment we learnt to create an applet for drawing different graphical shape by using applet and awt packages available in java.

 

Conclusion: 

We Implemented the java applet for creating different graphical shapes.

 

Question of Curiosity 

1. What is an applet? 

Ans : An applet is any small program that is used to perform one specific task that runs with the scope of dedicated widget engine or a larger program, often as a plug-in.

 

2. What is the importance of applets in Java coding?       

 Ans : 

1. Mostly all the applets are sub-classes of java.applet.Applet class.

2.Applets are not stand-alone programs. Instead, they run with either a web browser or an applet viewer. 

3. Execution of an applet does not begin at main() method.

Output of applet window is not handled by System.out.println(). It can also be handled with various AWT methods, such as drawString().

 

3. What is life cycle?

Ans : The core lifecycle events of an applet as as follows:

1. init() - When the web browser comes across an applet, after checking security settings, it runs the init() function within the applet. This code is written in Java. 

2. start() - As long as you're still on the page, the browser will initiate the start() function.

3. stop() - Once you navigate to a different page, the stop() function is run. If you go back to that page for any reason, start() is run again.

4 .destroy() - Finally, when at last you're completely done browsing and close the browser, destroy() is run.

  

4. What are life cycle methods of applet? What is the tag used for embedding an applet in HTML file?

Ans : 

1) init(): init() method is used to initialize an applet. It invokes only once at the time of initialization. We can also compare this method with a Thread class born state.

2)start(): start() method is used to start the applet. It invokes after the init() method invoked. It is invoking each time while browser loading or refreshing. Until init() method is invoked start() method is not invoked. We can also compare this method with Thread class start state

3)stop(): stop() method is used to stop the applet. It invokes every time when browser stopped or minimized or abrupt failure of the application. Afterstop() method called, we can also use start() method whenever we want. This method mainly deals with clean up code. We can also compare this method with Thread class blocked state.

4)destroy(): destroy() method is used to destroy the application once we have done with our applet work. It is invoked only once. Once applet is destroyed we can’t start()the applet. We can compare this method with Thread class dead state

5) paint(): paint() method is used for painting any shapes like square, rectangle, trapezium, eclipse, etc. paint() method has parameter as ClassGraphics. This Graphics class gives painting features in an applet. We can compare this method with Thread class runnable state.   

The HTML<applet> tag is used for embedding an JAVA applet within an HTML document.


Comments