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 for (int alignmentRow : rows)
94 // get details of this alignment row
95 SequenceI seq = rows.getSequence(alignmentRow);
97 // calculate where this row extends to in pixels
98 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
99 miniMe.getHeight() - 1);
103 for (int alignmentCol : cols)
110 // calculate where this column extends to in pixels
111 int endCol = Math.min(
112 Math.round((colIndex + 1) * pixelsPerCol) - 1,
113 miniMe.getWidth() - 1);
115 // don't do expensive colour determination if we're not going to use it
116 // NB this is important to avoid performance issues in the overview
118 if (pixelCol <= endCol)
120 rgbcolor = getColumnColourFromSequence(seq,
124 // fill in the appropriate number of pixels
125 for (int row = pixelRow; row <= endRow; ++row)
127 for (int col = pixelCol; col <= endCol; ++col)
129 miniMe.setRGB(col, row, rgbcolor);
133 pixelCol = endCol + 1;
137 pixelRow = endRow + 1;
141 overlayHiddenRegions(rows, cols);
146 * Find the colour of a sequence at a specified column position
149 * sequence to get colour for
151 * column position to get colour for
153 * FeatureColourFinder to use
154 * @return colour of sequence at this position, as RGB
156 private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
157 int lastcol, FeatureColourFinder fcfinder)
159 Color color = Color.white;
161 if ((seq != null) && (seq.getLength() > lastcol))
163 color = sr.getResidueColour(seq, lastcol, fcfinder);
166 return color.getRGB();
170 * Overlay the hidden regions on the overview image
173 * collection of rows the overview is built over
175 * collection of columns the overview is built over
177 private void overlayHiddenRegions(AlignmentRowsCollectionI rows,
178 AlignmentColsCollectionI cols)
180 if (cols.hasHidden() || rows.hasHidden())
182 BufferedImage mask = buildHiddenImage(rows, cols, miniMe.getWidth(),
185 Graphics2D g = (Graphics2D) miniMe.getGraphics();
186 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
188 g.drawImage(mask, 0, 0, miniMe.getWidth(), miniMe.getHeight(), null);
193 * Build a masking image of hidden columns and rows to be applied on top of
194 * the main overview image.
197 * collection of rows the overview is built over
199 * collection of columns the overview is built over
201 * width of overview in pixels
203 * height of overview in pixels
204 * @return BufferedImage containing mask of hidden regions
206 private BufferedImage buildHiddenImage(AlignmentRowsCollectionI rows,
207 AlignmentColsCollectionI cols, int width, int height)
210 BufferedImage hiddenImage = new BufferedImage(width, height,
211 BufferedImage.TYPE_INT_ARGB);
216 Color hidden = Color.DARK_GRAY.darker();
218 Graphics2D g2d = (Graphics2D) hiddenImage.getGraphics();
220 // set background to transparent
221 g2d.setComposite(AlphaComposite.Clear);
222 g2d.fillRect(0, 0, width, height);
224 // set next colour to opaque
225 g2d.setComposite(AlphaComposite.Src);
227 for (int alignmentCol : cols)
234 // calculate where this column extends to in pixels
235 int endCol = Math.min(Math.round((colIndex + 1) * pixelsPerCol) - 1,
236 hiddenImage.getWidth() - 1);
238 if (pixelCol <= endCol)
240 // determine the colour based on the sequence and column position
241 if (cols.isHidden(alignmentCol))
243 g2d.setColor(hidden);
244 g2d.fillRect(pixelCol, 0, endCol - pixelCol + 1, height);
247 pixelCol = endCol + 1;
255 for (int alignmentRow : rows)
262 // calculate where this row extends to in pixels
263 int endRow = Math.min(Math.round((seqIndex + 1) * pixelsPerSeq) - 1,
264 miniMe.getHeight() - 1);
266 // get details of this alignment row
267 if (rows.isHidden(alignmentRow))
269 g2d.setColor(hidden);
270 g2d.fillRect(0, pixelRow, width, endRow - pixelRow + 1);
272 pixelRow = endRow + 1;
280 * Draw the alignment annotation in the overview panel
283 * the graphics object to draw on
285 * alignment annotation information
287 * alignment character width value
289 * y-position for the annotation graph
291 * the collection of columns used in the overview panel
293 public void drawGraph(Graphics g, AlignmentAnnotation anno, int charWidth,
294 int y, AlignmentColsCollectionI cols)
296 Annotation[] annotations = anno.annotations;
297 g.setColor(Color.white);
298 g.fillRect(0, 0, miniMe.getWidth(), y);
303 for (int alignmentCol : cols)
309 if (alignmentCol >= annotations.length)
311 break; // no more annotations to draw here
315 int endCol = Math.min(
316 Math.round((colIndex + 1) * pixelsPerCol) - 1,
317 miniMe.getWidth() - 1);
319 if (annotations[alignmentCol] != null)
321 if (annotations[alignmentCol].colour == null)
323 g.setColor(Color.black);
327 g.setColor(annotations[alignmentCol].colour);
330 height = (int) ((annotations[alignmentCol].value / anno.graphMax) * y);
336 g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
338 pixelCol = endCol + 1;
345 * Allows redraw flag to be set
348 * value to set redraw to: true = redraw is occurring, false = no
351 public void setRedraw(boolean b)