package mapMaker; import MDColor.ColorUtils; import MDUtils.MGeomUtils; import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.BasicStroke; import java.awt.Dimension; import java.awt.Color; import java.awt.Polygon; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import java.awt.geom.Line2D; import javax.swing.BorderFactory; import java.util.ArrayList; import java.util.Iterator; import java.util.GregorianCalendar; import java.util.Date; import java.text.DateFormat; /** A panel on which a map will be drawn * * @author MD * @date 10/2002 */ public class MapPanel extends JPanel { final static int defaultWidth = 500; 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,200,200); //light pink final static Color stateBoundaryColor = Color.red; /** position of message identifying the program */ final static int messageX = 20, messageY = defaultHeight - 30; /**This will be set to the map (model) to be drawn **/ private MapModel theMap; //public MapPanel(Dimension maxdim) { public MapPanel() { super(); //Dimension panelSize = dimU.trimToScreenSize(maxdim); //panelSize = dimU.leastUpperBound(panelSize, desiredSize); //panelSize = dimU.scaleDimToAspectRatio(aspectRatio, panelSize); //this.setPreferredSize(panelSize); this.setPreferredSize(desiredSize); this.setBackground(defaultColor); this.setBorder(BorderFactory.createRaisedBevelBorder()); } //end constructor /** Set the map (model) to be drawn on this panel */ public void setMapModel(MapModel m) { //assert m != null; theMap = m; repaint(); } /** Method paintComponent is called AUTOMATICALLY when needed. * The following is general Java stuff, not specific to this project. * Rules: * 1. Don't call this method directly! * You may call repaint(); if you want the picture redrawn at any time * 2. This method should COMPLETELY redraw the ENTIRE image you want * on the panel * 3. Don't call ANY graphics methods from any other method in the program, * unless that method is called only as a result of this one * Not exactly rules, but still: * 1. You probably should not override any other methods of the base class * 2. You probably do not want to declare any objects of this class, * unless you are writing your own GUI. * * If at any time while working on this project, you feel you need to violate * one of those rules, and don't see any way around it -- ask! */ public void paintComponent(Graphics oldG) { //don't change this line //in a Swing application, g is always an instance of Graphics2D Graphics2D g2 = (Graphics2D) oldG; //don't change this line super.paintComponent(g2); //probably not needed if (theMap == null) { return; } Iterator mapIter = theMap.parsedCountyIterator(); Color currBoundaryColor; Color firstPtColor = Color.red; BasicStroke bStroke = new BasicStroke(2.0f); //not as thick as state outline g2.setStroke(bStroke); while (mapIter.hasNext()) { MapModel.CountyInfoUnit info = (MapModel.CountyInfoUnit) mapIter.next(); Shape currPolygon = info.getPolygon(); if (currPolygon == null) { continue; } Color currFillColor = info.fillColor; currBoundaryColor = ColorUtils.average(Color.white, currFillColor); g2.setColor(currBoundaryColor); g2.draw(currPolygon); g2.setColor(currFillColor); g2.fill(currPolygon); MGeomUtils.paintPolygon(g2, (Polygon) currPolygon, Color.black, Color.black); } //All county outlines have been drawn //Draw the state boundary next //Just a list of lines, not a polygon Iterator bIter = theMap.boundaryIterator(); g2.setColor(stateBoundaryColor); while (bIter.hasNext()) { Line2D bLine = (Line2D) bIter.next(); g2.draw(bLine); } Iterator cIter = theMap.parsedCountyIterator(); g2.setColor(Color.yellow); g2.setStroke(new BasicStroke(1.0f)); int symbolRadius = 5; while (cIter.hasNext()) { MapModel.CountyInfoUnit cinfo = (MapModel.CountyInfoUnit) cIter.next(); Point2D location = cinfo.getLocation(); if (location == null) { //signals that we have a polygon without knowing where it is. continue; } int x = (int) location.getX(), y = (int) location.getY(); Ellipse2D cityMarker = new Ellipse2D.Double(x, y, symbolRadius, symbolRadius); g2.draw(cityMarker); g2.drawString(cinfo.getCityName(), x, y); } g2.setColor(Color.blue); //A more careful implementation of the following would use FontMetrics. g2.drawString("Map courtesy of CSE143 Maps Unlimited", messageX, messageY); DateFormat dateForm = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL); String dateString = dateForm.format(new Date()); g2.drawString(dateString, messageX, messageY - 20); } } //end mapPanel