import java.awt.*; import java.util.*; public class VisibleHorizontalArray extends VisibleArray { private int cellWidth = 50; private int cellHeight = 30; private int indexWidth = 50; public VisibleHorizontalArray(String caption, Object a[]) { super(caption, a); } public void paint(Graphics g) { super.paint(g); FontMetrics fm = g.getFontMetrics(); int vOffset = fm.getAscent() + 8; int hOffset1 = 10; int vPosition1 = (int) (0.1 *cellHeight) + vOffset + 8; int vPosition2 = vOffset + cellHeight; g.setFont(new Font("Helvetica", Font.BOLD, (cellHeight * 2)/3)); FontMetrics fm2 = g.getFontMetrics(); for (int i = 0; i < length(); i++) { int hPos1 = hOffset1 + (i * cellWidth); // Show the array index on a green background: g.setColor(Color.green); g.fillRect(hPos1, vPosition1, cellWidth - 4, cellHeight - 4); g.setColor(Color.black); int indexWidth = fm2.stringWidth(i + ""); g.drawString( i + "", hPos1 + ((cellWidth - indexWidth)/2), vPosition2); // Show the array value on a blue background: g.setColor(Color.blue); g.fillRect(hPos1, vPosition1 + cellHeight, cellWidth - 4, cellHeight - 4); g.setColor(Color.red); int eltWidth = fm2.stringWidth(theArray[i].toString()); g.drawString( theArray[i].toString(), hPos1 + ((cellWidth - eltWidth)/2), vPosition2 + cellHeight); } } public void describeVHA() { System.out.println("A VisibleHorizontalArray: "); describe(); } } // S. Tanimoto, November 2000.