package mapMaker; import MDColor.ColorUtils; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Graphics2D; import MDUtils.MDDimUtils; import java.awt.Dimension; import java.awt.Color; import java.awt.Polygon; import java.awt.geom.Ellipse2D; import java.awt.Font; import java.awt.FontMetrics; import javax.swing.BorderFactory; import java.util.Iterator; import java.util.ListIterator; import java.util.Collection; /** A panel to hold text (COUNTY information) for the Map Maker project * @author Martin * Date: 11/7/2002 */ public class TextPanel extends JPanel { final static int defaultWidth = 250; final static int defaultHeight = 500; final static double aspectRatio = ( (double) defaultWidth)/defaultHeight; final static Dimension desiredSize = new Dimension(defaultWidth, defaultHeight); final static Color defaultColor = new Color(255,255,150); //light green final int startX = 20, startY = 25; //coordinates of starting position final float defaultFontSize = 14; private float currFontSize = defaultFontSize; /** This is where the data is kept */ private MapModel theMap; public TextPanel() { super(); this.setPreferredSize(desiredSize); this.setBackground(defaultColor); this.setBorder(BorderFactory.createRaisedBevelBorder()); } //end constructor /** Set the map (model) to be drawn on this panel. * The map is assumed to be completely full of data and ready to use. */ public void setMapModel(MapModel m) { //assert m != null; theMap = m; currFontSize = defaultFontSize; this.repaint(); } /** Paint the county information on the panel */ public void paintComponent(Graphics oldG) { Graphics2D g2 = (Graphics2D) oldG; //don't change this line super.paintComponent(g2); //probably not needed if (theMap == null) { g2.drawString("Waiting for map...", startX, startY); return; } Iterator infoIter = theMap.getCountyInfoIter(); //strings with all info if (! infoIter.hasNext()) { g2.drawString("No county information available", startX, startY); } else { assert currFontSize >= 2.0; Font currFont = g2.getFont(); currFont = currFont.deriveFont(currFontSize); g2.setFont(currFont); //start displaying g2.drawString("County Pop. County Seat", startX, startY); //header FontMetrics fm = g2.getFontMetrics(); int yStep = fm.getHeight(); boolean displayedOK = dumpStringList(g2, infoIter, startX, startY+yStep, yStep); if (!displayedOK && currFontSize > 4) { //probably the font was too big currFontSize = currFontSize - 2; repaint(); //let the process begin again } }//done displaying } //end paintComponent /** paint each String from an list of strings, each on consecutive lines. * The graphics context's current settings are used. * @param iter Iterator of a collection of strings, one per county. * @param x x-coordinate of the first location to draw a string * @param y y-coord. @param yStep ncrease y by this for each line * @return boolean true iff the display was successful; mostly likely * reason for failure is a font size too large. */ public boolean dumpStringList(final Graphics2D g, final Iterator iter, final int xCoord, final int yCoord, final int yStep) { assert yStep > 0; int y = yCoord; while (iter.hasNext()) { y += yStep; String oneLine = (String) iter.next(); if (oneLine != null) { g.drawString(oneLine, xCoord, y); } } //end while //peek at where we are int panelHeight = this.getSize().height; if (y > panelHeight) { //oops, off the bottom edge of this panel return false; //unsatisfactory.. better to compute needed size here } else { return true; } } //end dumpStringList } //end class TextPanel