We will be handling various mouse event and mouse motion events in this implementation.
We gonna refresh ours green days of disney time when Micky mouse used to hang around with his Mini lol
Interfaces and the function -
MouseListener -
mouseClicked() , mousePressed() , mouseEntered() , mouseReleased() , mouseExit()
MouseMotionListener -
mouseDragged() , mouseMoved()
Main Class
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args){
GUI go = new GUI();
//defines the operation done on closing the app , programme terminates
//exit the JFrame
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the frame size
go.setSize(275, 275);
//set visibility
go.setVisible(true);
}
}
Mouse Listener class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI extends JFrame {
private JPanel mousePanel;
private JLabel statusBar;
// constructor
public GUI() {
super("Micky the Mouse");
mousePanel = new JPanel();
mousePanel.setBackground(Color.WHITE);
add(mousePanel, BorderLayout.CENTER);
statusBar = new JLabel("Micky's sweet home");
add(statusBar, BorderLayout.SOUTH);
HandlerClass handler = new HandlerClass();
// Add the specified mouse listener to receive mouse
// events and motion events from this component.
mousePanel.addMouseListener(handler);
mousePanel.addMouseMotionListener(handler);
}
private class HandlerClass implements MouseListener, MouseMotionListener {
// mouse motion event
// Invoked when a mouse button is pressed on a component and then
// dragged
@Override
public void mouseDragged(MouseEvent e) {
statusBar.setText("Mouse dragged");
}
// Invoked when the mouse cursor has been moved onto a
// component but no buttons have been pushed
@Override
public void mouseMoved(MouseEvent e) {
statusBar.setText("Micky Mouse movin");
}
// mouse event
// Invoked when the mouse button has been clicked
// (pressed and released) on a component
@Override
public void mouseClicked(MouseEvent e) {
statusBar.setText(String.format("Micky mouse clicked at %d , %d ",
e.getX(), e.getY()));
}
// Invoked when a mouse button has been pressed on a component
@Override
public void mousePressed(MouseEvent e) {
statusBar.setText("micky mouse pressed");
}
// Invoked when a mouse button has been released on a component.
@Override
public void mouseReleased(MouseEvent e) {
statusBar.setText("u released micky Mouse ");
}
// Invoked when the mouse enters a component.
@Override
public void mouseEntered(MouseEvent e) {
statusBar.setText("MMicky mouse entered the arena");
mousePanel.setBackground(Color.BLACK);
}
@Override
public void mouseExited(MouseEvent e) {
statusBar.setText("Micky Mouse went to sleep zzzzzzzZZZ ");
mousePanel.setBackground(Color.WHITE);
}
}
}
We gonna refresh ours green days of disney time when Micky mouse used to hang around with his Mini lol
MouseListener -
mouseClicked() , mousePressed() , mouseEntered() , mouseReleased() , mouseExit()
MouseMotionListener -
mouseDragged() , mouseMoved()
Main Class
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args){
GUI go = new GUI();
//defines the operation done on closing the app , programme terminates
//exit the JFrame
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the frame size
go.setSize(275, 275);
//set visibility
go.setVisible(true);
}
}
Mouse Listener class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI extends JFrame {
private JPanel mousePanel;
private JLabel statusBar;
// constructor
public GUI() {
super("Micky the Mouse");
mousePanel = new JPanel();
mousePanel.setBackground(Color.WHITE);
add(mousePanel, BorderLayout.CENTER);
statusBar = new JLabel("Micky's sweet home");
add(statusBar, BorderLayout.SOUTH);
HandlerClass handler = new HandlerClass();
// Add the specified mouse listener to receive mouse
// events and motion events from this component.
mousePanel.addMouseListener(handler);
mousePanel.addMouseMotionListener(handler);
}
private class HandlerClass implements MouseListener, MouseMotionListener {
// mouse motion event
// Invoked when a mouse button is pressed on a component and then
// dragged
@Override
public void mouseDragged(MouseEvent e) {
statusBar.setText("Mouse dragged");
}
// Invoked when the mouse cursor has been moved onto a
// component but no buttons have been pushed
@Override
public void mouseMoved(MouseEvent e) {
statusBar.setText("Micky Mouse movin");
}
// mouse event
// Invoked when the mouse button has been clicked
// (pressed and released) on a component
@Override
public void mouseClicked(MouseEvent e) {
statusBar.setText(String.format("Micky mouse clicked at %d , %d ",
e.getX(), e.getY()));
}
// Invoked when a mouse button has been pressed on a component
@Override
public void mousePressed(MouseEvent e) {
statusBar.setText("micky mouse pressed");
}
// Invoked when a mouse button has been released on a component.
@Override
public void mouseReleased(MouseEvent e) {
statusBar.setText("u released micky Mouse ");
}
// Invoked when the mouse enters a component.
@Override
public void mouseEntered(MouseEvent e) {
statusBar.setText("MMicky mouse entered the arena");
mousePanel.setBackground(Color.BLACK);
}
@Override
public void mouseExited(MouseEvent e) {
statusBar.setText("Micky Mouse went to sleep zzzzzzzZZZ ");
mousePanel.setBackground(Color.WHITE);
}
}
}
No comments:
Post a Comment