shp_renderer
Class SHPRenderer

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by shp_renderer.SHPRenderer
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class SHPRenderer
extends javax.swing.JPanel

SHPRenderer - This class does all the map rendering inside of a panel (itself). It knows how to render the shapes as well as the map lines. It also manages the viewport, which means the window on the map that we are currently viewing.

Author:
Richard McKenna
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.BaselineResizeBehavior
 
Field Summary
 double EQUATOR
           
 double PRIME_MERIDIAN
           
 double TROPIC_OF_CANCER
           
 double TROPIC_OF_CAPRICORN
           
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
SHPRenderer(SHPViewer initViewer)
          This constructor gives default values to the viewport and background (blue), but it doesn't know what to draw until the user selects an .shp file.
 
Method Summary
 void clearSelectionRect()
          This method updates a rectangle to all zero, making it no longer visible.
 void correctViewport()
          This method makes sure the viewport has not gone off the map.
 void decViewportCenterX()
          Move the viewport left.
 void decViewportCenterY()
          Move the viewport down.
 double getViewportCenterX()
          This method returns the x value for the viewport center.
 double getViewportCenterY()
          This method returns the y value for the viewport center.
 void incViewportCenterX()
          Move the viewport right.
 void incViewportCenterY()
          Move the viewport up.
 void initBatchRenderingData()
          This method updates necessary rendering variables that may change if the user chooses to zoom in or move the viewport.
 void initPoliticalMapColors()
          This method will provide the user with a political view of the world map.
 void initStatisticalMapColors()
          This method should be called whenever the user wants to view a statistical view of the world map.
 boolean isInViewport(SHPShape shape)
          This method is to be used as a rejection test to make sure we don't bother trying to rendering shapes that are not inside the viewport.
 javax.swing.JPanel mapLegend()
          This method creates a map legend to be render onto the map.
 void paintComponent(java.awt.Graphics g)
          This method is called by the Swing and AWT libraries every time our panel is displayed and then when we repaint it.
 double pixelToXCoordinate(int xPixel)
          This calculates and returns the longitude value that corresponds to the xPixel argument.
 double pixelToYCoordinate(int yPixel)
          This calculates and returns the latitude value that corresponds to the yPixel argument.
 void renderMap(java.awt.Graphics g)
          This method is the root method for doing the actual rendering of EVERYTHING that we actually want to appear inside the panel.
 void renderMapLine(java.awt.Graphics2D g2, double line, double min, double max, boolean isVertical)
          This method renders a single map line.
 void renderMapLines(java.awt.Graphics2D g2)
          This method is responsible for rendering all of the map lines, including the equator, the tropics, and the lat and long incremental lines.
 void resetOutlinedPolygon()
          This method resets the outlined polygon to null, no polygon outlined.
 void setCenter(double longCenter, double latCenter)
          This method moves the viewport to the arguments location, correcting if necessary.
 void setCenter(int x, int y)
          This method moves the viewport to the pixel location, assuming the user has clicked there.
 void setOutlinedPolygon(SHPPolygon initPoly)
          This method sets the outlined polygon to the new input value.
 void updateSelectionRect(int x1, int y1, int x2, int y2)
          This method updates a rectangle with new points.
 int xCoordinateToPixel(double xCoord)
          This calculates and returns the x pixel value that corresponds to the xCoord longitude argument.
 int yCoordinateToPixel(double yCoord)
          This calculates and returns the y pixel value that corresponds to the yCoord latitude argument.
 void zoom(int x1, int y1, int x2, int y2)
          This method will zoom our viewport in on the region represented by the x,y arguments.
 void zoomFarIn()
          Zoom way in and correct if necessary.
 void zoomFarOut()
          Zoom way out and correct if necessary.
 void zoomIn()
          Zoom the viewport slightly in.
 void zoomOut()
          Zoom the viewport slightly out and correct if necessary.
 void zoomToMapBounds()
          This can be used to zoom to perfectly fit the bounding box of a shape, so the viewport moves right to a country, for example.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TROPIC_OF_CANCER

public double TROPIC_OF_CANCER

EQUATOR

public double EQUATOR

TROPIC_OF_CAPRICORN

public double TROPIC_OF_CAPRICORN

PRIME_MERIDIAN

public double PRIME_MERIDIAN
Constructor Detail

SHPRenderer

public SHPRenderer(SHPViewer initViewer)
This constructor gives default values to the viewport and background (blue), but it doesn't know what to draw until the user selects an .shp file.

Parameters:
initViewer - The SHPViewer that the renderer will be added to.
Method Detail

getViewportCenterX

public double getViewportCenterX()
This method returns the x value for the viewport center.

Returns:
The viewport center x value.

getViewportCenterY

public double getViewportCenterY()
This method returns the y value for the viewport center.

Returns:
The viewport center y value.

setOutlinedPolygon

public void setOutlinedPolygon(SHPPolygon initPoly)
This method sets the outlined polygon to the new input value.

Parameters:
initPoly - The polygon to outline.

resetOutlinedPolygon

public void resetOutlinedPolygon()
This method resets the outlined polygon to null, no polygon outlined.


updateSelectionRect

public void updateSelectionRect(int x1,
                                int y1,
                                int x2,
                                int y2)
This method updates a rectangle with new points.

