Output

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

import java.applet.

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

public class MouseDemo extends Applet implements MouseListener {


Label l;
public void init() {
setLayout(null);
l = new Label("Hello Mouse");
l.setBounds(50, 50, 200, 100);
add(l);
addMouseListener(this);
}
@Override
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed; # of clicks: " + e.getClickCount() + " at position " + e.getX() + ", " + e.getY());
}

@Override
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released; # of clicks: " + e.getClickCount());
}
@Override
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
@Override
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
@Override
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked; # of clicks: " + e.getClickCount());
}
}
/* <APPLET CODE="MouseDemo.class" WIDTH=300 HEIGHT=200> </APPLET> */

Output :
Exercise No 1 :
import java.applet.Applet; @Override
import java.awt.*; public void mousePressed(MouseEvent e) {
import java.awt.event.*; panel.setBackground(Color.GREEN);
public class BackgroundColorChanger extends Applet l.setText("Green");
implements MouseListener { }
private Panel panel; @Override
private Label l; public void mouseReleased(MouseEvent e) {
public void init() { panel.setBackground(Color.YELLOW);
setLayout(null); l.setText("Yellow");
l = new Label("Hello Mouse", Label.CENTER); }
l.setBounds(50, 50, 200, 100); @Override
add(l); public void mouseEntered(MouseEvent e) {
panel = new Panel(); panel.setBackground(Color.BLUE);
panel.setBackground(Color.WHITE); l.setText("Blue");
panel.setBounds(0, 0, getWidth(), getHeight()); }
panel.addMouseListener(this); @Override
add(panel); public void mouseExited(MouseEvent e) {
} panel.setBackground(Color.RED);
@Override l.setText("Red");
public void mouseClicked(MouseEvent e) { }
panel.setBackground(Color.CYAN); }
l.setText("Cyan"); /* <APPLET CODE="BackgroundColorChanger.class"
} WIDTH=400 HEIGHT=300> </APPLET> */

Output :
Exercise No 2:
import javax.swing.*;
import java.awt.event.*;

public class ClickCounter extends JFrame implements MouseListener {


private JLabel label;
private int clickCount = 0;
public ClickCounter() {
setTitle("Click Counter");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
label = new JLabel("Clicks: 0", JLabel.CENTER);
label.setBounds(50, 50, 200, 100);
add(label);
addMouseListener(this);
}

@Override
public void mouseClicked(MouseEvent e) {
clickCount++;
label.setText("Clicks: " + 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) {
ClickCounter frame = new ClickCounter();
frame.setVisible(true);
}
}

Output :
Exercise No 3:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MouseMotionDemo extends JFrame implements MouseMotionListener {


private JLabel coordinatesLabel;

public MouseMotionDemo() {
setTitle("Mouse Motion Demo");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
coordinatesLabel = new JLabel("Move the mouse inside the window", JLabel.CENTER);
add(coordinatesLabel, BorderLayout.CENTER);
addMouseMotionListener(this);
}
@Override
public void mouseDragged(MouseEvent e) {
coordinatesLabel.setText("Dragging at: (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseMoved(MouseEvent e) {
coordinatesLabel.setText("Moving at: (" + e.getX() + ", " + e.getY() + ")");
}
public static void main(String[] args) {
MouseMotionDemo frame = new MouseMotionDemo();
frame.setVisible(true);
}
}

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