Practical No.
10
Aim :- Write a program to demonstrate use of special keys (for example:
F1,pgdown,etc) on Applet Window using KeyEvent class.
Program :-
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class SimpleSpecialKeyApplet extends Applet implements KeyListener {
String message = "";
public void init() {
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_F1) {
message = "F1 Key Pressed";
} else if (e.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
message = "Page Down Key Pressed";
} else if (e.getKeyCode() == KeyEvent.VK_PAGE_UP) {
message = "Page Up Key Pressed";
} else {
message = "Other Key Pressed";
}
repaint();
}
public void keyReleased(KeyEvent e) {
message = "Key Released";
repaint();
}
public void keyTyped(KeyEvent e) {}
public void paint(Graphics g) {
g.drawString(message, 50, 100);
}
}
}
/* <applet code=" SimpleSpecialKeyApplet" width=400 height=400></applet> */
Output : -
Conclusion : -
In this practical , I successfully perform KeyEvents in an applet.
Marks Obtained Dated signature
of Teacher
Process Product Total (50)
Related (35) Related (15)
v Practical No. 11
Aim:- Write a program to demonstrate MouseEvent on applet Window.
Program :-
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class MouseEventsApplet extends Applet implements MouseListener,
MouseMotionListener {
String message = "Move or click the mouse.";
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}
public void mouseClicked(MouseEvent e) {
message = "Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")";
repaint();
}
public void mousePressed(MouseEvent e) {
message = "Mouse Pressed at (" + e.getX() + ", " + e.getY() + ")";
repaint();
}
public void mouseReleased(MouseEvent e) {
message = "Mouse Released at (" + e.getX() + ", " + e.getY() + ")";
repaint();
}
public void mouseEntered(MouseEvent e) {
message = "Mouse Entered the Applet Area";
repaint();
}
public void mouseExited(MouseEvent e) {
message = "Mouse Exited the Applet Area";
repaint();
}
public void mouseDragged(MouseEvent e) {
message = "Mouse Dragged at (" + e.getX() + ", " + e.getY() + ")";
repaint();
}
public void mouseMoved(MouseEvent e) {
message = "Mouse Moved at (" + e.getX() + ", " + e.getY() + ")";
repaint();
}
public void paint(Graphics g) {
g.drawString(message, 20, 20);
}
}
/* <applet code="MouseEventsApplet" width=400 height=400></applet> */
Output : -
Conclusion : -
In this practical , I successfully perform MouseEvent in an applet.
Marks Obtained Dated signature
of Teacher
Process Product Total (50)
Related (35) Related (15)
Practical No. 12
Aim:- Write a program to perform basic arithmetic operation on two numbers using TextField
and button to handle ActionEvent in an Applet.
Program :-
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class ArithmeticApplet extends Applet implements ActionListener {
TextField num1Field, num2Field, resultField;
Button addButton, subtractButton, multiplyButton, divideButton;
Label resultLabel;
public void init() {
setLayout(null);
num1Field = new TextField();
num2Field = new TextField();
resultField = new TextField();
resultField.setEditable(false);
addButton = new Button("Add");
subtractButton = new Button("Subtract");
multiplyButton = new Button("Multiply");
divideButton = new Button("Divide");
resultLabel = new Label("Result:");
num1Field.setBounds(50, 50, 100, 30);
num2Field.setBounds(50, 100, 100, 30);
resultLabel.setBounds(50, 150, 60, 30);
resultField.setBounds(120, 150, 100, 30);
addButton.setBounds(50, 200, 60, 30);
subtractButton.setBounds(120, 200, 60, 30);
multiplyButton.setBounds(190, 200, 60, 30);
divideButton.setBounds(260, 200, 60, 30);
add(num1Field);
add(num2Field);
add(resultLabel);
add(resultField);
add(addButton);
add(subtractButton);
add(multiplyButton);
add(divideButton);
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = 0;
if (e.getSource() == addButton) {
result = num1 + num2;
} else if (e.getSource() == subtractButton) {
result = num1 - num2;
} else if (e.getSource() == multiplyButton) {
result = num1 * num2;
} else if (e.getSource() == divideButton) {
if (num2 != 0) {
result = num1 / num2;
} else {
resultField.setText("Error");
return;
}
}
resultField.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
resultField.setText("Invalid input");
}
}
}
/* <applet code="ArithmeticApplet" width=400 height=400></applet> */
Output :-
Conclusion :-
I successfully perform arithmetic operation using an applet.
Marks Obtained Dated signature
of Teacher
Process Product Total (50)
Related (35) Related (15)
Practical No. 13
Aim:- Write a program to accept keyboard input to show the pressed/released
status of each key on Applet Window.
Program :-
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class CheckboxItemEventApplet extends Applet implements ItemListener {
Checkbox option1, option2, option3;
Label statusLabel;
public void init() {
setLayout(null);
option1 = new Checkbox("Apple");
option2 = new Checkbox("Banana");
option3 = new Checkbox("Orange");
statusLabel = new Label("Selected: None");
option1.setBounds(50, 50, 100, 30);
option2.setBounds(50, 100, 100, 30);
option3.setBounds(50, 150, 100, 30);
statusLabel.setBounds(50, 200, 200, 30);
add(option1);
add(option2);
add(option3);
add(statusLabel);
option1.addItemListener(this);
option2.addItemListener(this);
option3.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
String selectedOptions = "Selected: ";
if (option1.getState()) {
selectedOptions += "Apple ";
}
if (option2.getState()) {
selectedOptions += "banana ";
}
if (option3.getState()) {
selectedOptions += "Orange ";
}
if (selectedOptions.equals("Selected: ")) {
selectedOptions = "Selected: None";
}
statusLabel.setText(selectedOptions);
}
}
/* <applet code="CheckboxItemEventApplet" width=400 height=400></applet */
Output :-
Conclusion :-
In this practical , I successfully perform Some events on Checkbox on applet window.
Marks Obtained Dated signature
of Teacher
Process Product Total (50)
Related (35) Related (15)
Practical No. 14
Aim:- Write a program to accept keyboard input to show the pressed/released
status of each key on Applet Window.
Program :-
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class LoginApplet extends Applet implements TextListener {
TextField usernameField, passwordField;
Label usernameLabel, passwordLabel, statusLabel;
public void init() {
setLayout(null);
usernameField = new TextField();
passwordField = new TextField();
usernameLabel = new Label("Username:");
passwordLabel = new Label("Password:");
statusLabel = new Label("Username and Password");
passwordField.setEchoChar('*');
usernameLabel.setBounds(50, 30, 80, 30);
usernameField.setBounds(140, 30, 200, 30);
passwordLabel.setBounds(50, 80, 80, 30);
passwordField.setBounds(140, 80, 200, 30);
statusLabel.setBounds(50, 130, 300, 30);
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(statusLabel);
usernameField.addTextListener(this);
passwordField.addTextListener(this);
}
public void textValueChanged(TextEvent e) {
// Get text from text fields
String username = usernameField.getText();
String password = passwordField.getText();
statusLabel.setText("Username: " + username + " | Password: " + password);
}
}
/* <applet code="LoginApplet" width=400 height=400></applet> */
Output :-
Conclusion :-
In this practical , I successfully perform make the simple login page on applet
window.
Marks Obtained Dated signature
of Teacher
Process Product Total (50)
Related (35) Related (15)