Parameters:
x1 - Top left point.
y1 - Bottom left point.
x2 - Top right point.
y2 - Bottom right point.

clearSelectionRect

public void clearSelectionRect()
This method updates a rectangle to all zero, making it no longer visible.


paintComponent

public void paintComponent(java.awt.Graphics g)
This method is called by the Swing and AWT libraries every time our panel is displayed and then when we repaint it. All needed rendering on the panel must be done here each time.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - The graphics to paint with.

initPoliticalMapColors

public void initPoliticalMapColors()
This method will provide the user with a political view of the world map. By default the world map is set to a political view. When the user switches to a political view this method is called As it is, it generates random colors for each polygon individually, with those near the north pole lighter and with polygons getting gradually darker as we go south.


initStatisticalMapColors

public void initStatisticalMapColors()
This method should be called whenever the user wants to view a statistical view of the world map. As it is, it generates a random color, with those near the north pole lighter and with polygons getting gradually darker as we go south.


initBatchRenderingData

public void initBatchRenderingData()
This method updates necessary rendering variables that may change if the user chooses to zoom in or move the viewport.


renderMap

public void renderMap(java.awt.Graphics g)
This method is the root method for doing the actual rendering of EVERYTHING that we actually want to appear inside the panel.

Parameters:
g - The graphics to paint with.

isInViewport

public boolean isInViewport(SHPShape shape)
This method is to be used as a rejection test to make sure we don't bother trying to rendering shapes that are not inside the viewport. If the shape's bounding box does not overlap the viewport this method returns false, true if it does. Note this just uses a basic method of separating axis test.

Parameters:
shape - The SHPShape to test.
Returns:
Whether or not the shape is in the viewport.

renderMapLines

public void renderMapLines(java.awt.Graphics2D g2)
This method is responsible for rendering all of the map lines, including the equator, the tropics, and the lat and long incremental lines.

Parameters:
g2 - Used to change stroke and color of the lines being renderer.

renderMapLine

public void renderMapLine(java.awt.Graphics2D g2,
                          double line,
                          double min,
                          double max,
                          boolean isVertical)
This method renders a single map line.

Parameters:
g2 - Used to draw the lines.
line - The line to be drawn.
min - Minimum location for a line.
max - Maximum location for a line.
isVertical - Whether or not the line is a vertical line.

mapLegend

public javax.swing.JPanel mapLegend()
This method creates a map legend to be render onto the map. A JPanel with custom size(200,500), color(Grey), location(10,300) is created.

Returns:
The map legend

incViewportCenterX

public void incViewportCenterX()
Move the viewport right.


decViewportCenterX

public void decViewportCenterX()
Move the viewport left.


incViewportCenterY

public void incViewportCenterY()
Move the viewport up.


decViewportCenterY

public void decViewportCenterY()
Move the viewport down.


correctViewport

public void correctViewport()
This method makes sure the viewport has not gone off the map. If the latitude or longitude is illegal, it gets corrected.


setCenter

public void setCenter(double longCenter,
                      double latCenter)
This method moves the viewport to the arguments location, correcting if necessary.

Parameters:
longCenter - The longitude value of the center.
latCenter - The latitude value of the center.

setCenter

public void setCenter(int x,
                      int y)
This method moves the viewport to the pixel location, assuming the user has clicked there. Note that this method takes pixel locations x and y and must convert them into corresponding lat/lon values.

Parameters:
x - The new x value for the center.
y - The new y value for the center.

zoomIn

public void zoomIn()
Zoom the viewport slightly in.


zoomOut

public void zoomOut()
Zoom the viewport slightly out and correct if necessary.


zoomFarOut

public void zoomFarOut()
Zoom way out and correct if necessary.


zoomFarIn

public void zoomFarIn()
Zoom way in and correct if necessary.


zoom

public void zoom(int x1,
                 int y1,
                 int x2,
                 int y2)
This method will zoom our viewport in on the region represented by the x,y arguments. Note that these are pixel coordinates, so we must convert to lat,long. Also note that we have to determine the proper scale.

Parameters:
x1 - New top left value of the viewport.
y1 - New bottom left value of the viewport.
x2 - New top right value of the viewport.
y2 - New bottom right value of the viewport.

zoomToMapBounds

public void zoomToMapBounds()
This can be used to zoom to perfectly fit the bounding box of a shape, so the viewport moves right to a country, for example.


pixelToXCoordinate

public double pixelToXCoordinate(int xPixel)
This calculates and returns the longitude value that corresponds to the xPixel argument.

Parameters:
xPixel - The x value in terms of pixels.
Returns:
The coordinate value of x.

pixelToYCoordinate

public double pixelToYCoordinate(int yPixel)
This calculates and returns the latitude value that corresponds to the yPixel argument.

Parameters:
yPixel - The y value in terms of pixels.
Returns:
The coordinate value of y.

xCoordinateToPixel

public int xCoordinateToPixel(double xCoord)
This calculates and returns the x pixel value that corresponds to the xCoord longitude argument.

Parameters:
xCoord - The x value coordinate.
Returns:
The value of x in terms of pixels.

yCoordinateToPixel

public int yCoordinateToPixel(double yCoord)
This calculates and returns the y pixel value that corresponds to the yCoord latitude argument.

Parameters:
yCoord - The y value coordinate.
Returns:
The value of y in terms of pixels.