/* * 1.1 code. */ import java.applet.Applet; import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; public class MouseEventDemo extends Applet implements MouseListener { BlankArea blankArea; TextArea textArea; static final int maxInt = java.lang.Integer.MAX_VALUE; String newline; public void init() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 1.0; c.insets = new Insets(1, 1, 1, 1); blankArea = new BlankArea(new Color(0.98f, 0.97f, 0.85f)); gridbag.setConstraints(blankArea, c); add(blankArea); c.insets = new Insets(0, 0, 0, 0); textArea = new TextArea(5, 20); textArea.setEditable(false); gridbag.setConstraints(textArea, c); add(textArea); //Register for mouse events on blankArea and applet (panel). blankArea.addMouseListener(this); addMouseListener(this); newline = System.getProperty("line.separator"); } public void mousePressed(MouseEvent e) { saySomething("Mouse pressed; # of clicks: " + e.getClickCount(), e); } public void mouseReleased(MouseEvent e) { saySomething("Mouse released; # of clicks: " + e.getClickCount(), e); } public void mouseEntered(MouseEvent e) { saySomething("Mouse entered", e); } public void mouseExited(MouseEvent e) { saySomething("Mouse exited", e); } public void mouseClicked(MouseEvent e) { saySomething("Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } void saySomething(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " detected on " + e.getComponent().getClass().getName() + newline); textArea.setCaretPosition(maxInt); //hack to scroll to bottom } }