home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / RectangleDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  4.8 KB  |  164 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.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This displays a framed area.  When the user drags within
  22.  * the area, this program displays a rectangle extending from
  23.  * where the user first pressed the mouse button to the current
  24.  * cursor location.
  25.  */
  26.  
  27. public class RectangleDemo extends Applet {
  28.     RFramedArea framedArea;
  29.     Label label;
  30.  
  31.     public void init() {
  32.         GridBagLayout gridBag = new GridBagLayout();
  33.         GridBagConstraints c = new GridBagConstraints();
  34.  
  35.         setLayout(gridBag);
  36.  
  37.         framedArea = new RFramedArea(this);
  38.         c.fill = GridBagConstraints.BOTH;
  39.         c.weighty = 1.0;
  40.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  41.         gridBag.setConstraints(framedArea, c);
  42.         add(framedArea);
  43.  
  44.         label = new Label("Drag within the framed area.");
  45.         c.fill = GridBagConstraints.HORIZONTAL;
  46.         c.weightx = 1.0;
  47.         c.weighty = 0.0;
  48.         gridBag.setConstraints(label, c);
  49.         add(label);
  50.  
  51.         validate();
  52.     }
  53.  
  54.     public void rectChanged(Rectangle rect) {
  55.         label.setText("Rectangle goes from ("
  56.                       + rect.x + ", " + rect.y + ") to ("
  57.                       + (rect.x + rect.width - 1) + ", "
  58.                       + (rect.y + rect.height - 1) + ").");
  59.         repaint();
  60.     }
  61. }
  62.  
  63. /* This class exists solely to put a frame around the coordinate area. */
  64. class RFramedArea extends Panel {
  65.     public RFramedArea(RectangleDemo controller) {
  66.         super();
  67.  
  68.         //Set layout to one that makes its contents as big as possible.
  69.         setLayout(new GridLayout(1,0));
  70.  
  71.         add(new SelectionArea(controller));
  72.         validate();
  73.     }
  74.  
  75.     public Insets insets() {
  76.         return new Insets(4,4,5,5);
  77.     }
  78.  
  79.     public void paint(Graphics g) {
  80.         Dimension d = size();
  81.         Color bg = getBackground();
  82.  
  83.         g.setColor(bg);
  84.         g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
  85.         g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
  86.     }
  87. }
  88.  
  89. class SelectionArea extends Canvas {
  90.     Rectangle currentRect;
  91.     RectangleDemo controller;
  92.  
  93.     public SelectionArea(RectangleDemo controller) {
  94.         super();
  95.         this.controller = controller;
  96.     }
  97.  
  98.     public boolean mouseDown(Event event, int x, int y) {
  99.         currentRect = new Rectangle(x, y, 0, 0);
  100.         repaint();
  101.         return false;
  102.     }
  103.  
  104.     public boolean mouseDrag(Event event, int x, int y) {
  105.         currentRect.resize(x - currentRect.x, y - currentRect.y);
  106.         repaint();
  107.         return false;
  108.     }
  109.  
  110.     public boolean mouseUp(Event event, int x, int y) {
  111.         currentRect.resize(x - currentRect.x, y - currentRect.y);
  112.         repaint();
  113.         return false;
  114.     }
  115.  
  116.     public void paint(Graphics g) {
  117.         Dimension d = size();
  118.  
  119.         //If currentRect exists, paint a rectangle on top.
  120.         if (currentRect != null) {
  121.             Rectangle box = getDrawableRect(currentRect, d);
  122.             controller.rectChanged(box);
  123.     
  124.             //Draw the box outline.
  125.             g.drawRect(box.x, box.y, box.width - 1, box.height - 1);
  126.         }
  127.     }
  128.  
  129.     Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) {
  130.         int x = originalRect.x;
  131.         int y = originalRect.y;
  132.         int width = originalRect.width;
  133.         int height = originalRect.height;
  134.  
  135.         //Make sure rectangle width and height are positive.
  136.         if (width < 0) {
  137.             width = 0 - width;
  138.             x = x - width + 1;
  139.             if (x < 0) {
  140.                 width += x;
  141.                 x = 0;
  142.             }
  143.         }
  144.         if (height < 0) {
  145.             height = 0 - height;
  146.             y = y - height + 1;
  147.             if (y < 0) {
  148.                 height += y;
  149.                 y = 0;
  150.             }
  151.         }
  152.  
  153.         //The rectangle shouldn't extend past the drawing area.
  154.         if ((x + width) > drawingArea.width) {
  155.             width = drawingArea.width - x;
  156.         }
  157.         if ((y + height) > drawingArea.height) {
  158.             height = drawingArea.height - y;
  159.         }
  160.  
  161.         return new Rectangle(x, y, width, height);
  162.     }
  163. }
  164.