// Copyright (c) 1996-98 The Regents of the University of California. All // Rights Reserved. Permission to use, copy, modify, and distribute this // software and its documentation for educational, research and non-profit // purposes, without fee, and without a written agreement is hereby granted, // provided that the above copyright notice and this paragraph appear in all // copies. Permission to incorporate this software into commercial products may // be obtained by contacting the University of California. David F. Redmiles // Department of Information and Computer Science (ICS) University of // California Irvine, California 92697-3425 Phone: 714-824-3823. This software // program and documentation are copyrighted by The Regents of the University // of California. The software program and documentation are supplied "as is", // without any accompanying services from The Regents. The Regents do not // warrant that the operation of the program will be uninterrupted or // error-free. The end-user understands that the program was developed for // research purposes and is advised not to rely exclusively on the program for // any reason. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS // DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY // DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE // SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, // ENHANCEMENTS, OR MODIFICATIONS. // File: FigRect.java // Classes: FigRect // Original Author: ics125b spring 1996 // $Id: FigRect.java,v 1.11 1997/06/10 23:42:46 jrobbins Exp $ package uci.graphedit; import java.applet.*; import java.awt.*; import java.io.*; import java.util.*; /** Primitive Fig to draw rectangles on a LayerDiagram. * * * FEATURE: basic_shapes_rect. */ public class FigRect extends Fig { //////////////////////////////////////////////////////////////// // constants /* number of handles on the boundary rectangle */ public static final int numHandles = 8; //////////////////////////////////////////////////////////////// // instance variables //////////////////////////////////////////////////////////////// // constructors /** Construct a new FigRect w/ the given position, size, and line color. */ public FigRect(int x, int y, int w, int h, Color line_color){ super(x, y, w, h, line_color, null, numHandles); } /** Construct a new FigRect w/ the given position, size, and attributes. */ public FigRect(int x, int y, int w, int h, Hashtable gAttrs){ super(x, y, w, h, Color.black, null, numHandles); put(gAttrs); } /** Construct a new FigRect w/ the given position, size, line color, * and fill color.*/ public FigRect(int x, int y, int w, int h, Color line_color, Color fill_color) { super(x, y, w, h, line_color, fill_color, numHandles); } //////////////////////////////////////////////////////////////// // drawing methods /** Draw this FigRect */ public void draw(Graphics g) { int rx = position().x; int ry = position().y; int new_x, new_y, new_w, new_h; /* Compensate for negative width and height (for drag) */ if (objectWidth > 0) { new_x = rx; new_w = objectWidth; } else { new_x = rx + objectWidth; new_w = -objectWidth; } if (objectHeight > 0) { new_y = ry; new_h = objectHeight; } else { new_y = ry + objectHeight; new_h = -objectHeight; } if (filled) { g.setColor(objectFillColor); g.fillRect(new_x, new_y, new_w, new_h); } if (lineWidth > 0) { g.setColor(objectLineColor); g.drawRect(new_x, new_y, new_w - lineWidth, new_h - lineWidth); } } /** Position and draw handles on this FigRect */ public void drawSelected(Graphics g) { int rx = position().x; int ry = position().y; int new_x, new_y, new_w, new_h; /* Compensate for negative width and height (for drag) */ if (objectWidth > 0) { new_x = rx; new_w = objectWidth; } else { new_x = rx + objectWidth; new_w = -objectWidth; } if (objectHeight>0) { new_y = ry; new_h = objectHeight; } else { new_y = ry + objectHeight; new_h = -objectHeight; } /* Set the handle point positions*/ _handleRects[0].move(new_x - HAND_SIZE/2, new_y - HAND_SIZE/2); _handleRects[1].move(new_x + (new_w/2) - HAND_SIZE/2, new_y - HAND_SIZE/2); _handleRects[2].move(new_x + (new_w) - HAND_SIZE/2, new_y - HAND_SIZE/2); _handleRects[3].move(new_x + (new_w) - HAND_SIZE/2, new_y + (new_h/2) - HAND_SIZE/2); _handleRects[4].move(new_x + (new_w) - HAND_SIZE/2, new_y + new_h - HAND_SIZE/2); _handleRects[5].move(new_x + (new_w/2) - HAND_SIZE/2, new_y + new_h - HAND_SIZE/2); _handleRects[6].move(new_x - HAND_SIZE/2, new_y + new_h - HAND_SIZE/2); _handleRects[7].move(new_x - HAND_SIZE/2, new_y + (new_h/2) - HAND_SIZE/2); drawHandles(g); } //////////////////////////////////////////////////////////////// // DiagramElement API /** When this FigRect is selected in an Editor, use SelectionHandles * to record that fact and process events. */ public Selection selectionObject() { return new SelectionHandles(this); } private static Rectangle _EnlargedBBox = new Rectangle(0, 0, 0, 0); private static Rectangle _ReducedBBox = new Rectangle(0, 0, 0, 0); /** Reply true if the given mouse coordinates are inside or "near" * this FigRect. Needs-More-Work: I whould have separate near() and * inside() functions. */ public boolean inside(int x, int y) { Point p = position(); synchronized (_EnlargedBBox) { /* Set the bounding rectangle*/ _EnlargedBBox.reshape(p.x - GRIP_MARGIN, p.y - GRIP_MARGIN, objectWidth + GRIP_MARGIN * 2, objectHeight + GRIP_MARGIN * 2); _ReducedBBox.reshape(p.x + GRIP_MARGIN, p.y + GRIP_MARGIN, objectWidth - GRIP_MARGIN * 2, objectHeight - GRIP_MARGIN * 2); if (filled) return _EnlargedBBox.inside(x,y); else return _EnlargedBBox.inside(x,y) && !_ReducedBBox.inside(x,y); } } /** When the user drags a handle, update the position or size of * this FigRect as needed. */ public void dragHandle(int mouse_x,int mouse_y, int anchor_x,int anchor_y, Handle h) { Point p = position(); int old_tlx = p.x; // old top-left corner int old_tly = p.y; int old_brx = p.x + objectWidth; // old bottom-right corner int old_bry = p.y + objectHeight; //startTrans(); switch (h.index) { case 0: /* The top left handle */ p.x = mouse_x; p.y = mouse_y; objectWidth = old_brx - mouse_x; objectHeight = old_bry - mouse_y; break; case 1: /* The top middle handle */ p.y = mouse_y; objectHeight = old_bry - mouse_y; break; case 2: /* The top right handle */ p.y = mouse_y; objectWidth = mouse_x - old_tlx; objectHeight = old_bry - mouse_y; break; case 3: /* The right middle handle */ objectWidth = mouse_x - old_tlx; break; case 4: /* The right bottom handle */ objectWidth = mouse_x - old_tlx; objectHeight = mouse_y - old_tly; break; case 5: /* The bottom middle handle */ objectHeight = mouse_y - old_tly; break; case 6: /* The bottom left handle */ p.x = mouse_x; objectHeight = mouse_y - old_tly; objectWidth = old_brx - mouse_x; break; case 7: p.x = mouse_x; objectWidth = old_brx - mouse_x; break; } if (objectWidth < MIN_SIZE) { objectWidth = MIN_SIZE; if (h.index == 0 || h.index == 6 || h.index == 7) p.x = old_brx - MIN_SIZE; } if (objectHeight < MIN_SIZE) { objectHeight = MIN_SIZE; if (h.index == 0 || h.index == 1 || h.index==2) p.y = old_bry - MIN_SIZE; } } /* end method dragHandle */ } /* end class FigRect */