Ajp Exp 11 Op
Ajp Exp 11 Op
Ajp Exp 11 Op
Practical Code:
1. Debug the following Program code and write the output (Refer Manual pg. no. 59)
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/* <applet code="ColorChangeApplet.class" width="300" height="200"></applet> */
public class ColorChangeApplet extends Applet implements MouseListener {
Label l;
public void init() {
setLayout(null);
setBackground(Color.WHITE);
l = new Label("Click to change background color");
l.setBounds(50, 50, 200, 30);
add(l);
addMouseListener(this);
}
public void mousePressed(MouseEvent e) {
setBackground(Color.RED);
}
public void mouseReleased(MouseEvent e) {
setBackground(Color.GREEN);
}
public void mouseEntered(MouseEvent e) {
setBackground(Color.YELLOW);
}
public void mouseExited(MouseEvent e) {
setBackground(Color.BLUE);
}
public void mouseClicked(MouseEvent e) {
setBackground(Color.WHITE);
}
}
2. Write a program to count the number of clicks performed by the user in a Frame window.
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ClickCounter {
public static void main(String[] args) {
JFrame frame = new JFrame("Click Counter");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new java.awt.FlowLayout());
JLabel clickLabel = new JLabel("Clicks: 0");
frame.add(clickLabel);
frame.addMouseListener(new MouseAdapter() {
private int clickCount = 0;
@Override
public void mouseClicked(MouseEvent e) {
clickCount++;
clickLabel.setText("Clicks: " + clickCount);
}
});
frame.setVisible(true);
}
}
3. Write a program to demonstrate the use of mouseDragged and mouseMoved method of MouseMotionListener.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
public class MouseMotionDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Mouse Motion Demo");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel statusLabel = new JLabel("Move or drag the mouse");
frame.add(statusLabel);
frame.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
statusLabel.setText("Mouse Moved at: (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseDragged(MouseEvent e) {
statusLabel.setText("Mouse Dragged at: (" + e.getX() + ", " + e.getY() + ")");
}
});
frame.setVisible(true);
}
}