Output
Output
Output
Applet;
import java.awt.*;
import java.awt.event.*;
@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.*;
@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 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 :