/** * A visual display for GridCol. * * @author Kyle Burke <paithanq@gmail.com> */ //package packageName import javax.swing.*; import java.util.*; import java.awt.*; public class GridColPanel extends JPanel { /* constants */ /* fields */ private GridCol position; //Map of integers to appropriate colors //initialize this outside of the constructor because it's static. private final Map<Integer, Color> intsToColors = new TreeMap<Integer, Color>(); /* constructors */ /** * Creates a new instance of GridColPanel. */ public GridColPanel(GridCol position) { this.position = position; //set up the ints->Colors map this.intsToColors.put(Col.BLUE, Color.BLUE); this.intsToColors.put(Col.RED, Color.RED); this.intsToColors.put(Col.UNCOLORED, Color.WHITE); } /* public methods */ //@override public void paintComponent(Graphics g) { int gridHeight = this.position.getHeight(); int gridWidth = this.position.getWidth(); int panelHeightInPixels = this.getHeight(); int panelWidthInPixels = this.getWidth(); g.clearRect(0, 0, panelWidthInPixels, panelHeightInPixels); int pixelsPerColumn = panelWidthInPixels / (gridWidth); int pixelsPerRow = panelHeightInPixels / (gridHeight); int radius = Math.min(pixelsPerRow, pixelsPerColumn)/5; int buffer = radius / 2; for (int row = 0; row < gridHeight; row++) { for (int column = 0; column < gridWidth; column++) { //get the color for the node int id = column + row*gridWidth; Vertex<Integer> vertex = this.position.getGraph().getVertexById(id); Color vertexColor = this.intsToColors.get(vertex.getValue()); //draw the square g.setColor(vertexColor); g.drawRect(buffer + radius + pixelsPerColumn * column, buffer + radius + pixelsPerRow * row, 2 * radius, 2 * radius); } } } /* hidden methods (private/protected) (JavaDoc not necessary) */ /* main method for testing */ /** * Unit test for GridColPanel * @param args Arguments used to test this class. */ public static void main(String[] args) { } } //end of GridColPanel