Sant Longowal Institute of Engineering & Technology: Assignment of Java Programing Language
Sant Longowal Institute of Engineering & Technology: Assignment of Java Programing Language
Asst.prof of CSE
REG.NO: 1712889 DATE: 14/04/2020
DEPT: DCSCDE
BATCH: 2K17
PROGRAM:
import java.awt.*;
import java.applet.*;
/*
<applet code="Exp10_1" width = "200" height = "200">
</applet>
*/
String msg;
msg = this.getParameter("str");
/*
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
repaint();
}
// draw a ellipse
g.drawOval(100, 100, 150, 100);
}
}
OUTPUT:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class FilledRectangleExample extends Applet{
setForeground(Color.red);
g.fillRect(100,100,50,50);
}
}
OUTPUT:
Applets are used to make the web site more dynamic and entertaining.
Important points :
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.
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.
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.