/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Copyright (c) John D. Mitchell, 1996 -- All Rights Reserved PROJECT: JavaWorld MODULE: Tips & Tricks FILE: DoubleClicker.java AUTHOR: John D. Mitchell, Mar 22, 1996 REVISION HISTORY: Name Date Description ---- ---- ----------- JDM 96.04.01 Initial version. DESCRIPTION: This is a demonstration file for the JavaWorld Tips & Tricks column. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ import java.awt.*; import java.applet.*; public class DoubleClicker extends Applet { // The string to display after a single click. static private String singleClickString = "Hello JavaWorld!"; // The string to display after a double click! static private String doubleClickString = "Happy April Fools!"; // Which of the two strings should be displayed? private boolean displayDoubleClickString = false; // The number of milliseconds that should bound a double click. static private long mouseDoubleClickThreshold = 300L; // The previous mouseUp event's time. private long mouseUpClickTime = 0; // mouseUp() will be called once per mouse button *release*. // We treat all clicks as a single click unless the second click // event happens within the threshold set above. public boolean mouseUp (Event e, int x, int y) { long eventTime = e.when; long timeDiff; // Make sure the event times difference is a positive number. // Note that this is due to the fact that the time value on a // long of machines is a 64-bit *unsigned* value but, alas, // Java doesn't have unsigned numbers so at a certain point // the values roll over into the very high negative numbers // which then actually *decrease* (recall that Java mandates a // two's complement arithmetic). timeDiff = eventTime - mouseUpClickTime; if (timeDiff < 0) timeDiff = -timeDiff; // Display the event time information. showStatus ("Current event :" + eventTime + ": Previous event :" + mouseUpClickTime + ": Difference :" + timeDiff + ":"); // Did this click happen with the given time bound? if (mouseDoubleClickThreshold > timeDiff) { // Yep, display the double click message. displayDoubleClickString = true; // Clear out the previous event time so that we don't get // confused in our count. // Note that this means that triple, quadruple, // etc. clicks will *not* be condensed into a double // click. If you want that sort of behavior change this // to: mouseUpClickTime = eventTime; mouseUpClickTime = 0; } else { // Nope, just a single click so make sure that we display // the single click message. displayDoubleClickString = false; // Update the event time holder. mouseUpClickTime = eventTime; } // Force a repaint so we can see what's happening. repaint(); // We've handled the event. return true; } public void paint (Graphics g) { // Let's muck about with the fonts. Font font = new Font("Times Roman", Font.PLAIN, 20); FontMetrics fm = getFontMetrics(font); g.setFont (font); // Figure out the width of the string to be displayed. int displayStringWidth; if (displayDoubleClickString) displayStringWidth = fm.stringWidth (doubleClickString); else displayStringWidth = fm.stringWidth (singleClickString); // Let's center the string in the window, just for kicks. int x = (this.size().width - displayStringWidth) / 2; int y = (this.size().height - fm.getHeight()) / 2; if (displayDoubleClickString) g.drawString (doubleClickString, x, y); else g.drawString (singleClickString, x, y); } }