0% found this document useful (0 votes)
168 views8 pages

Develop A Java Program To Demonstrate Applet Life Cycle

The document describes a Java program that demonstrates the applet lifecycle by creating an Applet class called LifeCycleNew that overrides the init(), start(), stop(), and destroy() methods. It prints the name of each method as it is called to the applet window. The program also shows how to add menus to a Java Frame by creating a MenuBar and adding Menu items for "File" and "Help".

Uploaded by

Vds Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
168 views8 pages

Develop A Java Program To Demonstrate Applet Life Cycle

The document describes a Java program that demonstrates the applet lifecycle by creating an Applet class called LifeCycleNew that overrides the init(), start(), stop(), and destroy() methods. It prints the name of each method as it is called to the applet window. The program also shows how to add menus to a Java Frame by creating a MenuBar and adding Menu items for "File" and "Help".

Uploaded by

Vds Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 8

Develop a Java Program to demonstrate Applet Life Cycle

/*
<applet code="LifeCycleNew.class" width=300 height=300></applet>
*/

import java.applet.Applet;
import java.awt.*;

public class LifeCycleNew extends Applet


{
String output = "";
public void init()
{
this.setBackground(Color.yellow);
this.setForeground(Color.red);
output = output + "init";
}
public void start()
{
output = output + "start";
}

public void stop()


{
output = output + "stop";
}

public void destroy()


{
output = output + "destroy";
System.out.println("Destroying");
}

public void paint(Graphics g)


{
Font f = new Font("TimesNewRoman",Font.BOLD,25);
g.setFont(f);
g.drawString(output, 100, 100);
}

}
E:\Java\AWT\Nagarjuna>javac LifeCycleNew.java

E:\Java\AWT\Nagarjuna>appletviewer LifeCycleNew.java
Develop a Java Program to demonstrate Mouse Event Handling(using Applet)
/* <body>
* <applet code="MouseEventHandlingDemo.class" width=200 height=300></applet>
* </body>*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class MouseEventHandlingDemo extends Applet implements MouseListener,


MouseMotionListener {

public void init()


{
this.addMouseListener(this);
this.addMouseMotionListener(this);
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.BLUE);
int x = e.getX();
int y = e.getY();
print(x,y);

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.RED);
int x = e.getX();
int y = e.getY();
print(x,y);

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.GREEN);
int x = e.getX();
int y = e.getY();
print(x,y);

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.YELLOW);
int x = e.getX();
int y = e.getY();
print(x,y);

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.BLACK);
int x = e.getX();
int y = e.getY();
print(x,y);

}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.WHITE);
int x = e.getX();
int y = e.getY();
print(x,y);

@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
setBackground(Color.MAGENTA);
int x =e.getX();
int y = e.getY();
print(x,y);

private void print(int x, int y) {


// TODO Auto-generated method stub
showStatus("X coordinate is:"+x+"Y coordinate is:"+y);

}
Develop a Java Program to menus to a Frame (MenuDemo.java)

import java.awt.*;

class MenuDemo extends Frame


{
MenuDemo()
{
/* Creating a Menu bar */
MenuBar mBar = new MenuBar();
Frame f = new Frame();
f.setMenuBar(mBar);
f.setVisible(true);
f.setSize(400,400);

/* Creating Menu File */


Menu File = new Menu("File");
MenuItem i1,i2;
File.add(new MenuItem("NEw"));
File.add(new MenuItem("Open"));
mBar.add(File);

/* Creating Menu Help */


Menu help = new Menu("Help");
MenuItem i3,i4;
help.add(new MenuItem("Welcome"));
help.add(new MenuItem("About"));
mBar.add(help);

}
public static void main(String [] args)
{
MenuDemo obj = new MenuDemo();
}
}
Output:

E:\Java\AWT\Nagarjuna>javac MenuDemo.java

E:\Java\AWT\Nagarjuna>java MenuDemo

You might also like