Sunteți pe pagina 1din 15

Chapter 1- Java Applets

Winter 2018

1.Which packages are required to create an applet? [2]


Ans:- 1.java.applet.Applet
2.java.awt.*

2.List four methods of an applet life cycle. [2]


Ans:- 1.init()
2.start()
3.stop()
4.destroy()

3.List attributes of<PARAM> tag. [2]


Ans:- 1.Name
2.Value

4.Explain parameter passing in applet. [No. of times asked in gtu -6] [3]
Ans:-
->Parameters specify extra information that can be passed to an applet from the HTML page.
Parameters are specified using the HTML’s param tag.
-> Parameters are passed to applets in NAME=VALUE pairs in <PARAM> tags between the
opening and closing APPLET tags. Inside the applet, you read the values passed through
the PARAM tags with the getParameter() method of the java.applet.Applet class.
Syntax:
<param name=parameter_name value=parameter_value>
Example:

myapplet.html

<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>

UseParam.java
import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet
{
public void paint(Graphics g)
{
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}

5.Explain local applet and remote applet. [No. of times asked in gtu -7] [3]
Ans:- Local applet:
->An applet developed locally and stored in a local system is known as a local applet.
->When a Web page is trying to find a local applet, it does not need to use the Internet and
therefore the local system does not require the Internet connection.
-> It simply searches the directories in the local system and locates and loads the specified
applet.

Remote applet:
->A remote applet is developed by someone else and stored on a remote computer connected
to the Internet.
->If our system is connected to the internet, we can download the remote applet onto our
system via at the Internet and run it.
->To locate and load a remote applet, we must know the applet’s address on the Web. This
address is known as Uniform Resource Locator (URL) and must be specified in the applet’s
HTML document as the value of the CODEBASE attribute.
6.Give advantages and limitations of applet. [No. of times asked in gtu -2] [3]
Ans:- Advantages:
->Automatically integrated with HTML, hence resolved virtually all installation issues.
->Can be accessed from various platforms and various java-enabled web browsers.
->Can provide dynamic, graphics capabilities and visualizations.
->Implemented in Java, an easy-to-learn OO programming language.
->Can be launched as a standalone web application independent of the host web server.
->Safe! Because of the security built into the core java language and the applet structure, you
don’t have to worry about bad code causing damage to someone’s system.

Limitations:

Applets have many restrictions over the areas of security because they are obtained from
remote machines and can harm client-side machines. Some of them are as follows :

->Applet cannot work with native methods.

->Applet itself cannot run or modify any application on the local system.

->Applets has no access to client-side resources such as files , OS etc.

->Applet has little restriction when it comes to communication. It can communicate only with
the machine from which it was loaded.

->Applets tend to be slow on execution because all the classes and resources which it needs
have to be transported over the network.
7.Write a program to print “GTU Exam” in center of Applet window with RED color. [4]

Ans:-Program:

package AJava;

import java.applet.Applet;

import java.awt.*;

public class demo extends Applet

public void paint(Graphics g)

g.setColor(Color.red);

g.drawString("GTU Exam",getWidth()/2-30,getHeight()/2);

}
Summer 2018

1. List Attributes of <Applet> tag. [No. of times asked in gtu -2] [2]
Ans:- 1.align
2.alt
3.code
4.codebase
5.height
6.width
7.name
8.object
9.title
10.vspace
11.hspace

2. Write a short note on Applet life cycle. [No. of times asked in gtu -6] [3]
Ans:-
->In applet life cycle, there are 5 stages which are given in above figure and these stages are
represented by 5 methods.
->These methods called automatically by the browser whenever required for the execution of
the applet.
->No need to call these methods by the user.

Following are the methods of the applet life cycle


1.init() method
2.start() method
3.paint() method
4.stop() method
5.destroy() method

->All above mention methods is defined in java.applet.Applet class except paint method.
->paint() method is defined in java.component class.

1.init()
->It is used to initialize the applet. It is invoked only once.
->This method is called before all the other methods.
2.start()
->This method is automatically called after the browser called the init method.
->It is also called whenever the user returns the page containing the applet after having gone
off to other pages.
3.stop()
->An applet comes in idle state when its execution has been stopped either implicitly or
explicitly.
->An applet is implicitly stopped when we leave the page containing the currently running
applet.
->An applet is explicitly stopped when we call stop() method to stop execution.
->So, this method called repeatedly in the same applet.
4.destroy()
->This method is only called when the bowser shut down normally. It is called only once.
->It is called just before an applet is removed from the memory.
5.paint()
->It is invoked immediately after the start() method, and also any time the applet needs to
repaint itself in the browser.
->The paint method is actually inherited from the java.awt.
->It provides graphics class object that can be used for drawing oval, rectangle etc.

