Ajp Exp 11 Op

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy