import sub_arctic.lib.*; import sub_arctic.output.drawable; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; /** * Small interactor class that draws a rectangle for its bounding box along with * a small label at the top left...representing a window * @author Scott Hudson * * ...modified by Khai Truong */ class labeled_object extends base_parent_interactor { /** Text for the label we display */ protected String _text = ""; /** Font to display label in */ protected Font _font = null; /** Metrics object for current font */ protected FontMetrics _metrics = null; /** * Constructor * @param int wid width of the object. * @param int hi height of the object. * @param String lab text of the display label */ public labeled_object(int wid, int hi, String lab) { super(0,0,wid,hi); set_text(lab); set_font(new Font("Helvetica",Font.PLAIN,9)); } /** * Text for the label we display * @return String the string we are currently displaying. */ public String text() { return _text; } /** * Set the text for the label we display * @param String t the new label text. */ public void set_text(String t) { if (!_text.equals(t)) { _text = t; damage_self(); } } /** * Font to display label in * @return Font the font we are display in. */ public Font font() { return _font; } /** * Set font to display label in * @param Font f the new font. */ public void set_font(Font f) { if (_font != f) { _font = f; _metrics = manager.get_metrics(f); damage_self(); } } /** * Output routine for the interactor. * @param drawable surf the drawing surface we draw on. */ public void draw_self_local(drawable surf) { int sz_w, sz_h; /* draw a rectangle at the bounds */ surf.setColor(Color.lightGray); surf.fillRect(0,0, w()-1, h()-1); surf.setColor(Color.black); surf.drawRect(0,0, w()-1, h()-1); /* measure the string and clear behind it */ sz_w = _metrics.stringWidth(text()) + 4; sz_h = _metrics.getHeight() + 2; surf.setColor(Color.white); surf.fillRect(0,0, sz_w-1,sz_h-1); /* draw the text tag */ surf.setColor(Color.black); surf.setFont(font()); surf.drawString(text(), 2, _metrics.getAscent()+1); surf.drawRect(0,0, sz_w-1, sz_h-1); /* let superclass draw any children */ super.draw_self_local(surf); } }