0% found this document useful (0 votes)
65 views10 pages

Sant Longowal Institute of Engineering & Technology: Assignment of Java Programing Language

This document contains Java applet programs submitted by a student named Kabita Kumari for an assignment. It includes 4 programs: 1) An applet that displays a string, 2) An applet that takes two numbers as input and displays their sum, 3) Applets that draw lines, rectangles, a rounded rectangle, and filled rectangles, 4) Explanations of the applet lifecycle and differences between applets and applications. The programs demonstrate basic applet functionality and concepts in Java programming.

Uploaded by

kabita kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views10 pages

Sant Longowal Institute of Engineering & Technology: Assignment of Java Programing Language

This document contains Java applet programs submitted by a student named Kabita Kumari for an assignment. It includes 4 programs: 1) An applet that displays a string, 2) An applet that takes two numbers as input and displays their sum, 3) Applets that draw lines, rectangles, a rounded rectangle, and filled rectangles, 4) Explanations of the applet lifecycle and differences between applets and applications. The programs demonstrate basic applet functionality and concepts in Java programming.

Uploaded by

kabita kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

SANT LONGOWAL INSTITUTE OF

ENGINEERING & TECHNOLOGY

ASSIGNMENT OF JAVA PROGRAMING


LANGUAGE.
SUBMITTED BY: SUBMITTED TO:

NAME: KABITA KUMARI MR. NAVNEET GARG

Asst.prof of CSE
REG.NO: 1712889 DATE: 14/04/2020
DEPT: DCSCDE
BATCH: 2K17

Q.1.Write a program to display string on applet window.

PROGRAM:

import java.awt.*;

import java.applet.*;

/*
<applet code="Exp10_1" width = "200" height = "200">

<param name="str" value="Java Applet">

</applet>

*/

public class Exp10_1 extends Applet

String msg;

public void init()

msg = this.getParameter("str");

setFont(new Font("Times New Roman", Font.BOLD + Font.ITALIC, 14));

public void paint(Graphics g)

g.drawString(msg, 50, 100);

/*

OUTPUT!!!
Q.2 Write an applet which takes two numbers from the user into each text field box
displayed in the applet area and compute the sum of these two numbers and displayed
the result.import java.awt.*;

import java.awt.event.*;
import java.applet.*;
public class Q2 extends Applet implements ActionListener
{
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
TextField t3 = new TextField(10);
Label l1 = new Label("FIRST NO=:");
Label l2 = new Label("SECOND NO:");
Label l3 = new Label("SUM:");
Button b = new Button("ADD");
public void init()
{
t1.setForeground(Color = Red);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b)
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
t3.setText(" " + (n1 + n2));
}
}
}
Output:

Q.3 write an applet code that draws three lines, a ractangle, a filled
ractangle, a rounded ractangle.
program: A ractangle
import java.awt.*;
import java.applet.*;
public class RectanglesDrawing extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawRect(50, 80, 150, 100);
g.setColor(Color.magenta);
g.fillRect(230, 80, 150, 100);
}
}

Output

PROGRAM: A ROUNDED RACTANGLE


import java.awt.*;
import javax.swing.*;
public class ellipse extends JApplet {
public void init()
{
// set size
setSize(400, 400);

repaint();
}

// paint the applet


public void paint(Graphics g)
{
// set Color for rectangle
g.setColor(Color.red);

// draw a ellipse
g.drawOval(100, 100, 150, 100);
}
}
OUTPUT:

PROGRAM: A FILLED RACTANGLE

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class FilledRectangleExample extends Applet{

public void paint(Graphics g){

setForeground(Color.red);
g.fillRect(100,100,50,50);
}

}
OUTPUT:

Q.4 life cycle of applet.


An applet is a Java program that can be embedded into a web page. It runs inside the web
browser and works at client side. An applet is embedded in an HTML page using the
APPLET or OBJECT tag and hosted on a web server.

Applets are used to make the web site more dynamic and entertaining.

Important points :

All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.Applets


are not stand-alone programs.

Instead, they run within either a web browser or an applet viewer.

JDK provides a standard applet viewer tool called applet viewer.In general, execution of an
applet does not begin at main() method.
Output of an applet window is not performed by System.out.println(). Rather it is handled
with various AWT methods, such as drawString().

Diagram:

It is important to understand the order in which the various methods shown in the above
image are called. When an applet begins, the following methods are called, in this sequence:

init( )
start( )
paint( )
When an applet is terminated, the following sequence of method calls takes place:

stop( )
destroy( )
Let’s look more closely at these methods.

init( ) : The init( ) method is the first method to be called. This is where you should initialize
variables. This method is called only once during the run time of your applet.

start( ) : The start( ) method is called after init( ). It is also called to restart an applet after it
has been stopped. Note that init( ) is called once i.e. when the first time an applet is loaded
whereas start( ) is called each time an applet’s HTML document is displayed onscreen. So, if
a user leaves a web page and comes back, the applet resumes execution at start( ).

paint( ) : The paint( ) method is called each time an AWT-based applet’s output must be
redrawn. This situation can occur for several reasons. For example, the window in which the
applet is running may be overwritten by another window and then uncovered. Or the applet
window may be minimized and then restored.

paint( ) is also called when the applet begins execution. Whatever the cause, whenever the
applet must redraw its output, paint( ) is called.

Q.5 difference between applet and application.

1.Definition of Application and Applet – Applets are feature rich application programs that
are specifically designed to be executed within an HTML web document to execute small
tasks or just part of it. Java applications, on the other hand, are stand-alone programs that
are designed to run on a stand-alone machine without having to use a browser.

2.Execution of Application and Applet– Applications require main method() to execute the
code from the command line, whereas an applet does not require main method() for
execution. An applet requires an HTML file before its execution. The browser, in fact,
requires a Java plugin to run an applet.

3.Compilation of Application and Applet–Application programs are compiled using the


“javac” command and further executed using the java command. Applet programs, on the
other hand, are also compiled using the “javac” command but are executed either by using
the “appletviewer” command or using the web browser.

4.Security Access of Application and Applet – Java application programs can access all the
resources of the system including data and information on that system, whereas applets
cannot access or modify any resources on the system except only the browser specific
services.

5.Restrictions of Application and Applet – Unlike applications, applet programs cannot be


run independently, thus require highest level of security. However, they do not require any
specific deployment procedure during execution. Java applications, on the other hand, run
independently and do not require any security as they are trusted.

You might also like