// CS10Display.java // For displaying results of CS 10 assignments. // cmc, updated 8/19/08 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Rectangle; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.border.LineBorder; /** Provides static methods to display results of CS 10 assignments. */ public class CS10Display extends JPanel { // GraphicsEnvironment.isHeadless() returns true if no graphics available private final static boolean NOGRAPHICS = GraphicsEnvironment.isHeadless(); // Maximum size of visible grid private final static int VIEW_SIZE = 400; // instance variables used by showGrid method private Color[][] grid; private Rectangle2D.Double[][] r; private int n, m; // only constructor is private - clients have no need to create an object // used by showGrid only private CS10Display(Color[][] grid) { this.grid = grid; n = grid.length; // okay: showGrid already tested null or 0 length m = 0; for (Color[] row : grid) if (row.length > m) m = row.length; // set to longest row r = new Rectangle2D.Double[n][m]; double stepWidth = (double)VIEW_SIZE / m; double stepHeight = (double)VIEW_SIZE / n; for (int i = 0; i < n; i++) for (int j=0; j < m; j++) r[i][j] = new Rectangle2D.Double(j * stepWidth, i * stepHeight, stepWidth, stepHeight); } /** Prints color values to standard output. @param color the color(s) to print. Can be a single Color, a comma-separated list of Colors, or an array of Colors. */ public static void print(Color... color) { for (Color c : color) System.out.println(componentValuesString(c)); } // utility creates string of color component values as percent private static String componentValuesString(Color c) { return String.format("red=%03d, green=%03d, blue=%03d", c.getRed(), c.getGreen(), c.getBlue()); } /** Displays color(s). Uses graphics display if available. Otherwise just invokes print method. @param color the color(s) to display. Can be a single Color, a comma-separated list of Colors, or an array of Colors. */ public static void show(Color... color) { // do nothing if no colors to show if (color == null || color.length == 0) return; // just print strings if no graphics environment available if (NOGRAPHICS) { System.err.println("No graphics display. " + "Just printing to standard output."); print(color); return; } // otherwise okay to display graphically JPanel panel = new JPanel(); int n = color.length; panel.setLayout(new GridLayout(n, 2)); JLabel label = null; for (Color c : color) { String text = " " + componentValuesString(c) + ": "; label = new JLabel(text, JLabel.LEFT); label.setBorder(new LineBorder(Color.BLACK)); panel.add(label); JPanel canvas = new JPanel(); canvas.setBackground(c); canvas.setBorder(new LineBorder(Color.BLACK)); panel.add(canvas); } JScrollPane scroller = new JScrollPane(panel); double height = label.getPreferredSize().getHeight() * n; if (height > VIEW_SIZE) height = VIEW_SIZE; scroller.setPreferredSize(new Dimension(VIEW_SIZE, (int)height + 10)); JOptionPane.showMessageDialog(null, scroller, "Color Display", JOptionPane.PLAIN_MESSAGE); } /** Displays color grid. Only executes if graphics display is available. @param grid the array of colors to show - both dimensions must be <= 400. */ public static void showGrid(Color[][] grid) { // do nothing if no colors to show if (grid == null || grid.length == 0) return; // just print error message if no graphics environment available if (NOGRAPHICS) { System.err.println("No graphics display. " + "Cannot show grid."); return; } // otherwise okay to display graphically if dimensions in range CS10Display panel = new CS10Display(grid); if (panel.n > VIEW_SIZE || panel.m > VIEW_SIZE) { System.err.println("Grid size exceeds maximum. " + "Cannot show."); return; } panel.setPreferredSize(new Dimension(VIEW_SIZE, VIEW_SIZE)); panel.setBorder(new LineBorder(Color.BLACK)); JOptionPane.showMessageDialog(null, panel, "Color Grid Display", JOptionPane.PLAIN_MESSAGE); } /** Called by the windowing system when showGrid is used. Do not attempt to call directly. @param g the graphics object passed by the windowing system. */ public void paintComponent(Graphics g) { if (grid == null) return; Graphics2D g2 = (Graphics2D)g; // lay down the colors int i = 0; for (Color[] row : grid) { int j = 0; for (Color c : row) { g2.setColor(c); g2.fill(r[i][j]); ++j; } ++i; } // draw the grid lines, rows first, starting with second one g2.setColor(Color.BLACK); if (n < VIEW_SIZE/2) // otherwise only grid shows up for (i = 1; i < n; i++) { double y = r[i][0].getY(); g2.draw(new Line2D.Double(0, y, VIEW_SIZE, y)); } if (m < VIEW_SIZE/2) for (int j = 1; j < m; j++) { double x = r[0][j].getX(); g2.draw(new Line2D.Double(x, 0, x, VIEW_SIZE)); } } }