3. Give the difference between Applet and Application. [No. of times asked in gtu -5] [3]
Ans:-

Applet Application

1.An applet does not contains a main method. 1.Java application contains a main method.
2.Requires an internet connection to execute. 2.Does not requires an internet connection to
execute.
3.Part of Web page. 3.Stand alone application.
4.Requires a java compatible browser. 4.Can be run without a browser.
5.Entry point is init method. 5.Entry point is main method.
6.Use GUI interface provided by AWT or Swings. 6.Use stream I/O classes.
7.Generally used for GUI interfaces. 7.Generally used for console programs.
Winter 2017

1.What is applet? Which package is used in all applet programs? [2]


Ans:- Applet
->An applet is a small java program
->Applets are used for creating graphical programs.
->Java applets can perform interactive animations, immediate calculations, or other simple
tasks.
->java.applet.Applet package is used in all applet programs.

2. ____________ Method of Graphics class is used to print string in applet. [2]


Ans:- drawString

3. <PARAM> tag contains two attributes namely ________ and _______. [2]
Ans:- NAME AND VALUE

4. Explain <applet> tag with its necessary attributes. [No. of times asked in gtu -4] [3]
Ans:- <applet> tag
->The HTML <applet> tag specifies an applet.
->It is used for embedding a Java applet within an HTML document.
Applet tag has following attributes
1.code- name of class file.
2.width- width of applet.
3.height- height of applet.
4.codebase- applet’s directory
5.alt- alternate text if applet not available
6.name- name of the applet.
7.align(top, left, right, bottom)- justify applet with text
5. Explain any three methods of Graphics class. [3]
Ans:-

1. public abstract void setColor(Color c): is used to set the graphics current color to the
specified color.

2. public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the
specified width and height.

3.public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the
default color and specified width and height.

4. public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified
width and height.

5. public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the
default color and specified width and height.

6. Write an applet program to draw circle inside square. [4]


Ans:- Program:
package AJava;
import java.applet.Applet;
import java.awt.*;
public class circle_inside_square extends Applet
{
public void paint(Graphics g)
{
g.drawRect(100, 100, 200, 200);
g.drawOval(150,150 , 100, 100);
}
}
Summer-2017

1.List four methods of an applet. [No. of times asked in gtu -2] [2]
Ans:- 1.init()
2.start()
3.stop()
4.destroy()

2. Write an applet that sets the background color to yellow and draw circle of radius 50
pixels centered in the middle of the applet. Fill the circle with red color. [4]
Ans:- Program
package AJava;
import java.applet.Applet;
import java.awt.*;
public class demo extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.red);
g.fillOval(getWidth()/2-100, getHeight()/2-100, 200, 200);
}
}
Winter-2016

1 What is an applet? How does applet differ from applications? [3]


Ans:- Applet
->An applet is a small java program
->Applets are used for creating graphical programs.
->Java applets can perform interactive animations, immediate calculations, or other simple
tasks.
->An applet is a program written in the Java programming language that can be included in an
HTML page. An application is a standalone Java program that runs as a true application, outside
of a browser.

2. Explain how to set the background color within the applet area. [3]
Ans:-
-> In an applet, by default it has gray background when displayed in a browser.
->If you want to change the background color of an applet, then you can call the
setBackground(java.awt.Color) method.
-> Using the setBackground(java.awt.Color) method you can choose whichever color you want.
Example:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class BackgroundColorApplet extends Applet
{
public void init()
{
setBackgroundColor(Color.YELLOW);
}
}
3. Write a program to draw rectangle in the applet. [4]
Ans:- Program:
package AJava;
import java.applet.Applet;
import java.awt.*;
public class demo extends Applet
{
public void paint(Graphics g)
{
g.drawRect(100, 100, 500, 300);
}
}
Summer-2016

1. Write a program in JAVA to draw ten circles in the applet. [4]


Ans:- Program:
package Practicals;
import java.applet.Applet;
import java.awt.*;
public class p2 extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<10;i++)
{
g.drawOval(getWidth()/2-20, i*40, 40, 40);
}
}
}
Winter-2015

1.Describe process to design a webpage with <APPLET>tag. [3]


Ans:-
Summer-2015

1. What is the use of appletviewer utility in JAVA? [2]


Ans:-
-> AppletViewer is a standalone command-line program from Sun to run Java applets.
-> Appletviewer is generally used by developers for testing their applets before deploying them
to a website.

2. Write a program in JAVA to draw five red circles in the applet. [3]
Ans:- Program:
package Practicals;
import java.applet.Applet;
import java.awt.*;
public class p2 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
for(int i=0;i<5;i++)
{
g.drawOval(getWidth()/2-20, i*40, 40, 40);
}
}
}

S-ar putea să vă placă și