You are on page 1of 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