// File: FigAltVert.java // Classes: FigAltVert // Author: Kedar Patankar package edu.neu.ccs.demeter.tools.apstudio.graphedit; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Point; import java.awt.FontMetrics; import java.awt.Font; import java.util.Hashtable; import java.awt.Toolkit; /** Primitive Fig to draw rectangles on a LayerDiagram */ public class FigAltVert 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 FigAltVert(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; // FontMetrics fm = Globals.getFontMetrics(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(new Font("Times Roman",Font.ITALIC,12)); // width=Math.max(width, fm.stringWidth("<>")+4); int stringWidth = fm.stringWidth(name)+2; int letterHeight = fm.getHeight(); width = Math.max(width,stringWidth); // height = Math.max(height,2*letterHeight + 15 + 2); height = Math.max(height,letterHeight + 15 + 2); _label=name; objectWidth=width; objectHeight=height; // System.out.println(width); } 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 AltVert */ public void draw(Graphics g) { int x = position().x; int y = position().y; int width=objectWidth; int height=objectHeight; if (objectFillColor != null) { g.setColor(objectFillColor); g.fillRect(x,y,width,height); } g.setColor(objectLineColor); g.drawRect(x,y,width,height); // FontMetrics fm =Globals.getFontMetrics(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(new Font("Times Roman",Font.ITALIC,12)); int stringy = y + 15; int stringx = x; // int letterheight = fm.getHeight(); // int abst=fm.stringWidth("<>"); // g.drawString("<>",stringx+((width-abst)/2), stringy+fm.getMaxAscent()); String s =_label; // g.drawString(s,stringx+((width-fm.stringWidth(s))/2),stringy+letterheight+fm.getMaxAscent()); Font f= g.getFont(); g.setFont(new Font("Times Roman",Font.ITALIC,12)); g.drawString(s,stringx+((width-fm.stringWidth(s))/2),stringy+fm.getMaxAscent()); g.setFont(f); } /** 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 FigAltVert */