Question 4

(a) Explain various methods called during execution cycle of the applet.
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletLifeCycle" width=300 height=300>
</applet>
*/
public class AppletLifeCycle extends Applet
{
String msg1,msg2,msg3,msg4;
// called first
public void init()
{
// Initialization
msg1="init section begins";
}
// called second
public void start()
{
// start or resume execution
msg2="start section";
}
// called when applet stopped
public void stop()
{
// suspends execution
msg3="stop section";
}
// called when applet
public void destroy()
{
// execute shutdown activities
msg4="destroy section";
}
public void paint(Graphics g)
{
// redisplay content of window
g.drawString("Demo for basic methods of applet",20,40);
g.drawString(msg1,20,80);
g.drawString(msg2,20,90);
g.drawString(msg3,20,100);
g.drawString(msg4,20,120);
}
}
(b) Write a program to create a frame with exit capabilities. Handle events for mouse pressed, mouse released, mouse clicked and mouse dragged by displaying appropriate message describing the event at the coordinates where the event has taken place.
import java.awt.*;
import java.awt.event.*;
public class HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E extends Frame
{
String msg = "";
int mouseX=30;
int mouseY=30;
public HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E()
{
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
public static void main(String[] args)
{
HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f = new HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E();
f.setSize(new Dimension(300,300));
f.setTitle("HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E");
f.setVisible(true);
}
}
class MyMouseAdapter extends MouseAdapter
{
HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1;
public MyMouseAdapter(HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1)
{
this.f1 = f1;
}
public void mousePressed(MouseEvent me)
{
f1.mouseX = me.getX();
f1.mouseY = me.getY();
f1.msg = "Mouse Down at "+f1.mouseX+","+f1.mouseY;
f1.repaint();
}
public void mouseClicked(MouseEvent me)
{
f1.mouseX = me.getX();
f1.mouseY = me.getY();
f1.msg = "Mouse Clicked at "+f1.mouseX+","+f1.mouseY;
f1.repaint();
}
public void mouseReleased(MouseEvent me)
{
f1.mouseX = me.getX();
f1.mouseY = me.getY();
f1.msg = "Mouse Released at "+f1.mouseX+","+f1.mouseY;
f1.repaint();
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter
{
HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1;
public MyMouseMotionAdapter(HandlingMouseEventWindowEventMouseMotionEventInFramExampleH2E f1)
{
this.f1 = f1;
}
public void mouseDragged(MouseEvent me)
{
f1.mouseX = me.getX();
f1.mouseY = me.getY();
f1.msg = "Mouse Dragged at "+f1.mouseX+","+f1.mouseY;
f1.repaint();
}
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
(b) Write a complete program to create a frame for providing GUI to implement a stack for storing integer numbers. There are two buttons called PUSH & POP and a text field. Clicking of button PUSH pushes the number entered in the text field onto the stack. The click of button POP pops an element from the stack and displays that in the text field.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class stackUsingJFrameInJavaPushPopExampleH2E
{
JLabel lbl;
JButton pop,push;
JTextField text;
String stackArr[] = new String[10];
int top=-1;
CopyOfstackUsingJFrameInJavaPushPopExampleH2E()
{
JFrame frm = new JFrame("Stack Implementation In Frame - Help2Engg");
frm.setLayout(new FlowLayout());
frm.setSize(300,300);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
push = new JButton("PUSH");
pop = new JButton("POP");
text = new JTextField(12);
push.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
++top;
if(top < 10)
{
stackArr[top] = text.getText();
lbl.setText("Push Element : "+text.getText());
}
else
lbl.setText("Stack is Full");
}
}
);
pop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(top >= 0)
{
lbl.setText("Pop Element : "+stackArr[top]);
--top;
}
else
lbl.setText("Stack is Empty");
}
}
);
frm.add(text);
frm.add(push);
frm.add(pop);
lbl = new JLabel("Press a Button");
frm.add(lbl);
frm.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new CopyOfstackUsingJFrameInJavaPushPopExampleH2E();
}
}
);
}
}

Make Comments..!!


Oops!! No posts from user.

Download Android App