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.
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.ScaleRenderer;
29 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import jalview.util.Comparison;
31 import jalview.viewmodel.ViewportListenerI;
32 import jalview.viewmodel.ViewportRanges;
34 import java.awt.AlphaComposite;
35 import java.awt.BasicStroke;
36 import java.awt.BorderLayout;
37 import java.awt.Color;
38 import java.awt.FontMetrics;
39 import java.awt.Graphics;
40 import java.awt.Graphics2D;
41 import java.awt.RenderingHints;
42 import java.awt.Shape;
43 import java.awt.image.BufferedImage;
44 import java.beans.PropertyChangeEvent;
45 import java.util.List;
47 import javax.swing.JComponent;
50 * The Swing component on which the alignment sequences, and annotations (if
51 * shown), are drawn. This includes scales above, left and right (if shown) in
52 * Wrapped mode, but not the scale above in Unwrapped mode.
55 public class SeqCanvas extends JComponent implements ViewportListenerI
57 private static final String ZEROS = "0000000000";
59 final FeatureRenderer fr;
69 private final SequenceRenderer seqRdr;
71 private boolean fastPaint = false;
73 private boolean fastpainting = false;
75 private AnnotationPanel annotations;
78 * measurements for drawing a wrapped alignment
80 private int labelWidthEast; // label right width in pixels if shown
82 private int labelWidthWest; // label left width in pixels if shown
84 private int wrappedSpaceAboveAlignment; // gap between widths
86 private int wrappedRepeatHeightPx; // height in pixels of wrapped width
88 private int wrappedVisibleWidths; // number of wrapped widths displayed
90 private Graphics2D gg;
93 * Creates a new SeqCanvas object.
97 public SeqCanvas(AlignmentPanel ap)
100 fr = new FeatureRenderer(ap);
101 seqRdr = new SequenceRenderer(av);
102 setLayout(new BorderLayout());
103 PaintRefresher.Register(this, av.getSequenceSetId());
104 setBackground(Color.white);
106 av.getRanges().addPropertyChangeListener(this);
109 public SequenceRenderer getSequenceRenderer()
114 public FeatureRenderer getFeatureRenderer()
120 * Draws the scale above a region of a wrapped alignment, consisting of a
121 * column number every major interval (10 columns).
124 * the graphics context to draw on, positioned at the start (bottom
125 * left) of the line on which to draw any scale marks
127 * start alignment column (0..)
129 * end alignment column (0..)
131 * y offset to draw at
133 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
135 int charHeight = av.getCharHeight();
136 int charWidth = av.getCharWidth();
139 * white fill the scale space (for the fastPaint case)
141 g.setColor(Color.white);
142 g.fillRect(0, ypos - charHeight - charHeight / 2, getWidth(),
143 charHeight * 3 / 2 + 2);
144 g.setColor(Color.black);
146 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
148 for (ScaleMark mark : marks)
150 int mpos = mark.column; // (i - startx - 1)
155 String mstring = mark.text;
161 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
165 * draw a tick mark below the column number, centred on the column;
166 * height of tick mark is 4 pixels less than half a character
168 int xpos = (mpos * charWidth) + (charWidth / 2);
169 g.drawLine(xpos, (ypos + 2) - (charHeight / 2), xpos, ypos - 2);
175 * Draw the scale to the left or right of a wrapped alignment
178 * graphics context, positioned at the start of the scale to be drawn
180 * first column of wrapped width (0.. excluding any hidden columns)
182 * last column of wrapped width (0.. excluding any hidden columns)
184 * vertical offset at which to begin the scale
186 * if true, scale is left of residues, if false, scale is right
188 void drawVerticalScale(Graphics g, final int startx, final int endx,
189 final int ypos, final boolean left)
191 int charHeight = av.getCharHeight();
192 int charWidth = av.getCharWidth();
194 int yPos = ypos + charHeight;
198 if (av.hasHiddenColumns())
200 HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
201 startX = hiddenColumns.adjustForHiddenColumns(startx);
202 endX = hiddenColumns.adjustForHiddenColumns(endx);
204 FontMetrics fm = getFontMetrics(av.getFont());
206 for (int i = 0; i < av.getAlignment().getHeight(); i++)
208 SequenceI seq = av.getAlignment().getSequenceAt(i);
211 * find sequence position of first non-gapped position -
212 * to the right if scale left, to the left if scale right
214 int index = left ? startX : endX;
216 while (index >= startX && index <= endX)
218 if (!Comparison.isGap(seq.getCharAt(index)))
220 value = seq.findPosition(index);
234 * white fill the space for the scale
236 g.setColor(Color.white);
237 int y = (yPos + (i * charHeight)) - (charHeight / 5);
238 // fillRect origin is top left of rectangle
239 g.fillRect(0, y - charHeight, left ? labelWidthWest : labelWidthEast,
245 * draw scale value, right justified within its width less half a
246 * character width padding on the right
248 int labelSpace = left ? labelWidthWest : labelWidthEast;
249 labelSpace -= charWidth / 2; // leave space to the right
250 String valueAsString = String.valueOf(value);
251 int labelLength = fm.stringWidth(valueAsString);
252 int xOffset = labelSpace - labelLength;
253 g.setColor(Color.black);
254 g.drawString(valueAsString, xOffset, y);
260 * Does a fast paint of an alignment in response to a scroll. Most of the
261 * visible region is simply copied and shifted, and then any newly visible
262 * columns or rows are drawn. The scroll may be horizontal or vertical, but
263 * not both at once. Scrolling may be the result of
265 * <li>dragging a scroll bar</li>
266 * <li>clicking in the scroll bar</li>
267 * <li>scrolling by trackpad, middle mouse button, or other device</li>
268 * <li>by moving the box in the Overview window</li>
269 * <li>programmatically to make a highlighted position visible</li>
273 * columns to shift right (positive) or left (negative)
275 * rows to shift down (positive) or up (negative)
277 public void fastPaint(int horizontal, int vertical)
279 if (fastpainting || gg == null || img == null)
288 int charHeight = av.getCharHeight();
289 int charWidth = av.getCharWidth();
291 ViewportRanges ranges = av.getRanges();
292 int startRes = ranges.getStartRes();
293 int endRes = ranges.getEndRes();
294 int startSeq = ranges.getStartSeq();
295 int endSeq = ranges.getEndSeq();
299 gg.copyArea(horizontal * charWidth, vertical * charHeight,
300 img.getWidth(), img.getHeight(), -horizontal * charWidth,
301 -vertical * charHeight);
303 if (horizontal > 0) // scrollbar pulled right, image to the left
305 transX = (endRes - startRes - horizontal) * charWidth;
306 startRes = endRes - horizontal;
308 else if (horizontal < 0)
310 endRes = startRes - horizontal;
312 else if (vertical > 0) // scroll down
314 startSeq = endSeq - vertical;
316 if (startSeq < ranges.getStartSeq())
317 { // ie scrolling too fast, more than a page at a time
318 startSeq = ranges.getStartSeq();
322 transY = img.getHeight() - ((vertical + 1) * charHeight);
325 else if (vertical < 0)
327 endSeq = startSeq - vertical;
329 if (endSeq > ranges.getEndSeq())
331 endSeq = ranges.getEndSeq();
335 gg.translate(transX, transY);
336 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
337 gg.translate(-transX, -transY);
342 fastpainting = false;
347 public void paintComponent(Graphics g)
349 super.paintComponent(g);
351 int charHeight = av.getCharHeight();
352 int charWidth = av.getCharWidth();
354 ViewportRanges ranges = av.getRanges();
356 int width = getWidth();
357 int height = getHeight();
359 width -= (width % charWidth);
360 height -= (height % charHeight);
362 // selectImage is the selection group outline image
363 BufferedImage selectImage = drawSelectionGroup(
364 ranges.getStartRes(), ranges.getEndRes(),
365 ranges.getStartSeq(), ranges.getEndSeq());
367 if ((img != null) && (fastPaint
368 || (getVisibleRect().width != g.getClipBounds().width)
369 || (getVisibleRect().height != g.getClipBounds().height)))
371 BufferedImage lcimg = buildLocalImage(selectImage);
372 g.drawImage(lcimg, 0, 0, this);
375 else if ((width > 0) && (height > 0))
377 // img is a cached version of the last view we drew, if any
378 // if we have no img or the size has changed, make a new one
379 if (img == null || width != img.getWidth()
380 || height != img.getHeight())
387 gg = (Graphics2D) img.getGraphics();
388 gg.setFont(av.getFont());
393 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
394 RenderingHints.VALUE_ANTIALIAS_ON);
397 gg.setColor(Color.white);
398 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
400 if (av.getWrapAlignment())
402 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
406 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
407 ranges.getStartSeq(), ranges.getEndSeq(), 0);
410 // lcimg is a local *copy* of img which we'll draw selectImage on top of
411 BufferedImage lcimg = buildLocalImage(selectImage);
412 g.drawImage(lcimg, 0, 0, this);
417 * Draw an alignment panel for printing
420 * Graphics object to draw with
422 * start residue of print area
424 * end residue of print area
426 * start sequence of print area
428 * end sequence of print area
430 public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
431 int startSeq, int endSeq)
433 drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
435 BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
437 if (selectImage != null)
439 ((Graphics2D) g1).setComposite(AlphaComposite
440 .getInstance(AlphaComposite.SRC_OVER));
441 g1.drawImage(selectImage, 0, 0, this);
446 * Draw a wrapped alignment panel for printing
449 * Graphics object to draw with
451 * width of drawing area
452 * @param canvasHeight
453 * height of drawing area
455 * start residue of print area
457 public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
458 int canvasHeight, int startRes)
460 SequenceGroup group = av.getSelectionGroup();
462 drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
466 BufferedImage selectImage = null;
469 selectImage = new BufferedImage(canvasWidth, canvasHeight,
470 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
471 } catch (OutOfMemoryError er)
474 System.err.println("Print image OutOfMemory Error.\n" + er);
475 new OOMWarning("Creating wrapped alignment image for printing", er);
477 if (selectImage != null)
479 Graphics2D g2 = selectImage.createGraphics();
480 setupSelectionGroup(g2, selectImage);
481 drawWrappedSelection(g2, group, canvasWidth, canvasHeight,
485 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
486 g.drawImage(selectImage, 0, 0, this);
493 * Make a local image by combining the cached image img
496 private BufferedImage buildLocalImage(BufferedImage selectImage)
498 // clone the cached image
499 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
501 Graphics2D g2d = lcimg.createGraphics();
502 g2d.drawImage(img, 0, 0, null);
504 // overlay selection group on lcimg
505 if (selectImage != null)
508 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
509 g2d.drawImage(selectImage, 0, 0, this);
517 * Set up a buffered image of the correct height and size for the sequence canvas
519 private BufferedImage setupImage()
521 BufferedImage lcimg = null;
523 int charWidth = av.getCharWidth();
524 int charHeight = av.getCharHeight();
526 int width = getWidth();
527 int height = getHeight();
529 width -= (width % charWidth);
530 height -= (height % charHeight);
532 if ((width < 1) || (height < 1))
539 lcimg = new BufferedImage(width, height,
540 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
541 } catch (OutOfMemoryError er)
545 "Group image OutOfMemory Redraw Error.\n" + er);
546 new OOMWarning("Creating alignment image for display", er);
555 * Returns the visible width of the canvas in residues, after allowing for
556 * East or West scales (if shown)
559 * the width in pixels (possibly including scales)
563 public int getWrappedCanvasWidth(int canvasWidth)
565 int charWidth = av.getCharWidth();
567 FontMetrics fm = getFontMetrics(av.getFont());
571 if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
573 labelWidth = getLabelWidth(fm);
576 labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
578 labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
580 return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
584 * Returns a pixel width sufficient to show the largest sequence coordinate
585 * (end position) in the alignment, calculated as the FontMetrics width of
586 * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
587 * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
588 * half a character width space on either side.
593 protected int getLabelWidth(FontMetrics fm)
596 * find the biggest sequence end position we need to show
597 * (note this is not necessarily the sequence length)
600 AlignmentI alignment = av.getAlignment();
601 for (int i = 0; i < alignment.getHeight(); i++)
603 maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
607 for (int i = maxWidth; i > 0; i /= 10)
612 return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
616 * Draws as many widths of a wrapped alignment as can fit in the visible
621 * available width in pixels
622 * @param canvasHeight
623 * available height in pixels
625 * the first column (0...) of the alignment to draw
627 public void drawWrappedPanel(Graphics g, int canvasWidth,
628 int canvasHeight, final int startColumn)
630 int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
633 av.setWrappedWidth(wrappedWidthInResidues);
635 ViewportRanges ranges = av.getRanges();
636 ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
639 * draw one width at a time (including any scales or annotation shown),
640 * until we have run out of either alignment or vertical space available
642 int ypos = wrappedSpaceAboveAlignment;
643 int maxWidth = ranges.getVisibleAlignmentWidth();
645 int start = startColumn;
646 int currentWidth = 0;
647 while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
650 .min(maxWidth, start + wrappedWidthInResidues - 1);
651 drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
652 ypos += wrappedRepeatHeightPx;
653 start += wrappedWidthInResidues;
657 drawWrappedDecorators(g, startColumn);
661 * Calculates and saves values needed when rendering a wrapped alignment.
662 * These depend on many factors, including
664 * <li>canvas width and height</li>
665 * <li>number of visible sequences, and height of annotations if shown</li>
666 * <li>font and character width</li>
667 * <li>whether scales are shown left, right or above the alignment</li>
671 * @param canvasHeight
672 * @return the number of residue columns in each width
674 protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
676 int charHeight = av.getCharHeight();
679 * vertical space in pixels between wrapped widths of alignment
680 * - one character height, or two if scale above is drawn
682 wrappedSpaceAboveAlignment = charHeight
683 * (av.getScaleAboveWrapped() ? 2 : 1);
686 * height in pixels of the wrapped widths
688 wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
690 wrappedRepeatHeightPx += av.getRanges().getViewportHeight()
692 // add annotations panel height if shown
693 wrappedRepeatHeightPx += getAnnotationHeight();
696 * number of visible widths (the last one may be part height),
697 * ensuring a part height includes at least one sequence
699 ViewportRanges ranges = av.getRanges();
700 int xMax = ranges.getVisibleAlignmentWidth();
701 wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
702 int remainder = canvasHeight % wrappedRepeatHeightPx;
703 if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
705 wrappedVisibleWidths++;
709 * compute width in residues; this also sets East and West label widths
711 int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
714 * limit visibleWidths to not exceed width of alignment
716 int maxWidths = (xMax - ranges.getStartRes()) / wrappedWidthInResidues;
717 if (xMax % wrappedWidthInResidues > 0)
721 wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
723 return wrappedWidthInResidues;
727 * Draws one width of a wrapped alignment, including sequences and
728 * annnotations, if shown, but not scales or hidden column markers
734 * @param canvasHeight
736 protected void drawWrappedWidth(Graphics g, int ypos, int startColumn,
737 int endColumn, int canvasHeight)
739 ViewportRanges ranges = av.getRanges();
740 int viewportWidth = ranges.getViewportWidth();
742 int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
745 * move right before drawing by the width of the scale left (if any)
746 * plus column offset from left margin (usually zero, but may be non-zero
747 * when fast painting is drawing just a few columns)
749 int charWidth = av.getCharWidth();
750 int xOffset = labelWidthWest
751 + ((startColumn - ranges.getStartRes()) % viewportWidth)
753 g.translate(xOffset, 0);
755 // When printing we have an extra clipped region,
756 // the Printable page which we need to account for here
757 Shape clip = g.getClip();
761 g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
765 g.setClip(0, (int) clip.getBounds().getY(),
766 viewportWidth * charWidth, (int) clip.getBounds().getHeight());
770 * white fill the region to be drawn (so incremental fast paint doesn't
771 * scribble over an existing image)
773 gg.setColor(Color.white);
774 gg.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
775 wrappedRepeatHeightPx);
777 drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
780 int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
782 if (av.isShowAnnotation())
784 g.translate(0, cHeight + ypos + 3);
785 if (annotations == null)
787 annotations = new AnnotationPanel(av);
790 annotations.renderer.drawComponent(annotations, av, g, -1,
791 startColumn, endx + 1);
792 g.translate(0, -cHeight - ypos - 3);
795 g.translate(-xOffset, 0);
799 * Draws scales left, right and above (if shown), and any hidden column
800 * markers, on all widths of the wrapped alignment
805 protected void drawWrappedDecorators(Graphics g, final int startColumn)
807 int charWidth = av.getCharWidth();
809 g.setFont(av.getFont());
810 g.setColor(Color.black);
812 int ypos = wrappedSpaceAboveAlignment;
813 ViewportRanges ranges = av.getRanges();
814 int viewportWidth = ranges.getViewportWidth();
815 int maxWidth = ranges.getVisibleAlignmentWidth();
817 int startCol = startColumn;
819 while (widthsDrawn < wrappedVisibleWidths)
821 int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
823 if (av.getScaleLeftWrapped())
825 drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
828 if (av.getScaleRightWrapped())
830 int x = labelWidthWest + viewportWidth * charWidth;
832 drawVerticalScale(g, startCol, endColumn, ypos, false);
837 * white fill region of scale above and hidden column markers
838 * (to support incremental fast paint of image)
840 g.setColor(Color.white);
841 g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
842 * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
843 g.setColor(Color.black);
845 g.translate(labelWidthWest, 0);
847 if (av.getScaleAboveWrapped())
849 drawNorthScale(g, startCol, endColumn, ypos);
852 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
854 drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
857 g.translate(-labelWidthWest, 0);
859 ypos += wrappedRepeatHeightPx;
860 startCol += viewportWidth;
871 protected void drawHiddenColumnMarkers(Graphics g, int ypos,
872 int startColumn, int endColumn)
874 int charHeight = av.getCharHeight();
875 int charWidth = av.getCharWidth();
877 g.setColor(Color.blue);
878 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
879 List<Integer> positions = hidden.findHiddenRegionPositions();
880 for (int pos : positions)
882 int res = pos - startColumn;
884 if (res < 0 || res > endColumn - startColumn)
890 * draw a downward-pointing triangle at the hidden columns location
891 * (before the following visible column)
893 int xMiddle = res * charWidth;
894 int[] xPoints = new int[] { xMiddle - charHeight / 4,
895 xMiddle + charHeight / 4, xMiddle };
896 int yTop = ypos - (charHeight / 2);
897 int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
898 g.fillPolygon(xPoints, yPoints, 3);
903 * Draw a selection group over a wrapped alignment
905 private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
907 int canvasHeight, int startRes)
909 int charHeight = av.getCharHeight();
910 int charWidth = av.getCharWidth();
912 // height gap above each panel
913 int hgap = charHeight;
914 if (av.getScaleAboveWrapped())
919 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
921 int cHeight = av.getAlignment().getHeight() * charHeight;
923 int startx = startRes;
925 int ypos = hgap; // vertical offset
926 int maxwidth = av.getAlignment().getWidth();
928 if (av.hasHiddenColumns())
930 maxwidth = av.getAlignment().getHiddenColumns()
931 .findColumnPosition(maxwidth);
934 // chop the wrapped alignment extent up into panel-sized blocks and treat
935 // each block as if it were a block from an unwrapped alignment
936 while ((ypos <= canvasHeight) && (startx < maxwidth))
938 // set end value to be start + width, or maxwidth, whichever is smaller
939 endx = startx + cWidth - 1;
946 g.translate(labelWidthWest, 0);
948 drawUnwrappedSelection(g, group, startx, endx, 0,
949 av.getAlignment().getHeight() - 1,
952 g.translate(-labelWidthWest, 0);
954 // update vertical offset
955 ypos += cHeight + getAnnotationHeight() + hgap;
957 // update horizontal offset
962 int getAnnotationHeight()
964 if (!av.isShowAnnotation())
969 if (annotations == null)
971 annotations = new AnnotationPanel(av);
974 return annotations.adjustPanelHeight();
978 * Draws the visible region of the alignment on the graphics context. If there
979 * are hidden column markers in the visible region, then each sub-region
980 * between the markers is drawn separately, followed by the hidden column
984 * the graphics context, positioned at the first residue to be drawn
986 * offset of the first column to draw (0..)
988 * offset of the last column to draw (0..)
990 * offset of the first sequence to draw (0..)
992 * offset of the last sequence to draw (0..)
994 * vertical offset at which to draw (for wrapped alignments)
996 public void drawPanel(Graphics g1, final int startRes, final int endRes,
997 final int startSeq, final int endSeq, final int yOffset)
999 int charHeight = av.getCharHeight();
1000 int charWidth = av.getCharWidth();
1002 if (!av.hasHiddenColumns())
1004 draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
1009 final int screenYMax = endRes - startRes;
1010 int blockStart = startRes;
1011 int blockEnd = endRes;
1013 for (int[] region : av.getAlignment().getHiddenColumns()
1014 .getHiddenColumnsCopy())
1016 int hideStart = region[0];
1017 int hideEnd = region[1];
1019 if (hideStart <= blockStart)
1021 blockStart += (hideEnd - hideStart) + 1;
1026 * draw up to just before the next hidden region, or the end of
1027 * the visible region, whichever comes first
1029 blockEnd = Math.min(hideStart - 1, blockStart + screenYMax
1032 g1.translate(screenY * charWidth, 0);
1034 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1037 * draw the downline of the hidden column marker (ScalePanel draws the
1038 * triangle on top) if we reached it
1040 if (av.getShowHiddenMarkers() && blockEnd == hideStart - 1)
1042 g1.setColor(Color.blue);
1044 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1045 0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1046 (endSeq - startSeq + 1) * charHeight + yOffset);
1049 g1.translate(-screenY * charWidth, 0);
1050 screenY += blockEnd - blockStart + 1;
1051 blockStart = hideEnd + 1;
1053 if (screenY > screenYMax)
1055 // already rendered last block
1060 if (screenY <= screenYMax)
1062 // remaining visible region to render
1063 blockEnd = blockStart + screenYMax - screenY;
1064 g1.translate(screenY * charWidth, 0);
1065 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1067 g1.translate(-screenY * charWidth, 0);
1074 * Draws a region of the visible alignment
1078 * offset of the first column in the visible region (0..)
1080 * offset of the last column in the visible region (0..)
1082 * offset of the first sequence in the visible region (0..)
1084 * offset of the last sequence in the visible region (0..)
1086 * vertical offset at which to draw (for wrapped alignments)
1088 private void draw(Graphics g, int startRes, int endRes, int startSeq,
1089 int endSeq, int offset)
1091 int charHeight = av.getCharHeight();
1092 int charWidth = av.getCharWidth();
1094 g.setFont(av.getFont());
1095 seqRdr.prepare(g, av.isRenderGaps());
1099 // / First draw the sequences
1100 // ///////////////////////////
1101 for (int i = startSeq; i <= endSeq; i++)
1103 nextSeq = av.getAlignment().getSequenceAt(i);
1104 if (nextSeq == null)
1106 // occasionally, a race condition occurs such that the alignment row is
1110 seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1111 startRes, endRes, offset + ((i - startSeq) * charHeight));
1113 if (av.isShowSequenceFeatures())
1115 fr.drawSequence(g, nextSeq, startRes, endRes,
1116 offset + ((i - startSeq) * charHeight), false);
1120 * highlight search Results once sequence has been drawn
1122 if (av.hasSearchResults())
1124 SearchResultsI searchResults = av.getSearchResults();
1125 int[] visibleResults = searchResults.getResults(nextSeq,
1127 if (visibleResults != null)
1129 for (int r = 0; r < visibleResults.length; r += 2)
1131 seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1132 visibleResults[r + 1], (visibleResults[r] - startRes)
1134 + ((i - startSeq) * charHeight));
1139 if (av.cursorMode && cursorY == i && cursorX >= startRes
1140 && cursorX <= endRes)
1142 seqRdr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
1143 offset + ((i - startSeq) * charHeight));
1147 if (av.getSelectionGroup() != null
1148 || av.getAlignment().getGroups().size() > 0)
1150 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1155 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1156 int startSeq, int endSeq, int offset)
1158 Graphics2D g = (Graphics2D) g1;
1160 // ///////////////////////////////////
1161 // Now outline any areas if necessary
1162 // ///////////////////////////////////
1164 SequenceGroup group = null;
1165 int groupIndex = -1;
1167 if (av.getAlignment().getGroups().size() > 0)
1169 group = av.getAlignment().getGroups().get(0);
1175 g.setStroke(new BasicStroke());
1176 g.setColor(group.getOutlineColour());
1180 drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1185 g.setStroke(new BasicStroke());
1187 if (groupIndex >= av.getAlignment().getGroups().size())
1192 group = av.getAlignment().getGroups().get(groupIndex);
1194 } while (groupIndex < av.getAlignment().getGroups().size());
1202 * Draw the selection group as a separate image and overlay
1204 private BufferedImage drawSelectionGroup(int startRes, int endRes,
1205 int startSeq, int endSeq)
1207 // get a new image of the correct size
1208 BufferedImage selectionImage = setupImage();
1210 if (selectionImage == null)
1215 SequenceGroup group = av.getSelectionGroup();
1222 // set up drawing colour
1223 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1225 setupSelectionGroup(g, selectionImage);
1227 if (!av.getWrapAlignment())
1229 drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1234 drawWrappedSelection(g, group, getWidth(), getHeight(),
1235 av.getRanges().getStartRes());
1239 return selectionImage;
1243 * Set up graphics for selection group
1245 private void setupSelectionGroup(Graphics2D g,
1246 BufferedImage selectionImage)
1248 // set background to transparent
1249 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1250 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1252 // set up foreground to draw red dashed line
1253 g.setComposite(AlphaComposite.Src);
1254 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1255 BasicStroke.JOIN_ROUND, 3f, new float[]
1257 g.setColor(Color.RED);
1261 * Draw a selection group over an unwrapped alignment
1262 * @param g graphics object to draw with
1263 * @param group selection group
1264 * @param startRes start residue of area to draw
1265 * @param endRes end residue of area to draw
1266 * @param startSeq start sequence of area to draw
1267 * @param endSeq end sequence of area to draw
1268 * @param offset vertical offset (used when called from wrapped alignment code)
1270 private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1271 int startRes, int endRes, int startSeq, int endSeq, int offset)
1273 int charWidth = av.getCharWidth();
1275 if (!av.hasHiddenColumns())
1277 drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1282 // package into blocks of visible columns
1284 int blockStart = startRes;
1285 int blockEnd = endRes;
1287 for (int[] region : av.getAlignment().getHiddenColumns()
1288 .getHiddenColumnsCopy())
1290 int hideStart = region[0];
1291 int hideEnd = region[1];
1293 if (hideStart <= blockStart)
1295 blockStart += (hideEnd - hideStart) + 1;
1299 blockEnd = hideStart - 1;
1301 g.translate(screenY * charWidth, 0);
1302 drawPartialGroupOutline(g, group,
1303 blockStart, blockEnd, startSeq, endSeq, offset);
1305 g.translate(-screenY * charWidth, 0);
1306 screenY += blockEnd - blockStart + 1;
1307 blockStart = hideEnd + 1;
1309 if (screenY > (endRes - startRes))
1311 // already rendered last block
1316 if (screenY <= (endRes - startRes))
1318 // remaining visible region to render
1319 blockEnd = blockStart + (endRes - startRes) - screenY;
1320 g.translate(screenY * charWidth, 0);
1321 drawPartialGroupOutline(g, group,
1322 blockStart, blockEnd, startSeq, endSeq, offset);
1324 g.translate(-screenY * charWidth, 0);
1330 * Draw the selection group as a separate image and overlay
1332 private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1333 int startRes, int endRes, int startSeq, int endSeq,
1336 int charHeight = av.getCharHeight();
1337 int charWidth = av.getCharWidth();
1339 int visWidth = (endRes - startRes + 1) * charWidth;
1343 boolean inGroup = false;
1351 for (i = startSeq; i <= endSeq; i++)
1353 // position of start residue of group relative to startRes, in pixels
1354 sx = (group.getStartRes() - startRes) * charWidth;
1356 // width of group in pixels
1357 xwidth = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
1360 sy = verticalOffset + (i - startSeq) * charHeight;
1362 if (sx + xwidth < 0 || sx > visWidth)
1367 if ((sx <= (endRes - startRes) * charWidth)
1368 && group.getSequences(null)
1369 .contains(av.getAlignment().getSequenceAt(i)))
1371 if ((bottom == -1) && !group.getSequences(null)
1372 .contains(av.getAlignment().getSequenceAt(i + 1)))
1374 bottom = sy + charHeight;
1379 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1380 .contains(av.getAlignment().getSequenceAt(i - 1)))
1393 // if start position is visible, draw vertical line to left of
1395 if (sx >= 0 && sx < visWidth)
1397 g.drawLine(sx, oldY, sx, sy);
1400 // if end position is visible, draw vertical line to right of
1402 if (sx + xwidth < visWidth)
1404 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1413 // don't let width extend beyond current block, or group extent
1415 if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1417 xwidth = (endRes - startRes + 1) * charWidth - sx;
1420 // draw horizontal line at top of group
1423 g.drawLine(sx, top, sx + xwidth, top);
1427 // draw horizontal line at bottom of group
1430 g.drawLine(sx, bottom, sx + xwidth, bottom);
1441 sy = verticalOffset + ((i - startSeq) * charHeight);
1442 if (sx >= 0 && sx < visWidth)
1444 g.drawLine(sx, oldY, sx, sy);
1447 if (sx + xwidth < visWidth)
1449 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1458 if (sx + xwidth > visWidth)
1462 else if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1464 xwidth = (endRes - startRes + 1) * charWidth;
1469 g.drawLine(sx, top, sx + xwidth, top);
1475 g.drawLine(sx, bottom - 1, sx + xwidth, bottom - 1);
1484 * Highlights search results in the visible region by rendering as white text
1485 * on a black background. Any previous highlighting is removed. Answers true
1486 * if any highlight was left on the visible alignment (so status bar should be
1487 * set to match), else false.
1489 * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
1490 * alignment had to be scrolled to show the highlighted region, then it should
1491 * be fully redrawn, otherwise a fast paint can be performed. This argument
1492 * could be removed if fast paint of scrolled wrapped alignment is coded in
1493 * future (JAL-2609).
1496 * @param noFastPaint
1499 public boolean highlightSearchResults(SearchResultsI results,
1500 boolean noFastPaint)
1506 boolean wrapped = av.getWrapAlignment();
1509 fastPaint = !noFastPaint;
1510 fastpainting = fastPaint;
1513 * to avoid redrawing the whole visible region, we instead
1514 * redraw just the minimal regions to remove previous highlights
1517 SearchResultsI previous = av.getSearchResults();
1518 av.setSearchResults(results);
1519 boolean redrawn = false;
1520 boolean drawn = false;
1523 redrawn = drawMappedPositionsWrapped(previous);
1524 drawn = drawMappedPositionsWrapped(results);
1529 redrawn = drawMappedPositions(previous);
1530 drawn = drawMappedPositions(results);
1535 * if highlights were either removed or added, repaint
1543 * return true only if highlights were added
1549 fastpainting = false;
1554 * Redraws the minimal rectangle in the visible region (if any) that includes
1555 * mapped positions of the given search results. Whether or not positions are
1556 * highlighted depends on the SearchResults set on the Viewport. This allows
1557 * this method to be called to either clear or set highlighting. Answers true
1558 * if any positions were drawn (in which case a repaint is still required),
1564 protected boolean drawMappedPositions(SearchResultsI results)
1566 if (results == null)
1572 * calculate the minimal rectangle to redraw that
1573 * includes both new and existing search results
1575 int firstSeq = Integer.MAX_VALUE;
1577 int firstCol = Integer.MAX_VALUE;
1579 boolean matchFound = false;
1581 ViewportRanges ranges = av.getRanges();
1582 int firstVisibleColumn = ranges.getStartRes();
1583 int lastVisibleColumn = ranges.getEndRes();
1584 AlignmentI alignment = av.getAlignment();
1585 if (av.hasHiddenColumns())
1587 firstVisibleColumn = alignment.getHiddenColumns()
1588 .adjustForHiddenColumns(firstVisibleColumn);
1589 lastVisibleColumn = alignment.getHiddenColumns()
1590 .adjustForHiddenColumns(lastVisibleColumn);
1593 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1594 .getEndSeq(); seqNo++)
1596 SequenceI seq = alignment.getSequenceAt(seqNo);
1598 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1600 if (visibleResults != null)
1602 for (int i = 0; i < visibleResults.length - 1; i += 2)
1604 int firstMatchedColumn = visibleResults[i];
1605 int lastMatchedColumn = visibleResults[i + 1];
1606 if (firstMatchedColumn <= lastVisibleColumn
1607 && lastMatchedColumn >= firstVisibleColumn)
1610 * found a search results match in the visible region -
1611 * remember the first and last sequence matched, and the first
1612 * and last visible columns in the matched positions
1615 firstSeq = Math.min(firstSeq, seqNo);
1616 lastSeq = Math.max(lastSeq, seqNo);
1617 firstMatchedColumn = Math.max(firstMatchedColumn,
1618 firstVisibleColumn);
1619 lastMatchedColumn = Math.min(lastMatchedColumn,
1621 firstCol = Math.min(firstCol, firstMatchedColumn);
1622 lastCol = Math.max(lastCol, lastMatchedColumn);
1630 if (av.hasHiddenColumns())
1632 firstCol = alignment.getHiddenColumns()
1633 .findColumnPosition(firstCol);
1634 lastCol = alignment.getHiddenColumns().findColumnPosition(lastCol);
1636 int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1637 int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1638 gg.translate(transX, transY);
1639 drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1640 gg.translate(-transX, -transY);
1647 public void propertyChange(PropertyChangeEvent evt)
1649 String eventName = evt.getPropertyName();
1651 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1656 else if (eventName.equals(ViewportRanges.STARTRES))
1659 if (eventName.equals(ViewportRanges.STARTRES))
1661 // Make sure we're not trying to draw a panel
1662 // larger than the visible window
1663 ViewportRanges vpRanges = av.getRanges();
1664 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1665 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1666 if (scrollX > range)
1670 else if (scrollX < -range)
1675 // Both scrolling and resizing change viewport ranges: scrolling changes
1676 // both start and end points, but resize only changes end values.
1677 // Here we only want to fastpaint on a scroll, with resize using a normal
1678 // paint, so scroll events are identified as changes to the horizontal or
1679 // vertical start value.
1681 // scroll - startres and endres both change
1682 if (av.getWrapAlignment())
1684 fastPaintWrapped(scrollX);
1688 fastPaint(scrollX, 0);
1691 else if (eventName.equals(ViewportRanges.STARTSEQ))
1694 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1700 * Does a minimal update of the image for a scroll movement. This method
1701 * handles scroll movements of up to one width of the wrapped alignment (one
1702 * click in the vertical scrollbar). Larger movements (for example after a
1703 * scroll to highlight a mapped position) trigger a full redraw instead.
1706 * number of positions scrolled (right if positive, left if negative)
1708 protected void fastPaintWrapped(int scrollX)
1710 ViewportRanges ranges = av.getRanges();
1712 if (Math.abs(scrollX) > ranges.getViewportWidth())
1715 * shift of more than one view width is
1716 * overcomplicated to handle in this method
1723 if (fastpainting || gg == null)
1729 fastpainting = true;
1733 calculateWrappedGeometry(getWidth(), getHeight());
1736 * relocate the regions of the alignment that are still visible
1738 shiftWrappedAlignment(-scrollX);
1741 * add new columns (sequence, annotation)
1742 * - at top left if scrollX < 0
1743 * - at right of last two widths if scrollX > 0
1747 int startRes = ranges.getStartRes();
1748 drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1749 - scrollX - 1, getHeight());
1753 fastPaintWrappedAddRight(scrollX);
1757 * draw all scales (if shown) and hidden column markers
1759 drawWrappedDecorators(gg, ranges.getStartRes());
1764 fastpainting = false;
1769 * Draws the specified number of columns at the 'end' (bottom right) of a
1770 * wrapped alignment view, including sequences and annotations if shown, but
1771 * not scales. Also draws the same number of columns at the right hand end of
1772 * the second last width shown, if the last width is not full height (so
1773 * cannot simply be copied from the graphics image).
1777 protected void fastPaintWrappedAddRight(int columns)
1784 ViewportRanges ranges = av.getRanges();
1785 int viewportWidth = ranges.getViewportWidth();
1786 int charWidth = av.getCharWidth();
1789 * draw full height alignment in the second last row, last columns, if the
1790 * last row was not full height
1792 int visibleWidths = wrappedVisibleWidths;
1793 int canvasHeight = getHeight();
1794 boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1796 if (lastWidthPartHeight)
1798 int widthsAbove = Math.max(0, visibleWidths - 2);
1799 int ypos = wrappedRepeatHeightPx * widthsAbove
1800 + wrappedSpaceAboveAlignment;
1801 int endRes = ranges.getEndRes();
1802 endRes += widthsAbove * viewportWidth;
1803 int startRes = endRes - columns;
1804 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1808 * white fill first to erase annotations
1810 gg.translate(xOffset, 0);
1811 gg.setColor(Color.white);
1812 gg.fillRect(labelWidthWest, ypos,
1813 (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1814 gg.translate(-xOffset, 0);
1816 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1820 * draw newly visible columns in last wrapped width (none if we
1821 * have reached the end of the alignment)
1822 * y-offset for drawing last width is height of widths above,
1825 int widthsAbove = visibleWidths - 1;
1826 int ypos = wrappedRepeatHeightPx * widthsAbove
1827 + wrappedSpaceAboveAlignment;
1828 int endRes = ranges.getEndRes();
1829 endRes += widthsAbove * viewportWidth;
1830 int startRes = endRes - columns + 1;
1833 * white fill first to erase annotations
1835 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1837 gg.translate(xOffset, 0);
1838 gg.setColor(Color.white);
1839 int width = viewportWidth * charWidth - xOffset;
1840 gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1841 gg.translate(-xOffset, 0);
1843 gg.setFont(av.getFont());
1844 gg.setColor(Color.black);
1846 if (startRes < ranges.getVisibleAlignmentWidth())
1848 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1852 * and finally, white fill any space below the visible alignment
1854 int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1855 if (heightBelow > 0)
1857 gg.setColor(Color.white);
1858 gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1863 * Shifts the visible alignment by the specified number of columns - left if
1864 * negative, right if positive. Copies and moves sequences and annotations (if
1865 * shown). Scales, hidden column markers and any newly visible columns must be
1870 protected void shiftWrappedAlignment(int positions)
1876 int charWidth = av.getCharWidth();
1878 int canvasHeight = getHeight();
1879 ViewportRanges ranges = av.getRanges();
1880 int viewportWidth = ranges.getViewportWidth();
1881 int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
1883 int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
1884 int xMax = ranges.getVisibleAlignmentWidth();
1889 * shift right (after scroll left)
1890 * for each wrapped width (starting with the last), copy (width-positions)
1891 * columns from the left margin to the right margin, and copy positions
1892 * columns from the right margin of the row above (if any) to the
1893 * left margin of the current row
1897 * get y-offset of last wrapped width, first row of sequences
1899 int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
1900 y += wrappedSpaceAboveAlignment;
1901 int copyFromLeftStart = labelWidthWest;
1902 int copyFromRightStart = copyFromLeftStart + widthToCopy;
1906 gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
1907 positions * charWidth, 0);
1910 gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
1911 positions * charWidth, heightToCopy, -widthToCopy,
1912 wrappedRepeatHeightPx);
1915 y -= wrappedRepeatHeightPx;
1921 * shift left (after scroll right)
1922 * for each wrapped width (starting with the first), copy (width-positions)
1923 * columns from the right margin to the left margin, and copy positions
1924 * columns from the left margin of the row below (if any) to the
1925 * right margin of the current row
1927 int xpos = av.getRanges().getStartRes();
1928 int y = wrappedSpaceAboveAlignment;
1929 int copyFromRightStart = labelWidthWest - positions * charWidth;
1931 while (y < canvasHeight)
1933 gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
1934 positions * charWidth, 0);
1935 if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
1936 && (xpos + viewportWidth <= xMax))
1938 gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
1939 * charWidth, heightToCopy, widthToCopy,
1940 -wrappedRepeatHeightPx);
1943 y += wrappedRepeatHeightPx;
1944 xpos += viewportWidth;
1951 * Redraws any positions in the search results in the visible region of a
1952 * wrapped alignment. Any highlights are drawn depending on the search results
1953 * set on the Viewport, not the <code>results</code> argument. This allows
1954 * this method to be called either to clear highlights (passing the previous
1955 * search results), or to draw new highlights.
1960 protected boolean drawMappedPositionsWrapped(SearchResultsI results)
1962 if (results == null)
1966 int charHeight = av.getCharHeight();
1968 boolean matchFound = false;
1970 calculateWrappedGeometry(getWidth(), getHeight());
1971 int wrappedWidth = av.getWrappedWidth();
1972 int wrappedHeight = wrappedRepeatHeightPx;
1974 ViewportRanges ranges = av.getRanges();
1975 int canvasHeight = getHeight();
1976 int repeats = canvasHeight / wrappedHeight;
1977 if (canvasHeight / wrappedHeight > 0)
1982 int firstVisibleColumn = ranges.getStartRes();
1983 int lastVisibleColumn = ranges.getStartRes() + repeats
1984 * ranges.getViewportWidth() - 1;
1986 AlignmentI alignment = av.getAlignment();
1987 if (av.hasHiddenColumns())
1989 firstVisibleColumn = alignment.getHiddenColumns()
1990 .adjustForHiddenColumns(firstVisibleColumn);
1991 lastVisibleColumn = alignment.getHiddenColumns()
1992 .adjustForHiddenColumns(lastVisibleColumn);
1995 int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
1997 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1998 .getEndSeq(); seqNo++)
2000 SequenceI seq = alignment.getSequenceAt(seqNo);
2002 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2004 if (visibleResults != null)
2006 for (int i = 0; i < visibleResults.length - 1; i += 2)
2008 int firstMatchedColumn = visibleResults[i];
2009 int lastMatchedColumn = visibleResults[i + 1];
2010 if (firstMatchedColumn <= lastVisibleColumn
2011 && lastMatchedColumn >= firstVisibleColumn)
2014 * found a search results match in the visible region
2016 firstMatchedColumn = Math.max(firstMatchedColumn,
2017 firstVisibleColumn);
2018 lastMatchedColumn = Math.min(lastMatchedColumn,
2022 * draw each mapped position separately (as contiguous positions may
2023 * wrap across lines)
2025 for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2027 int displayColumn = mappedPos;
2028 if (av.hasHiddenColumns())
2030 displayColumn = alignment.getHiddenColumns()
2031 .findColumnPosition(displayColumn);
2035 * transX: offset from left edge of canvas to residue position
2037 int transX = labelWidthWest
2038 + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2039 * av.getCharWidth();
2042 * transY: offset from top edge of canvas to residue position
2044 int transY = gapHeight;
2045 transY += (displayColumn - ranges.getStartRes())
2046 / wrappedWidth * wrappedHeight;
2047 transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2050 * yOffset is from graphics origin to start of visible region
2052 int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2053 if (transY < getHeight())
2056 gg.translate(transX, transY);
2057 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2059 gg.translate(-transX, -transY);
2071 * Answers the width in pixels of the left scale labels (0 if not shown)
2075 int getLabelWidthWest()
2077 return labelWidthWest;