Exp 11

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

EXP 11

X.Program Code

1. Ans

OUTPUT:

XII Exercise

1. Ans
CODE:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ColorChangeApplet extends Applet implements MouseListener {


public void init() {
setSize(400, 300);
addMouseListener(this);
}

@Override
public void mousePressed(MouseEvent e) {
setBackground(Color.RED);
}

@Override
public void mouseReleased(MouseEvent e) {
setBackground(Color.GREEN);
}

@Override
public void mouseEntered(MouseEvent e) {
setBackground(Color.YELLOW);
}

@Override
public void mouseExited(MouseEvent e) {
setBackground(Color.BLUE);
}

@Override
public void mouseClicked(MouseEvent e) {
setBackground(Color.CYAN);
}
}
HTML CODE:

<!DOCTYPE html>

<html>

<head>

<title>Color Change Applet</title>

</head>

<body>

<h1>Mouse Event Color Change Applet</h1>

<applet code="ColorChangeApplet.class" width="400" height="300">

Your browser does not support Java Applets.

</applet>

</body>

</html>

OUTPUT:
XIII. Exercise

2. Ans

CODE:

import javax.swing.*;

import java.awt.event.*;

public class ClickCounter extends JFrame implements MouseListener {

private int clickCount = 0;

private JLabel label;

public ClickCounter() {

setTitle("Click Counter");

setSize(300, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);

label = new JLabel("Click count: " + clickCount);

label.setBounds(50, 80, 200, 30);

add(label);

addMouseListener(this);

setVisible(true);

@Override

public void mouseClicked(MouseEvent e) {

clickCount++;

label.setText("Click count: " + clickCount);

}
@Override

public void mousePressed(MouseEvent e) {}

@Override

public void mouseReleased(MouseEvent e) {}

@Override

public void mouseEntered(MouseEvent e) {}

@Override

public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {

new ClickCounter();

OUTPUT:
XIII.Exercise

3. Ans

CODE:
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MouseMotionDemo extends JFrame implements MouseMotionListener {

private JLabel label;

public MouseMotionDemo() {

setTitle("Mouse Motion Demo");

setSize(400, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(null);

label = new JLabel("Move or drag the mouse in this window");

label.setBounds(50, 50, 300, 30);

add(label);

addMouseMotionListener(this);

setVisible(true);

@Override

public void mouseDragged(MouseEvent e) {

label.setText("Mouse dragged at: (" + e.getX() + ", " + e.getY() + ")");

}
@Override

public void mouseMoved(MouseEvent e) {

label.setText("Mouse moved at: (" + e.getX() + ", " + e.getY() + ")");

public static void main(String[] args) {

new MouseMotionDemo();

OUTPUT:

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