/* File: Bubble.java Author: Chris Johnson johnson@dcs.gla.ac.uk Modified: 16/10/98 Acknowledgement: This is based on an Applet developed by D.M. Geary in Graphic Java 1.1, Sunsoft Press, 1997, p.280. His version is based on AWT1.1 - I've adapted it to run under AWT1.0. Description: This class provides a constructor to takes any graphical component and a string. It references the frame associated with the component and constructs a window in that frame. Paint() is over-ridden to put a border round the window and add the text associated with the component. */ import java.awt.*; public class Bubble extends Window { private String text; public Bubble(Component comp, String text) { super(getFrame(comp)); this.text = text; setForeground(Color.black); } public Dimension preferredSize() { Graphics g = getGraphics(); FontMetrics fm = g.getFontMetrics(); return new Dimension(fm.stringWidth(text)+4, fm.getHeight()+4); } public void paint(Graphics g) { Dimension Component_size = size(); FontMetrics fm = g.getFontMetrics(); g.drawRect(0,0,Component_size.width-1, Component_size.height-1); g.drawString(text, 2, fm.getAscent()+2); } public void show(){ pack(); super.show(); } static Frame getFrame(Component c) { Frame frame = null; while ((c=c.getParent()) != null) if (c instanceof Frame) frame = (Frame)c; return frame; } }