// File: FigConstVert.java // Classes: FigConstVert // Author: Kedar Patankar package edu.neu.ccs.demeter.tools.apstudio.graphedit; import java.awt.Color; import java.awt.Graphics; import java.awt.FontMetrics; import java.awt.Rectangle; import java.awt.Point; import java.util.Hashtable; /** Primitive Fig to draw rectangles on a LayerDiagram */ public class FigConstVert extends Fig { /* Label associated with the Construction Class */ private String _label; /* number of handles on the boundary rectangle */ public static final int numHandles = 8; /** Construct a new ConstVert w/ the given position, size, and line color */ public FigConstVert(Integer x, Integer y, Integer r_width, Integer r_height, Color r_color, Color f_color, String label) { super(x.intValue(), y.intValue(), r_width.intValue(), r_height.intValue(), r_color, f_color, numHandles); _label = label; } /** Reply the vertex name */ public String get_label() { return _label; } /** Set the vertex name */ public void set_label(String name,Document d) { int width,height; width=d.vertexSize().x; height=d.vertexSize().y; // Graphics g = d.getGraphics(); // g.setFont(d.getFont()); // FontMetrics fm =g.getFontMetrics(); FontMetrics fm = Globals.getFontMetrics(); int stringWidth = fm.stringWidth(name)+4; int letterHeight = fm.getHeight(); width = Math.max(width,stringWidth); height = Math.max(height,letterHeight + 15 + 2); _label=name; objectWidth=width; objectHeight=height; } public void drawColored(Graphics g) { draw(g); Color old = g.getColor(); g.setColor(Color.blue); int x = position().x; int y = position().y; int width=objectWidth; g.fillRect(x,y,width,15); g.setColor(old); } /** Draw this ConstVert */ public void draw(Graphics g) { int x = position().x; int y = position().y; int width=objectWidth-2; int height=objectHeight-2; if (objectFillColor != null) { g.setColor(objectFillColor); g.fillRect(x,y,width,height); } g.setColor(objectLineColor); g.drawRect(x,y,width,height); // FontMetrics fm =g.getFontMetrics(g.getFont()); FontMetrics fm =Globals.getFontMetrics(); int stringx = x; int stringy = y + 15; String s = _label; g.drawString(s,stringx+((width- fm.stringWidth(s))/2),stringy+fm.getMaxAscent() ); } /** Position and draw handles on this FigRect */ public void drawSelected(Graphics g) { } /** When this FigRect is selected in an Editor, use SelectionHandles * to record that fact and process events */ public Selection selectionObject() { return new SelectionHandles(this); } /** 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) { Rectangle rect1;//, rect2; Point p = position(); /* Set the bounding rectangle*/ rect1 = new Rectangle(p.x - GRIP_MARGIN, p.y - GRIP_MARGIN, objectWidth + GRIP_MARGIN * 2, objectHeight + GRIP_MARGIN * 2); /* rect2 = new Rectangle(p.x + GRIP_MARGIN, p.y + GRIP_MARGIN, objectWidth - GRIP_MARGIN * 2, objectHeight - GRIP_MARGIN * 2); */ // if (objectFillColor != null) return rect1.contains(x,y); // else return rect1.contains(x,y) && !rect2.contains(x,y); } } /* end class FigConstVert */