Font font = new Font("Times", Font.PLAIN, 7);
Envelope bounds = context.getViewportModel().getBounds();
List<Double>[] gridLines = chooseGridLines(bounds);
ViewportGraphics graphics = context.getGraphics();
int mapPixelWidth = context.getMapDisplay().getWidth();
int mapPixelHeight = context.getMapDisplay().getHeight();
graphics.setColor(gridColor);
List<Double> xCoords = gridLines[0];
List<Double> yCoords = gridLines[1];
//draw grid "crosses" and ticks
for (int i = 0; i < xCoords.size(); i++) {
double x = xCoords.get(i).doubleValue();
for (int j = 0; j < yCoords.size() ; j++) {
double y = yCoords.get(j).doubleValue();
Coordinate thisCoord = new Coordinate(x, y);
Point thisPixel = context.worldToPixel(thisCoord);
//ticks along left and right
if (i == 0) {
//draw left-most ticks
graphics.drawLine(EDGE_WIDTH,
thisPixel.y,
EDGE_WIDTH + TICK_WIDTH,
thisPixel.y);
//draw right-most ticks
graphics.drawLine(mapPixelWidth - EDGE_WIDTH,
thisPixel.y,
mapPixelWidth - EDGE_WIDTH - TICK_WIDTH,
thisPixel.y);
}
//ticks along top and bottom
if (j == 0) {
//draw top-most ticks
graphics.drawLine(thisPixel.x,
EDGE_WIDTH,
thisPixel.x,
EDGE_WIDTH + TICK_WIDTH);
//draw bottom-most ticks
graphics.drawLine(thisPixel.x,
mapPixelHeight - EDGE_WIDTH,
thisPixel.x,
mapPixelHeight - EDGE_WIDTH - TICK_WIDTH);
}
//draw crosses
graphics.setColor(Color.YELLOW);
graphics.drawLine(thisPixel.x - TICK_WIDTH/2,
thisPixel.y,
thisPixel.x + TICK_WIDTH/2,
thisPixel.y);
graphics.drawLine(thisPixel.x,
thisPixel.y - TICK_WIDTH/2,
thisPixel.x,
thisPixel.y + TICK_WIDTH/2);
}
} //end crosses and ticks
//top border
graphics.setColor(Color.white);
graphics.fillRect(0,
0,
mapPixelWidth,
EDGE_WIDTH);
//right border
graphics.fillRect(mapPixelWidth - EDGE_WIDTH,
0,
EDGE_WIDTH,
mapPixelHeight);
//bottom border
graphics.fillRect(0,
mapPixelHeight - EDGE_WIDTH,
mapPixelWidth,
EDGE_WIDTH);
//left border
graphics.fillRect(0,
0,
EDGE_WIDTH,
mapPixelHeight);
//top-left corner
graphics.setColor(Color.white);
graphics.fillRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
graphics.setColor(Color.black);
graphics.fillOval(EDGE_WIDTH/2+1, EDGE_WIDTH/2+1, EDGE_WIDTH, EDGE_WIDTH);
graphics.drawRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
//top-right corner
AffineTransform origTrans = graphics.getTransform();
AffineTransform topRightTrans = graphics.getTransform();
topRightTrans.translate(mapPixelWidth - EDGE_WIDTH*2, 0);
graphics.setTransform(topRightTrans);
graphics.setColor(Color.white);
graphics.fillRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
graphics.setColor(Color.black);
graphics.fillOval(EDGE_WIDTH/2+1, EDGE_WIDTH/2+1, EDGE_WIDTH, EDGE_WIDTH);
graphics.drawRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
//bottom-right corner
AffineTransform bottomRightTrans = (AffineTransform)origTrans.clone();
bottomRightTrans.translate(mapPixelWidth - EDGE_WIDTH*2, mapPixelHeight - EDGE_WIDTH*2);
graphics.setTransform(bottomRightTrans);
graphics.setColor(Color.white);
graphics.fillRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
graphics.setColor(Color.black);
graphics.fillOval(EDGE_WIDTH/2+1, EDGE_WIDTH/2+1, EDGE_WIDTH, EDGE_WIDTH);
graphics.drawRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
//Note: bottom left corner is drawn after the outline... see below
//outer outline
graphics.setTransform(origTrans);
graphics.setColor(Color.black);
graphics.setStroke(ViewportGraphics.LINE_SOLID, 1);
graphics.drawRect(0,
0,
mapPixelWidth-1,
mapPixelHeight-1);
//inner outline, extends to edges
graphics.drawLine(0, EDGE_WIDTH, mapPixelWidth, EDGE_WIDTH); //top
graphics.drawLine(mapPixelWidth - EDGE_WIDTH, 0, mapPixelWidth - EDGE_WIDTH, mapPixelHeight); //right
graphics.drawLine(0, mapPixelHeight - EDGE_WIDTH, mapPixelWidth, mapPixelHeight - EDGE_WIDTH); //bottom
graphics.drawLine(EDGE_WIDTH, 0, EDGE_WIDTH, mapPixelHeight); //left
//bottom left corner
AffineTransform bottomleftTrans = graphics.getTransform();
bottomleftTrans.translate(0, mapPixelHeight - EDGE_WIDTH*2);
graphics.setTransform(bottomleftTrans);
graphics.setColor(Color.white);
graphics.fillRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
graphics.setColor(Color.black);
graphics.drawOval(EDGE_WIDTH/4, EDGE_WIDTH/4, EDGE_WIDTH*2 - EDGE_WIDTH/2, EDGE_WIDTH*2 - EDGE_WIDTH/2);
graphics.fillOval(EDGE_WIDTH - 1, EDGE_WIDTH - 1, 2, 2);
graphics.drawRect(0,
0,
EDGE_WIDTH*2,
EDGE_WIDTH*2);
graphics.setTransform(origTrans);
//-------------------------
//draw labels
//-------------------------
graphics.setFont(font);
graphics.setColor(Color.black);
drawVerticalLabels(bounds, graphics, context, xCoords, yCoords, mapPixelWidth, mapPixelHeight);
drawHorizontalLeftLabels(bounds, graphics, context, xCoords, yCoords, mapPixelWidth, mapPixelHeight);
drawHorizontalRightLabels(bounds, graphics, context, xCoords, yCoords, mapPixelWidth, mapPixelHeight);