0% found this document useful (0 votes)
31 views6 pages

Ajp Exp 11 Op

Uploaded by

muleayush848
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Ajp Exp 11 Op

Uploaded by

muleayush848
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

X.

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.*;

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


public class MouseDemo extends Applet implements MouseListener {
Label l;
Label infoLabel;
public void init() {
setLayout(null);
l = new Label("Hello Mouse");
l.setBounds(50, 100, 200, 50);
add(l);
addMouseListener(this);
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed no. of clicks: " + e.getClickCount() + " at position " + e.getX() + "," +
e.getY());
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released; # of clicks: " + e.getClickCount());
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked (# of clicks: " + e.getClickCount() + ")");
}
}
XIII. Exercise:
1. Write a program to change the background color of Applet when user performs events using mouse.

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);
}
}

You might also like