home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-1dot1 / MouseDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.9 KB  |  98 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.MouseListener;
  20. import java.awt.event.MouseEvent;
  21.  
  22. public class MouseDemo extends Applet 
  23.                        implements MouseListener {
  24.     BlankArea blankArea;
  25.     TextArea textArea;
  26.     static final int maxInt = java.lang.Integer.MAX_VALUE;
  27.  
  28.     public void init() {
  29.         GridBagLayout gridbag = new GridBagLayout();
  30.         GridBagConstraints c = new GridBagConstraints();
  31.         setLayout(gridbag);
  32.  
  33.         c.fill = GridBagConstraints.BOTH;
  34.         c.gridwidth = GridBagConstraints.REMAINDER;
  35.         c.weightx = 1.0;
  36.         c.weighty = 1.0;
  37.  
  38.     c.insets = new Insets(1, 1, 1, 1);
  39.         blankArea = new BlankArea();
  40.         gridbag.setConstraints(blankArea, c);
  41.         add(blankArea);
  42.  
  43.     c.insets = new Insets(0, 0, 0, 0);
  44.         textArea = new TextArea(5, 20);
  45.         textArea.setEditable(false);
  46.         gridbag.setConstraints(textArea, c);
  47.         add(textArea);
  48.  
  49.         //Register for mouse events on blankArea and applet (panel).
  50.         blankArea.addMouseListener(this);
  51.         addMouseListener(this);
  52.     }
  53.  
  54.     public void mousePressed(MouseEvent e) {
  55.        saySomething("Mouse button press", e);
  56.     }
  57.  
  58.     public void mouseReleased(MouseEvent e) {
  59.        saySomething("Mouse button release", e);
  60.     }
  61.  
  62.     public void mouseEntered(MouseEvent e) {
  63.        saySomething("Cursor enter", e);
  64.     }
  65.  
  66.     public void mouseExited(MouseEvent e) {
  67.        saySomething("Cursor exit", e);
  68.     }
  69.  
  70.     public void mouseClicked(MouseEvent e) {
  71.        saySomething("Mouse button click", e);
  72.     }
  73.  
  74.     void saySomething(String eventDescription, MouseEvent e) {
  75.         textArea.append(eventDescription + " detected on "
  76.                         + e.getComponent().getClass().getName()
  77.                         + ".\n");
  78.         textArea.setCaretPosition(maxInt); //hack to scroll to bottom
  79.     }
  80. }
  81.  
  82. class BlankArea extends Canvas {
  83.     Dimension minSize = new Dimension(100, 100);
  84.  
  85.     public Dimension getMinimumSize() {
  86.     return minSize;
  87.     }
  88.  
  89.     public Dimension getPreferredSize() {
  90.     return minSize;
  91.     }
  92.  
  93.     public void paint(Graphics g) {
  94.     Dimension size = getSize();
  95.     g.drawRect(0, 0, size.width - 1, size.height - 1);
  96.     }
  97. }
  98.