Demonstrate The Mouse Event Handlers

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

// Demonstrate the mouse event handlers.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MouseEvents" width=300 height=100>
</applet>
*/
public class MouseEvents extends Applet
implements MouseListener, MouseMotionListener
{
String msg = "";
int mouseX = 0, mouseY = 0; // coordinates of mouse
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}

// Handle mouse clicked.


public void mouseClicked(MouseEvent me)
{
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
repaint();
}

// Handle mouse entered.


public void mouseEntered(MouseEvent me)
{
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
repaint();
}

// Handle mouse exited


public void mouseExited(MouseEvent me)
{
// save coordinates
mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}

// Handle button pressed.


public void mousePressed(MouseEvent me)
{
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Down";
repaint();
}

// Handle button released.


public void mouseReleased(MouseEvent me)
{
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "Up";
repaint();
}

// Handle mouse dragged.


public void mouseDragged(MouseEvent me)
{
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint();
}

// Handle mouse moved.


public void mouseMoved(MouseEvent me)
{
// show status
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}

// Display msg in applet window at current X,Y location.


public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}

// Demonstrate the key event handlers.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="SimpleKey" width=300 height=100>
</applet>
*/
public class SimpleKey extends Applet
implements KeyListener
{
String msg = "";
int X = 10, Y = 20; // output coordinates
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint();
}
// Display keystrokes.
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}

// Demonstrate some virtual key codes.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="KeyEvents" width=300 height=100>
</applet>
*/
public class KeyEvents extends Applet
implements KeyListener
{
String msg = "";
int X = 10, Y = 20; // output coordinates
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
int key = ke.getKeyCode();
switch(key)
{
case KeyEvent.VK_F1:
msg += "<F1>";
break;
case KeyEvent.VK_F2:
msg += "<F2>";
break;
case KeyEvent.VK_F3:
msg += "<F3>";
break;
case KeyEvent.VK_PAGE_DOWN:
msg += "<PgDn>";
break;
case KeyEvent.VK_PAGE_UP:
msg += "<PgUp>";
break;
case KeyEvent.VK_LEFT:
msg += "<Left Arrow>";
break;
case KeyEvent.VK_RIGHT:
msg += "<Right Arrow>";
break;
}
repaint();
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint();
}
// Display keystrokes.
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}

//Demonstrate Mouse Listener


import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener
{
Label l;
MouseListenerExample()
{
addMouseListener(this);

l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{
l.setText("Mouse Entered");
}

public void mouseExited(MouseEvent e)


{
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
l.setText("Mouse Released");
}
public static void main(String[] args)
{
new MouseListenerExample();
}
}

//Demonstrate Mouse Listener


import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample2 extends Frame implements MouseListener
{
MouseListenerExample2()
{
addMouseListener(this);

setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

public static void main(String[] args)


{
new MouseListenerExample2();
}
}

//Demonstrate MouseMotionListener
import java.awt.*;
import java.awt.event.*;
public class MouseMotionListenerExample extends Frame implements
MouseMotionListener
{
MouseMotionListenerExample()
{
addMouseMotionListener(this);

setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {}

public static void main(String[] args)


{
new MouseMotionListenerExample();
}
}

//Demonstrate MouseMotionListener
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class MouseMotionListenerExample2 extends Frame implements
MouseMotionListener
{
Label l;
Color c=Color.BLUE;
MouseMotionListenerExample2()
{
l=new Label();
l.setBounds(20,40,100,20);
add(l);

addMouseMotionListener(this);

setSize(400,400);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
{
l.setText("X="+e.getX()+", Y="+e.getY());
Graphics g=getGraphics();
g.setColor(Color.RED);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e)
{
l.setText("X="+e.getX()+", Y="+e.getY());
}
public static void main(String[] args)
{
new MouseMotionListenerExample2();
}
}

//Demonstrate KeyListener
// importing awt libraries
import java.awt.*;
import java.awt.event.*;
public class KeyListenerExample extends Frame implements KeyListener
{
Label l;
TextArea area;

KeyListenerExample()
{
l = new Label();
l.setBounds (20, 50, 100, 20);

area = new TextArea();


area.setBounds (20, 80, 300, 300);

area.addKeyListener(this);

add(l);
add(area);

setSize (400, 400);


setLayout (null);
setVisible (true);
}
public void keyPressed (KeyEvent e)
{
l.setText ("Key Pressed");
}

public void keyReleased (KeyEvent e)


{
l.setText ("Key Released");
}

public void keyTyped (KeyEvent e)


{
l.setText ("Key Typed");
}

public static void main(String[] args) {


new KeyListenerExample();
}
}

/*Java KeyListener Example 2: Count Words &


Characters
In the following example, we are printing the count of
words and characters of the string.
Here, the string is fetched from the TextArea and
uses the KeyReleased() method of KeyListener interface.
*/
import java.awt.*;
import java.awt.event.*;
public class KeyListenerExample2 extends Frame implements KeyListener
{
Label l;
TextArea area;

KeyListenerExample2()
{
l = new Label();
l.setBounds (20, 50, 200, 20);

area = new TextArea();


area.setBounds (20, 80, 300, 300);

area.addKeyListener(this);

add(l);
add(area);

setSize (400, 400);


setLayout (null);
setVisible (true);
}

public void keyPressed(KeyEvent e) {}

public void keyReleased (KeyEvent e)


{

String text = area.getText();

String words[] = text.split ("\\s");

l.setText ("Words: " + words.length + " Characters:" + text.length());


}
public void keyTyped(KeyEvent e) {}

public static void main(String[] args)


{
new KeyListenerExample2();
}
}

//Demonstrate WindowListener
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class WindowExample extends Frame implements WindowListener


{
WindowExample()
{
addWindowListener(this);

setSize (400, 400);


setLayout (null);
setVisible (true);
}
public static void main(String[] args)
{
new WindowExample();
}

public void windowActivated (WindowEvent arg0)


{
System.out.println("activated");
}
public void windowClosed (WindowEvent arg0)
{
System.out.println("closed");
}

public void windowClosing (WindowEvent arg0)


{
System.out.println("closing");
dispose();
}

public void windowDeactivated (WindowEvent arg0)


{
System.out.println("deactivated");
}

public void windowDeiconified (WindowEvent arg0)


{
System.out.println("deiconified");
}

public void windowIconified(WindowEvent arg0)


{
System.out.println("iconified");
}

public void windowOpened(WindowEvent arg0)


{
System.out.println("opened");
}
}

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