Advertisement
Tranvick

Untitled

Jul 1st, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4.  
  5. class SampleFrame extends Frame implements MouseListener, MouseMotionListener{
  6.     String msg = "";
  7.     int mouseX = 10, mouseY = 40;
  8.     int movX = 0, movY = 0;
  9.    
  10.     SampleFrame(String title) {
  11.         super(title);
  12.         addMouseListener(this);
  13.         addMouseMotionListener(this);
  14.         addWindowListener (new WindowAdapter() {
  15.             public void windowClosing(WindowEvent we) {
  16.                 setVisible(false);
  17.             }
  18.         });
  19.     }
  20.    
  21.     public void mouseClicked(MouseEvent me) {
  22.     }
  23.    
  24.     public void mouseEntered(MouseEvent me) {
  25.         mouseX = 10;
  26.         mouseY = 54;
  27.         msg = "Mouse just entered child.";
  28.         repaint();
  29.     }
  30.    
  31.     public void mouseExited(MouseEvent me) {
  32.         mouseX = 10;
  33.         mouseY = 54;
  34.         msg = "Mouse just exited from child.";
  35.         repaint();
  36.     }
  37.    
  38.     public void mousePressed(MouseEvent me) {
  39.         mouseX = me.getX();
  40.         mouseY = me.getY();
  41.         msg = "Down";
  42.         repaint();
  43.     }
  44.    
  45.     public void mouseReleased(MouseEvent me) {
  46.         mouseX = me.getX();
  47.         mouseY = me.getY();
  48.         msg = "Up";
  49.         repaint();
  50.     }
  51.    
  52.     public void mouseDragged(MouseEvent me) {
  53.         mouseX = movX = me.getX();
  54.         mouseY = movY = me.getY();
  55.         msg = "*";
  56.         repaint();
  57.     }
  58.    
  59.     public void mouseMoved(MouseEvent me) {
  60.         movX = me.getX();
  61.         movY = me.getY();
  62.         repaint(0, 0, 100, 60);
  63.     }
  64.    
  65.     public void paint(Graphics g) {
  66.         g.drawString(msg, mouseX, mouseY);
  67.         g.drawString("MouseAt " + movX + ", " + movY, 10, 40);
  68.     }
  69. }
  70.  
  71. public class app extends Applet implements MouseListener, MouseMotionListener{
  72.     SampleFrame f;
  73.     String msg = "";
  74.     int mouseX = 0, mouseY = 10;
  75.     int movX = 0, movY = 0;
  76.    
  77.     public void init() {
  78.         f = new SampleFrame("Handle Mouse Events");
  79.         f.setSize(300,200);
  80.         f.setVisible(true);
  81.         addMouseListener(this);
  82.         addMouseMotionListener(this);
  83.     }
  84.    
  85.     public void stop() {
  86.         f.setVisible(false);
  87.     }
  88.    
  89.     public void start() {
  90.         f.setVisible(true);
  91.     }
  92.    
  93.     public void mouseClicked(MouseEvent me) {
  94.     }
  95.    
  96.     public void mouseEntered(MouseEvent me) {
  97.         mouseX = 0;
  98.         mouseY = 24;
  99.         msg = "Mouse just entered applet window";
  100.         repaint();
  101.     }
  102.    
  103.     public void mouseExited(MouseEvent me) {
  104.         mouseX = 0;
  105.         mouseY = 24;
  106.         msg = "Mouse justexited from applet window";
  107.         repaint();
  108.     }
  109.    
  110.     public void mousePressed(MouseEvent me) {
  111.         mouseX = me.getX();
  112.         mouseY = me.getY();
  113.         msg = "Down";
  114.         repaint();
  115.     }
  116.    
  117.     public void mouseReleased(MouseEvent me) {
  118.         mouseX = me.getX();
  119.         mouseY = me.getY();
  120.         msg = "Up";
  121.         repaint();
  122.     }
  123.    
  124.     public void mouseDragged(MouseEvent me) {
  125.         mouseX = movX = me.getX();
  126.         mouseY = movY = me.getY();
  127.         msg = "*";
  128.         repaint();
  129.     }
  130.    
  131.     public void mouseMoved(MouseEvent me) {
  132.         movX = me.getX();
  133.         movY = me.getY();
  134.         repaint(0, 0, 100, 20);
  135.     }
  136.    
  137.     public void paint(Graphics g) {
  138.         g.drawString(msg, mouseX, mouseY);
  139.         g.drawString("Mouse at " + movX + ", " + movY, 0, 10);
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement