// 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: FigImage.java // Classes: FigImage // Original Author: brw@tusc.com.au // $Id: FigImage.java,v 1.7 1997/06/10 23:42:41 jrobbins Exp $ package uci.graphedit; import java.awt.*; import java.awt.image.*; import java.util.*; import java.net.*; /** Primitive Fig to draw icons on a LayerDiagram. * * FEATURE: basic_shapes_image */ public class FigImage extends Fig implements ImageObserver { //////////////////////////////////////////////////////////////// // constants /** Number of handles on the boundary rectangle. */ public static final int numHandles = 8; //////////////////////////////////////////////////////////////// // instance variables /** The Image being rendered */ protected transient Image _image; protected URL _url; //////////////////////////////////////////////////////////////// // constructors /** Construct a new FigImage with the given position, size, and * highlight color. The highlight color is used in inverting the * image for highlighting when the icon is selected. */ public FigImage(int x, int y, int r_width, int r_height, Image img, Color x_color) { super(x, y, r_width, r_height, x_color, null, numHandles); _image = img; } /** Construct a new FigImage w/ the given position, size, image, and * attributes */ public FigImage(int x, int y, int r_width, int r_height, Image img, Hashtable gAttrs) { super(x, y, r_width, r_height, Color.white, null, numHandles); _image = img; put(gAttrs); } /** Construct a new FigImage w/ the given position, image, and attributes. */ public FigImage(int x, int y, Image i, Hashtable gAttrs) { super(x, y, 0, 0, Color.white, null, numHandles); _image = i; put(gAttrs); objectWidth = i.getWidth(this); objectHeight = i.getHeight(this); } /** Construct a new FigImage w/ the given position, URL, and attributes. */ public FigImage(int x, int y, URL imageUrl, Hashtable gAttrs) { super(x, y, 0, 0, Color.white, null, numHandles); _url = imageUrl; _image = Globals.getImage(_url); Globals.waitForImages(); put(gAttrs); objectWidth = _image.getWidth(this); objectHeight = _image.getHeight(this); } //////////////////////////////////////////////////////////////// // accessors // add get and put for the url... //////////////////////////////////////////////////////////////// // ImageObserver API public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) { boolean done=((infoflags&(ERROR | FRAMEBITS | ALLBITS)) != 0); return !done; } //////////////////////////////////////////////////////////////// // drawing methods /** Draw this FigImage on the given Graphics. */ 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 (_image == null) { System.out.println("reloading image"); if (_url != null) { _image = Globals.getImage(_url); Globals.waitForImages(); } } if (_image != null) g.drawImage(_image, new_x, new_y, new_w, new_h, this); else { g.setColor(getFillColor()); g.fillRect(new_x, new_y, new_w, new_h); } } /** Position and draw handles on this FigImage. */ 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); } //////////////////////////////////////////////////////////////// // Editor API /** When this FigImage is selected in an Editor, use SelectionHandles * to record that fact and process events */ public Selection selectionObject() { return new SelectionHandles(this); } private static Rectangle _CaptureRect = new Rectangle(0, 0, 0, 0); /** Reply true if the given mouse coordinates are inside or "near" * this FigImage. Needs-More-Work: I should have separate near() and * inside() functions. */ public boolean inside(int x, int y) { Point p = position(); /* Set the bounding rectangle */ synchronized (_CaptureRect) { _CaptureRect.reshape(p.x - GRIP_MARGIN, p.y - GRIP_MARGIN, objectWidth + GRIP_MARGIN * 2, objectHeight + GRIP_MARGIN * 2); return _CaptureRect.inside(x,y); } } public void createDrag(int anchorX, int anchorY, int x, int y, int snapX, int snapY) { position(snapX, snapY); } /** When the user drags a handle, update the position or size of * this FigImage 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; } MediaTracker tracker = new MediaTracker(Globals.getApplet()); tracker.addImage(_image, 1, objectWidth, objectHeight); try { tracker.waitForAll(1000); } catch (InterruptedException e) { } } /* end method dragHandle */ } /* end of FigImage class */