Basic Details About APPLET

Applet in Java

Programming For Future: JApplet


● Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

● Advantage of Applet 

There are many advantages of applet. They are as follows:
 – It works at client side so less response time.
 – Secured
● It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc.

Drawback of Applet 

● Plugin is required at client browser to execute applet.









Lifecycle of Java Applet :-


● Applet is initialized.
● Applet is started.
● Applet is painted.
● Applet is stopped.
● Applet is destroyed.


How to run an Applet? ??

● There are two ways to run an applet
 – By html file.
 – By appletViewer tool

setBackground and setForeground 


● To set the color of the background of an applet window, setBackground () method is used. 
● The general form of the setBackground () method is void setBackground(mycolor) 
● Similarly, to set the foreground color to a specific color, that is, the color of text, setForeground () method is used. 
● The general form of the setForeground () method is void setForeground(mycolor) 
● mycolor is one of the color constants or the new color created by the user 
● The list of color constants is given below: • Color.red • Color.orange • Color.gray • Color.darkGray • Color.lightGray • Color.cyan •Color.pink • Color.white • Color.blue • Color.green • Color.black • Color.yellow 



Image in Applet 


● Displaying Image in Applet
● Applet is mostly used in games and animation. For this purpose image is required to be displayed. The java.awt.Graphics class provide a method drawImage() to display the image. Syntax of drawImage() method: public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer) is used draw the specified image.
● How to get the object of Image: The java.applet.Applet class provides getImage() method that returns the object of Image.
● Syntax: public Image getImage(URL u, String image){}
● Other required methods of Applet class to display image: public URL getDocumentBase(): is used to return the URL of the document in which applet is embedded.
● public URL getCodeBase(): is used to return the base URL.


repaint(), paint() and update() 


● i) public void update(Graphics):

By default update(Graphics) fills the drawable area of a Component with its background color, and then sends paint(Graphics) to the object. Thus, flicker that comes from redrawing the background over and over, can sometimes be fixed by overriding update().

● ii) public void paint(Graphics) :

● Every Java Component implements paint(Graphics), which is responsible for painting that component in the Graphics context passed in the parameter. When you extend a Component (like when you write a Applet), if you want to display it differently than its superclass, you override public void paint(Graphics) .

● iii) repaint() :

● The repaint() method is sent to a Component when it needs to be repainted. This happens when a window is moved, or resized, or unhidden. It also happens when a webpage contains an image and the pixels of the image are arriving slowly down the wire. Calling the repaint method causes the whole component to be repainted. repaint() -> update() -> paint()

NeXt Artical Related APPLET With Demo Example in Java




Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)





Post a Comment

0 Comments