/*--------------------------------------------------------------------------- -- JAWAA 2.0 -- Copyright information: Susan H. Rodger, Pretesh Patel, Diana Jackson, Ayonike Akingbade Computer Science Department Duke University August 2002 Supported by National Science Foundation DUE-9752583. Copyright (c) 2002 All rights reserved. Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, and other materials related to such distribution and use acknowledge that the software was developed by the author. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. ---------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------ File: JawaaNode.java Package: JAWAA Version 2.0 Author: Pretesh Patel, Diana Jackson, Ayonike Akingbade Date: August 2002 Description of Contents: -------------------------------------------------------------------------------*/ package jawaa.structure; import jawaa.anim.AnimController; import jawaa.object.*; import jawaa.app.*; import java.awt.*; import jawaa.util.*; public class JawaaNode extends JawaaContainer implements Scalable { public JawaaNode (String name, Color foreground, Color background, Color textColor, String[] s, int x, int y, int width, int height) { FontMetrics metric = AnimController.getFontMetrics(); myColor = foreground; myName = name; myBackground = background; myTextColor = textColor; myLocation = new Point(x, y); myWidth = width; myHeight = height; myOutline = new JawaaRectangle(myLocation.x, myLocation.y, myWidth, myHeight, myColor, myBackground); makeText(metric, s, myLocation); setTextLocations(); } public JawaaNode (String name, Color foreground, Color background, Color textColor, JawaaText[] s, Point loc, int width, int height, JawaaShape outline) { myName = name; myColor = foreground; myBackground = background; myTextColor = textColor; myText = s; myLocation = loc; myWidth = width; myHeight = height; myOutline = outline; } public JawaaNode (String[] args) { FontMetrics metric = AnimController.getFontMetrics(); try { myLocation = new Point(Integer.parseInt(args[2]), Integer.parseInt(args[3])); myWidth = Integer.parseInt(args[4]); myHeight = Integer.parseInt(args[5]); myItemCount = Integer.parseInt(args[6]); String[] temp = new String[myItemCount]; int i = 7; int longest = 0; for(int j = 0; j < myItemCount; j++) { if (metric.stringWidth(args[i]) > longest) longest = metric.stringWidth(args[i]); temp[j] = args[i]; i++; } myColor = ColorFactory.getColor(args[i++]); myBackground = ColorFactory.getColor(args[i++]); myTextColor = ColorFactory.getColor(args[i++]); if (args[i].equals("CIRCLE")) makeCircle(metric, longest, temp); else if (args[i].equals("RECT")) makeRect(metric, longest, temp); else makeRect(metric, longest,temp); setTextLocations(); } catch (Exception e) { ErrorHandler.error(this, "syntax error", true); } } protected void makeCircle (FontMetrics metric, int longest, String[] s) { myOutline = new JawaaCircle(myLocation.x, myLocation.y, myWidth, myColor, myBackground); makeText(metric, s, calcVertOffset(metric, s, myLocation)); } protected void makeRect (FontMetrics metric, int longest, String[] s) { myOutline = new JawaaRectangle(myLocation.x, myLocation.y, myWidth, myHeight, myColor, myBackground); makeText(metric, s, calcVertOffset(metric, s, myLocation)); } protected void makeText(FontMetrics metric, String[] s, Point start) { myText = new JawaaText[s.length]; for (int i = 0; i < s.length; i++) { Point offset = new Point(0,0); myText[i] = new JawaaText(s[i], offset, myTextColor); } } protected Point calcVertOffset (FontMetrics metric, String[] s, Point point) { int offset = getCenter().y - point.y; if (s.length % 2 != 0) { offset -= metric.getMaxAscent(); } int index = s.length / 2; for (int i = 0; i < index; i++) { offset -= (metric.getMaxAscent() + metric.getMaxDescent() + 2); } if (offset < 0) return point; else return new Point(point.x, point.y + offset); } private void setTextLocations () { FontMetrics metric = AnimController.getFontMetrics(); BoundingBox b = getBoundingBox(myText); Point p = b.setCenter(getCenter()); int height = 0; for(int i = 0; i width) { width = s[i].stringWidth(); } } return new BoundingBox(width, height); } protected int calcHorzOffset (FontMetrics metric, String s, int width) { int offset = 0; offset = (width - metric.stringWidth(s))/2; if(offset < 0) return 0; else return offset; } protected int calcHorzOffset (JawaaText t, int width) { int offset = 0; offset = (width - t.stringWidth())/2; if(offset < 0) return 0; else return offset; } public void offsetLocation (int x, int y) { myLocation.x += x; myLocation.y += y; myOutline.offsetLocation(x, y); for(int i = 0; i < myText.length; i++) { myText[i].offsetLocation(x, y); } } public void setLocation (int x, int y) { myLocation.x = x; myLocation.y = y; Point p = myOutline.getLocation(); for(int i= 0; i< myText.length; i++) { myText[i].offsetLocation(x-p.x, y-p.y); } myOutline.setLocation(x,y); } public void setTextColor(String s) { myTextColor = ColorFactory.getColor(s); for (int i=0;i