Clover coverage report - baseCode - 0.2.5
Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT
file stats: LOC: 483   Methods: 46
NCLOC: 281   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
JMatrixDisplay.java 0% 0% 0% 0%
coverage
 1   
 package baseCode.gui;
 2   
 
 3   
 import java.awt.Color;
 4   
 import java.awt.Dimension;
 5   
 import java.awt.Font;
 6   
 import java.awt.Graphics;
 7   
 import java.awt.Graphics2D;
 8   
 import java.awt.image.BufferedImage;
 9   
 import java.io.File;
 10   
 import java.io.IOException;
 11   
 
 12   
 import javax.imageio.ImageIO;
 13   
 import javax.swing.JPanel;
 14   
 
 15   
 import baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix;
 16   
 import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
 17   
 import baseCode.graphics.text.Util;
 18   
 
 19   
 /**
 20   
  * <p>
 21   
  * Title: JMatrixDisplay
 22   
  * </p>
 23   
  * <p>
 24   
  * Description: a visual component for displaying a color matrix
 25   
  * </p>
 26   
  * <p>
 27   
  * Copyright: Copyright (c) 2004
 28   
  * </p>
 29   
  * <p>
 30   
  * Institution:: Columbia University
 31   
  * </p>
 32   
  * 
 33   
  * @author Will Braynen
 34   
  * @version $Id: JMatrixDisplay.java,v 1.39 2004/09/20 22:19:02 pavlidis Exp $
 35   
  */
 36   
 public class JMatrixDisplay extends JPanel {
 37   
 
 38   
    // data fields
 39   
    ColorMatrix m_matrix; // reference to standardized or unstandardized matrix
 40   
    ColorMatrix m_unstandardizedMatrix;
 41   
    ColorMatrix m_standardizedMatrix;
 42   
    boolean m_isShowingStandardizedMatrix = false;
 43   
 
 44   
    protected boolean m_isShowLabels = false;
 45   
    protected BufferedImage m_image = null;
 46   
 
 47   
    protected int m_ratioWidth = 0;
 48   
    protected int m_rowLabelWidth; // max
 49   
    protected int m_columnLabelHeight; // max
 50   
    protected int m_labelGutter = 5;
 51   
    protected int m_fontGutter;
 52   
    protected Font m_labelFont = null;
 53   
    protected int m_fontSize = 10;
 54   
    protected final int m_maxFontSize = 10;
 55   
    protected final int m_defaultResolution = 120;
 56   
    protected int m_resolution = m_defaultResolution;
 57   
    protected int m_textSize = 0;
 58   
 
 59   
    protected Dimension m_cellSize = new Dimension( 10, 10 ); // in pixels
 60   
 
 61  0
    public JMatrixDisplay( String filename ) throws IOException {
 62   
 
 63  0
       ColorMatrix matrix = new ColorMatrix( filename );
 64  0
       init( matrix );
 65   
    }
 66   
 
 67  0
    public JMatrixDisplay( DenseDoubleMatrix2DNamed matrix ) {
 68  0
       this( new ColorMatrix( matrix ) );
 69   
    }
 70   
 
 71  0
    public JMatrixDisplay( ColorMatrix matrix ) {
 72  0
       init( matrix );
 73   
    }
 74   
 
 75  0
    public void init( ColorMatrix matrix ) {
 76   
 
 77  0
       m_unstandardizedMatrix = m_matrix = matrix;
 78  0
       initSize();
 79   
 
 80   
       // create a standardized copy of the matrix
 81  0
       m_standardizedMatrix = ( ColorMatrix ) matrix.clone();
 82  0
       m_standardizedMatrix.standardize();
 83   
 
 84   
    }
 85   
 
 86   
    /**
 87   
     * Sets the display size
 88   
     */
 89  0
    protected void initSize() {
 90   
 
 91  0
       Dimension d = getSize( m_isShowLabels );
 92  0
       setMinimumSize( d );
 93  0
       setPreferredSize( d );
 94  0
       setSize( d );
 95  0
       this.revalidate();
 96   
    }
 97   
 
 98  0
    protected Dimension getSize( boolean withLabels ) {
 99   
 
 100  0
       if ( m_matrix == null ) {
 101  0
          return null;
 102   
       }
 103   
 
 104   
       // row label width and height (font-dependent)
 105  0
       setFont();
 106  0
       m_rowLabelWidth = m_labelGutter
 107   
             + Util.maxStringPixelWidth( m_matrix.getRowNames(), m_labelFont,
 108   
                   this );
 109   
       //m_rowLabelWidth += m_labelGutter; // this is optional (leaves some
 110   
       // space on the right)
 111  0
       m_columnLabelHeight = Util.maxStringPixelWidth(
 112   
             m_matrix.getColumnNames(), m_labelFont, this );
 113   
       //m_columnLabelHeight += m_labelGutter; // this is optional (leaves some
 114   
       // space on top)
 115   
 
 116   
       // height and width of this display component
 117  0
       int height = m_cellSize.height * m_matrix.getRowCount();
 118  0
       int width = m_cellSize.width * m_matrix.getColumnCount();
 119   
 
 120   
       // adjust for row and column labels
 121  0
       if ( withLabels ) {
 122  0
          width += m_rowLabelWidth;
 123  0
          height += ( m_columnLabelHeight + m_labelGutter );
 124   
       }
 125   
 
 126   
       // set the sizes
 127  0
       return new Dimension( width, height );
 128   
 
 129   
    } // end getSize
 130   
 
 131   
    /**
 132   
     * <code>JComponent</code> method used to render this component
 133   
     * 
 134   
     * @param g Graphics used for painting
 135   
     */
 136  0
    protected void paintComponent( Graphics g ) {
 137   
 
 138  0
       super.paintComponent( g );
 139  0
       drawMatrix( g, m_isShowLabels );
 140   
 
 141  0
       if ( m_isShowLabels ) {
 142  0
          drawRowNames( g );
 143  0
          drawColumnNames( g );
 144   
       }
 145   
    } // end paintComponent
 146   
 
 147  0
    public void setStandardizedEnabled( boolean showStandardizedMatrix ) {
 148  0
       m_isShowingStandardizedMatrix = showStandardizedMatrix;
 149  0
       if ( showStandardizedMatrix ) {
 150  0
          m_matrix = m_standardizedMatrix;
 151   
       } else {
 152  0
          m_matrix = m_unstandardizedMatrix;
 153   
       }
 154   
    } // end setStandardizedEnabled
 155   
 
 156  0
    public boolean getStandardizedEnabled() {
 157   
 
 158  0
       return m_isShowingStandardizedMatrix;
 159   
    }
 160   
 
 161   
    /**
 162   
     * Gets called from #paintComponent and #saveImage
 163   
     * 
 164   
     * @param g Graphics
 165   
     * @param leaveRoomForLabels boolean
 166   
     */
 167  0
    protected void drawMatrix( Graphics g, boolean leaveRoomForLabels ) {
 168   
 
 169  0
       g.setColor( Color.white );
 170  0
       g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
 171   
 
 172  0
       int rowCount = m_matrix.getRowCount();
 173  0
       int columnCount = m_matrix.getColumnCount();
 174   
 
 175   
       // loop through the matrix, one row at a time
 176  0
       for ( int i = 0; i < rowCount; i++ ) {
 177  0
          int y = ( i * m_cellSize.height );
 178  0
          if ( leaveRoomForLabels ) {
 179  0
             y += ( m_columnLabelHeight + m_labelGutter );
 180   
          }
 181   
 
 182   
          // draw an entire row, one cell at a time
 183  0
          for ( int j = 0; j < columnCount; j++ ) {
 184  0
             int x = ( j * m_cellSize.width );
 185  0
             int width = ( ( j + 1 ) * m_cellSize.width ) - x;
 186   
 
 187  0
             Color color = m_matrix.getColor( i, j );
 188  0
             g.setColor( color );
 189  0
             g.fillRect( x, y, width, m_cellSize.height );
 190   
          }
 191   
 
 192   
       } // end looping through the matrix, one row at a time
 193   
    } // end drawMatrix
 194   
 
 195   
    /**
 196   
     * Draws row names (horizontally)
 197   
     * 
 198   
     * @param g Graphics
 199   
     */
 200  0
    protected void drawRowNames( Graphics g ) {
 201   
 
 202  0
       if ( m_matrix == null ) return;
 203   
 
 204  0
       int rowCount = m_matrix.getRowCount();
 205   
 
 206   
       // draw row names
 207  0
       for ( int i = 0; i < rowCount; i++ ) {
 208  0
          g.setColor( Color.black );
 209  0
          g.setFont( m_labelFont );
 210  0
          int y = ( i * m_cellSize.height ) + m_columnLabelHeight
 211   
                + m_labelGutter;
 212  0
          int xRatio = ( m_matrix.getColumnCount() * m_cellSize.width )
 213   
                + m_labelGutter;
 214  0
          int yRatio = y + m_cellSize.height - m_fontGutter;
 215  0
          String rowName = m_matrix.getRowName( i );
 216  0
          if ( null == rowName ) {
 217  0
             rowName = "Undefined";
 218   
          }
 219  0
          g.drawString( rowName, xRatio, yRatio );
 220   
       } // end drawing row names
 221   
    } // end rawRowName
 222   
 
 223   
    /**
 224   
     * Draws column names vertically (turned 90 degrees counter-clockwise)
 225   
     * 
 226   
     * @param g Graphics
 227   
     */
 228  0
    protected void drawColumnNames( Graphics g ) {
 229   
 
 230  0
       if ( m_matrix == null ) return;
 231   
 
 232  0
       int columnCount = m_matrix.getColumnCount();
 233  0
       for ( int j = 0; j < columnCount; j++ ) {
 234   
          // compute the coordinates
 235  0
          int x = m_cellSize.width + ( j * m_cellSize.width ) - m_fontGutter;
 236  0
          int y = m_columnLabelHeight;
 237   
 
 238   
          // get column name
 239  0
          String columnName = m_matrix.getColumnName( j );
 240  0
          if ( null == columnName ) {
 241  0
             columnName = "Undefined";
 242   
          }
 243   
 
 244   
          // set font and color
 245  0
          g.setColor( Color.black );
 246  0
          g.setFont( m_labelFont );
 247   
 
 248   
          // print the text vertically
 249  0
          Util.drawVerticalString( g, columnName, m_labelFont, x, y );
 250   
 
 251   
       } // end for column
 252   
    } // end drawColumnNames
 253   
 
 254   
    /**
 255   
     * Sets the font used for drawing text
 256   
     */
 257  0
    private void setFont() {
 258  0
       int fontSize = Math
 259   
             .min( getFontSize(), ( int ) ( ( double ) m_maxFontSize
 260   
                   / ( double ) m_defaultResolution * m_resolution ) );
 261  0
       if ( ( fontSize != m_fontSize ) || ( m_labelFont == null ) ) {
 262  0
          m_fontSize = fontSize;
 263  0
          m_labelFont = new Font( "Ariel", Font.PLAIN, m_fontSize );
 264  0
          m_fontGutter = ( int ) ( m_cellSize.height * .22 );
 265   
       }
 266   
    }
 267   
 
 268   
    /**
 269   
     * @return the height of the font
 270   
     */
 271  0
    private int getFontSize() {
 272  0
       return Math.max( m_cellSize.height, 5 );
 273   
    }
 274   
 
 275   
    /**
 276   
     * Saves the image to a png file.
 277   
     * 
 278   
     * @param outPngFilename String
 279   
     * @throws IOException
 280   
     */
 281  0
    public void saveImage( String outPngFilename ) throws java.io.IOException {
 282  0
       saveImage( outPngFilename, m_isShowLabels, m_isShowingStandardizedMatrix );
 283   
    }
 284   
 
 285  0
    public void saveImage( String outPngFilename, boolean showLabels )
 286   
          throws java.io.IOException {
 287  0
       saveImage( outPngFilename, showLabels, m_isShowingStandardizedMatrix );
 288   
    }
 289   
 
 290   
    /**
 291   
     * @param outPngFilename String
 292   
     * @param showLabels boolean
 293   
     * @param standardize normalize to deviation 1, mean 0.
 294   
     * @todo never read
 295   
     * @throws IOException
 296   
     */
 297  0
    public void saveImage( String outPngFilename, boolean showLabels,
 298   
          boolean standardize ) throws java.io.IOException {
 299   
 
 300  0
       Graphics2D g = null;
 301   
 
 302   
       // Include row and column labels?
 303  0
       boolean wereLabelsShown = m_isShowLabels;
 304  0
       if ( !wereLabelsShown ) {
 305   
          // Labels aren't visible, so make them visible
 306  0
          setLabelsVisible( true );
 307  0
          initSize();
 308   
       }
 309   
 
 310   
       // Draw the image to a buffer
 311  0
       Dimension d = getSize( showLabels ); // how big is the image with row and
 312   
       // column labels
 313  0
       m_image = new BufferedImage( d.width, d.height,
 314   
             BufferedImage.TYPE_INT_RGB );
 315  0
       g = m_image.createGraphics();
 316  0
       drawMatrix( g, showLabels );
 317  0
       if ( showLabels ) {
 318  0
          drawRowNames( g );
 319  0
          drawColumnNames( g );
 320   
       }
 321   
 
 322   
       // Write the image to a png file
 323  0
       ImageIO.write( m_image, "png", new File( outPngFilename ) );
 324   
 
 325   
       // Restore state: make the image as it was before
 326  0
       if ( !wereLabelsShown ) {
 327   
          // Labels weren't visible to begin with, so hide them
 328  0
          setLabelsVisible( false );
 329  0
          initSize();
 330   
       }
 331   
    } // end saveImage
 332   
 
 333   
    /**
 334   
     * If this display component has already been added to the GUI, it will be resized to fit or exclude the row names
 335   
     * 
 336   
     * @param isShowLabels boolean
 337   
     */
 338  0
    public void setLabelsVisible( boolean isShowLabels ) {
 339  0
       m_isShowLabels = isShowLabels;
 340  0
       initSize();
 341   
    }
 342   
 
 343  0
    public ColorMatrix getColorMatrix() {
 344  0
       return m_matrix;
 345   
    }
 346   
 
 347  0
    public AbstractNamedDoubleMatrix getMatrix() {
 348  0
       return m_matrix.m_matrix;
 349   
    }
 350   
 
 351   
    /**
 352   
     * @param matrix the new matrix to use; will resize this display component as necessary
 353   
     */
 354  0
    public void setMatrix( ColorMatrix matrix ) {
 355  0
       m_matrix = matrix;
 356  0
       initSize();
 357   
    }
 358   
 
 359  0
    public void setCellSize( Dimension d ) {
 360   
 
 361  0
       m_cellSize = d;
 362  0
       initSize();
 363   
    }
 364   
 
 365  0
    public void setRowHeight( int height ) {
 366   
 
 367  0
       m_cellSize.height = height;
 368  0
       initSize();
 369   
    }
 370   
 
 371  0
    public int getRowHeight() {
 372  0
       return m_cellSize.height;
 373   
    }
 374   
 
 375  0
    public Color getColor( int row, int column ) {
 376  0
       return m_matrix.getColor( row, column );
 377   
    } // end getColor
 378   
 
 379  0
    public double getValue( int row, int column ) {
 380  0
       return m_matrix.getValue( row, column );
 381   
    } // end getValue
 382   
 
 383  0
    public double[] getRow( int row ) {
 384  0
       return m_matrix.getRow( row );
 385   
    }
 386   
 
 387  0
    public double[] getRowByName( String rowName ) {
 388  0
       return m_matrix.getRowByName( rowName );
 389   
    }
 390   
 
 391  0
    public int getRowCount() {
 392  0
       return m_matrix.getRowCount();
 393   
    }
 394   
 
 395  0
    public int getColumnCount() {
 396  0
       return m_matrix.getColumnCount();
 397   
    }
 398   
 
 399  0
    public String getColumnName( int column ) {
 400  0
       return m_matrix.getColumnName( column );
 401   
    }
 402   
 
 403  0
    public String getRowName( int row ) {
 404  0
       return m_matrix.getRowName( row );
 405   
    }
 406   
 
 407  0
    public String[] getColumnNames() {
 408  0
       return m_matrix.getColumnNames();
 409   
    }
 410   
 
 411  0
    public String[] getRowNames() {
 412  0
       return m_matrix.getRowNames();
 413   
    }
 414   
 
 415  0
    public int getRowIndexByName( String rowName ) {
 416  0
       return m_matrix.getRowIndexByName( rowName );
 417   
    }
 418   
 
 419  0
    public void setRowKeys( int[] rowKeys ) {
 420  0
       m_matrix.setRowKeys( rowKeys );
 421   
    }
 422   
 
 423  0
    public void resetRowKeys() {
 424  0
       m_matrix.resetRowKeys();
 425   
    }
 426   
 
 427   
    /**
 428   
     * @param colorMap an array of colors which define the midpoints in the color map; this can be one of the constants
 429   
     *        defined in the ColorMap class, like ColorMap.REDGREEN_COLORMAP and ColorMap.BLACKBODY_COLORMAP
 430   
     */
 431  0
    public void setColorMap( Color[] colorMap ) {
 432   
 
 433  0
       m_standardizedMatrix.setColorMap( colorMap );
 434  0
       m_unstandardizedMatrix.setColorMap( colorMap );
 435   
    }
 436   
 
 437   
    /**
 438   
     * @return the current color map
 439   
     */
 440  0
    public Color[] getColorMap() {
 441  0
       return m_matrix.m_colorMap;
 442   
    }
 443   
 
 444   
    /**
 445   
     * @return the smallest value in the matrix
 446   
     */
 447  0
    public double getMin() {
 448  0
       return m_matrix.m_min;
 449   
    }
 450   
 
 451   
    /**
 452   
     * @return the largest value in the matrix
 453   
     */
 454  0
    public double getMax() {
 455  0
       return m_matrix.m_max;
 456   
    }
 457   
 
 458  0
    public double getDisplayMin() {
 459  0
       return m_matrix.m_displayMin;
 460   
    }
 461   
 
 462  0
    public double getDisplayMax() {
 463  0
       return m_matrix.m_displayMax;
 464   
    }
 465   
 
 466  0
    public double getDisplayRange() {
 467  0
       return m_matrix.m_displayMax - m_matrix.m_displayMin;
 468   
    }
 469   
 
 470  0
    public void setDisplayRange( double min, double max ) {
 471  0
       m_matrix.setDisplayRange( min, max );
 472   
    }
 473   
 
 474   
    /**
 475   
     * @return the color used for missing values
 476   
     */
 477  0
    public Color getMissingColor() {
 478  0
       return m_matrix.m_missingColor;
 479   
    }
 480   
 
 481   
 } // end class JMatrixDisplay
 482   
 
 483