001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.bio.gui; 022 023import java.awt.Color; 024import java.awt.Font; 025import java.awt.Graphics2D; 026import java.awt.Shape; 027import java.awt.font.FontRenderContext; 028import java.awt.font.GlyphVector; 029import java.awt.geom.AffineTransform; 030import java.awt.geom.Rectangle2D; 031 032import org.biojava.bio.BioError; 033import org.biojava.bio.BioException; 034import org.biojava.bio.BioRuntimeException; 035import org.biojava.bio.dist.Distribution; 036import org.biojava.bio.seq.io.SymbolTokenization; 037import org.biojava.bio.symbol.AtomicSymbol; 038import org.biojava.bio.symbol.IllegalSymbolException; 039 040/** 041 * A BlockPainter that renders letters in proportion to the size of the signal. 042 * 043 * @author Matthew Pocock 044 * @author Thomas Down 045 */ 046public class TextBlock implements BlockPainter { 047 private Font logoFont = new Font("Tahoma", Font.PLAIN, 12); 048 049 /** 050 * Retrieve the current font. 051 * 052 * @return the current logo font 053 */ 054 public Font getLogoFont() { 055 return logoFont; 056 } 057 058 /** 059 * Set the current logo font. 060 * 061 * @param logoFont the new Font to render the logo letters in 062 */ 063 public void setLogoFont(Font logoFont) { 064 this.logoFont = logoFont; 065 } 066 067 public void paintBlock(LogoContext ctxt, Rectangle2D block, AtomicSymbol sym) { 068 Graphics2D g2 = ctxt.getGraphics(); 069 SymbolStyle style = ctxt.getStyle(); 070 Distribution dist = ctxt.getDistribution(); 071 SymbolTokenization toke = null; 072 try { 073 toke = dist.getAlphabet().getTokenization("token"); 074 } catch (BioException ex) { 075 throw new BioRuntimeException(ex); 076 } 077 078 FontRenderContext frc = g2.getFontRenderContext(); 079 try { 080 GlyphVector gv = logoFont.createGlyphVector(frc, toke.tokenizeSymbol(sym)); 081 082 Shape outline = gv.getOutline(); 083 Rectangle2D oBounds = outline.getBounds2D(); 084 085 AffineTransform at = new AffineTransform(); 086 at.setToTranslation(block.getX(), block.getY()); 087 at.scale( 088 block.getWidth() / oBounds.getWidth(), 089 block.getHeight() / oBounds.getHeight() 090 ); 091 at.translate(-oBounds.getMinX(), -oBounds.getMinY()); 092 outline = at.createTransformedShape(outline); 093 094 try { 095 g2.setPaint(style.fillPaint(sym)); 096 } catch (IllegalSymbolException ire) { 097 g2.setPaint(Color.black); 098 } 099 g2.fill(outline); 100 101 try { 102 g2.setPaint(style.outlinePaint(sym)); 103 } catch (IllegalSymbolException ire) { 104 g2.setPaint(Color.gray); 105 } 106 g2.draw(outline); 107 } catch (IllegalSymbolException ex) { 108 throw new BioError("Couldn't tokenize symbol", ex); 109 } 110 } 111}