2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.renderer;
23 import jalview.api.AlignmentColsCollectionI;
24 import jalview.api.AlignmentRowsCollectionI;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.seqfeatures.FeatureColourFinder;
29 import jalview.renderer.seqfeatures.FeatureRenderer;
30 import jalview.viewmodel.OverviewDimensions;
32 import java.awt.AlphaComposite;
33 import java.awt.Color;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.image.BufferedImage;
38 public class OverviewRenderer
40 // transparency of hidden cols/seqs overlay
41 private final float TRANSPARENCY = 0.5f;
43 private FeatureColourFinder finder;
45 private jalview.api.SequenceRenderer sr;
48 private BufferedImage miniMe;
50 // raw number of pixels to allocate to each column
51 private float pixelsPerCol;
53 // raw number of pixels to allocate to each row
54 private float pixelsPerSeq;
56 // flag to indicate whether to halt drawing
57 private volatile boolean redraw = false;
59 public OverviewRenderer(jalview.api.SequenceRenderer seqRenderer,
60 FeatureRenderer fr, OverviewDimensions od)
63 finder = new FeatureColourFinder(fr);
65 pixelsPerCol = od.getPixelsPerCol();
66 pixelsPerSeq = od.getPixelsPerSeq();
67 miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
68 BufferedImage.TYPE_INT_RGB);
72 * Draw alignment rows and columns onto an image
75 * Iterator over rows to be drawn
77 * Iterator over columns to be drawn
78 * @return image containing the drawing
80 public BufferedImage draw(AlignmentRowsCollectionI rows,
81 AlignmentColsCollectionI cols)
83 int rgbcolor = Color.white.getRGB();
87 BufferedImage hiddenImage = buildHiddenImage(rows, cols,
88 miniMe.getWidth(), miniMe.getHeight());
90 for (int alignmentRow : rows)
97 // get details of this alignment row
98 SequenceI seq = rows.getSequence(alignmentRow);
100 // calculate where this row extends to in pixels
101 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
102 miniMe.getHeight() - 1);
106 for (int alignmentCol : cols)
113 // calculate where this column extends to in pixels
114 int endCol = Math.min(
115 Math.round((colIndex + 1) * pixelsPerCol) - 1,
116 miniMe.getWidth() - 1);
118 // don't do expensive colour determination if we're not going to use it
119 // NB this is important to avoid performance issues in the overview
121 if (pixelCol <= endCol)
123 rgbcolor = getColumnColourFromSequence(seq,
127 // fill in the appropriate number of pixels
128 for (int row = pixelRow; row <= endRow; ++row)
130 for (int col = pixelCol; col <= endCol; ++col)
132 miniMe.setRGB(col, row, rgbcolor);
136 pixelCol = endCol + 1;
140 pixelRow = endRow + 1;
144 return applyMask(hiddenImage, miniMe);
148 * Find the colour of a sequence at a specified column position
150 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
151 int lastcol, FeatureColourFinder fcfinder)
153 Color color = Color.white;
155 if ((seq != null) && (seq.getLength() > lastcol))
157 color = sr.getResidueColour(seq, lastcol, fcfinder);
160 return color.getRGB();
164 * Overlay a buffered image (mask) onto another image (image) where mask
165 * has some transparency so image shows through from below
167 private BufferedImage applyMask(BufferedImage mask, BufferedImage image)
169 Graphics2D g = (Graphics2D) image.getGraphics();
171 AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
173 g.drawImage(mask, 0, 0, image.getWidth(), image.getHeight(), null);
178 * Build a masking image of hidden columns and rows to be applied on top
179 * of the main overview image.
181 private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
182 AlignmentColsCollectionI cols, int width, int height)
185 BufferedImage hiddenImage = new BufferedImage(width, height,
186 BufferedImage.TYPE_INT_ARGB);
191 Color hidden = Color.DARK_GRAY.darker();
193 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
195 // set background to transparent
196 g2d.setComposite(AlphaComposite.Clear);
197 g2d.fillRect(0, 0, width, height);
199 // set next colour to opaque
200 g2d.setComposite(AlphaComposite.Src);
202 for (int alignmentCol : cols)
209 // calculate where this column extends to in pixels
210 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
211 hiddenImage.getWidth() - 1);
213 if (pixelCol <= endCol)
215 // determine the colour based on the sequence and column position
216 if (cols.isHidden(alignmentCol))
218 g2d.setColor(hidden);
219 g2d.fillRect(pixelCol, 0, endCol - pixelCol + 1, height);
222 pixelCol = endCol + 1;
230 for (int alignmentRow : rows)
237 // calculate where this row extends to in pixels
238 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
239 miniMe.getHeight() - 1);
241 // get details of this alignment row
242 if (rows.isHidden(alignmentRow))
244 g2d.setColor(hidden);
245 g2d.fillRect(0, pixelRow, width, endRow - pixelRow + 1);
247 pixelRow = endRow + 1;
255 * Draw the alignment annotation in the overview panel
258 * the graphics object to draw on
260 * alignment annotation information
262 * alignment character width value
264 * y-position for the annotation graph
266 * the collection of columns used in the overview panel
268 public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
269 int y, AlignmentColsCollectionI cols)
271 Annotation[] annotations = anno.annotations;
272 g.setColor(Color.white);
273 g.fillRect(0, 0, miniMe.getWidth(), y);
278 for (int alignmentCol : cols)
284 if (alignmentCol >= annotations.length)
286 break; // no more annotations to draw here
290 int endCol = Math.min(
291 Math.round((colIndex + 1) * pixelsPerCol) - 1,
292 miniMe.getWidth() - 1);
294 if (annotations[alignmentCol] != null)
296 if (annotations[alignmentCol].colour == null)
298 g.setColor(Color.black);
302 g.setColor(annotations[alignmentCol].colour);
305 height = (int) ((annotations[alignmentCol].value / anno.graphMax) * y);
311 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
313 pixelCol = endCol + 1;
319 public void setRedraw(boolean b)