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 spring * @author Khai Truong */ class labeled_spring 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_spring(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.black); surf.drawLine(0,h()/2, w()/8, h()/2); surf.drawLine(w()/8,h()/2,w()/4,h()-1); surf.drawLine(w()/4,h()-1,(3*w())/8,0); surf.drawLine((3*w())/8,0,w()/2,h()-1); surf.drawLine(w()/2,h()-1,(5*w())/8,0); surf.drawLine((5*w())/8,0,(3*w()/4),h()-1); surf.drawLine((3*w())/4,h()-1,(7*w()/8),h()/2); surf.drawLine(w()-1-w()/8,h()/2, w()-1, h()/2); /* measure the string and clear behind it */ sz_w = _metrics.stringWidth(text()) + 4; sz_h = _metrics.getHeight() + 2; /* draw the text tag */ surf.setColor(Color.red); surf.setFont(font()); surf.drawString(text(), 2, _metrics.getAscent()+1); /* let superclass draw any children */ super.draw_self_local(surf); } }