/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JApplet; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MouseMotionApplet extends JApplet { JPanel p; public void init() { p = new MyPanel(); getContentPane().add(p); p.revalidate(); p.repaint(); } } class MyCanvas extends JPanel { public MyCanvas() { setSize(300,300); setBackground(Color.blue); } } class MyPanel extends JPanel { JComponent c; JLabel l; public MyPanel() { setSize(300, 330); setLayout(null); c = new MyCanvas(); c.setLocation(0,0); l = new JLabel(); l.setSize(300,30); l.setLocation(0,300); add(c); //, BorderLayout.CENTER); add(l); //, BorderLayout.SOUTH); //-------------------- // event listener installieren // fuer Maus-Bewegung c.addMouseMotionListener (new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { mouseStatus("mouse at",e.getX(),e.getY()); } public void mouseDragged(MouseEvent e) { mouseStatus("mouse dragged at",e.getX(),e.getY()); } } ); //-------------------- // event listener installieren // fuer Maus-Tasten c.addMouseListener (new MouseAdapter() { public void mousePressed(MouseEvent e) { mouseStatus("mouse pressed at",e.getX(),e.getY()); } public void mouseReleased(MouseEvent e) { mouseStatus("mouse released at",e.getX(),e.getY()); } public void mouseEntered(MouseEvent e) { mouseStatus("mouse entered at",e.getX(),e.getY()); } public void mouseExited(MouseEvent e) { mouseStatus("mouse exited at",e.getX(),e.getY()); } } ); } //-------------------- // die eigentliche Verarbeitung protected void mouseStatus(String status, int x, int y) { l.setText(status + " " + x + "," + y); } }