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.datamodel.VisibleContigsIterator;
29 import jalview.renderer.ScaleRenderer;
30 import jalview.renderer.ScaleRenderer.ScaleMark;
31 import jalview.util.Comparison;
32 import jalview.viewmodel.ViewportListenerI;
33 import jalview.viewmodel.ViewportRanges;
35 import java.awt.AlphaComposite;
36 import java.awt.BasicStroke;
37 import java.awt.BorderLayout;
38 import java.awt.Color;
39 import java.awt.FontMetrics;
40 import java.awt.Graphics;
41 import java.awt.Graphics2D;
42 import java.awt.RenderingHints;
43 import java.awt.Shape;
44 import java.awt.image.BufferedImage;
45 import java.beans.PropertyChangeEvent;
46 import java.util.Iterator;
47 import java.util.List;
49 import javax.swing.JComponent;
52 * The Swing component on which the alignment sequences, and annotations (if
53 * shown), are drawn. This includes scales above, left and right (if shown) in
54 * Wrapped mode, but not the scale above in Unwrapped mode.
57 public class SeqCanvas extends JComponent implements ViewportListenerI
59 private static final String ZEROS = "0000000000";
61 final FeatureRenderer fr;
71 private final SequenceRenderer seqRdr;
73 private boolean fastPaint = false;
75 private boolean fastpainting = false;
77 private AnnotationPanel annotations;
80 * measurements for drawing a wrapped alignment
82 private int labelWidthEast; // label right width in pixels if shown
84 private int labelWidthWest; // label left width in pixels if shown
86 private int wrappedSpaceAboveAlignment; // gap between widths
88 private int wrappedRepeatHeightPx; // height in pixels of wrapped width
90 private int wrappedVisibleWidths; // number of wrapped widths displayed
92 private Graphics2D gg;
95 * Creates a new SeqCanvas object.
99 public SeqCanvas(AlignmentPanel ap)
102 fr = new FeatureRenderer(ap);
103 seqRdr = new SequenceRenderer(av);
104 setLayout(new BorderLayout());
105 PaintRefresher.Register(this, av.getSequenceSetId());
106 setBackground(Color.white);
108 av.getRanges().addPropertyChangeListener(this);
111 public SequenceRenderer getSequenceRenderer()
116 public FeatureRenderer getFeatureRenderer()
122 * Draws the scale above a region of a wrapped alignment, consisting of a
123 * column number every major interval (10 columns).
126 * the graphics context to draw on, positioned at the start (bottom
127 * left) of the line on which to draw any scale marks
129 * start alignment column (0..)
131 * end alignment column (0..)
133 * y offset to draw at
135 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
137 int charHeight = av.getCharHeight();
138 int charWidth = av.getCharWidth();
141 * white fill the scale space (for the fastPaint case)
143 g.setColor(Color.white);
144 g.fillRect(0, ypos - charHeight - charHeight / 2, getWidth(),
145 charHeight * 3 / 2 + 2);
146 g.setColor(Color.black);
148 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
150 for (ScaleMark mark : marks)
152 int mpos = mark.column; // (i - startx - 1)
157 String mstring = mark.text;
163 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
167 * draw a tick mark below the column number, centred on the column;
168 * height of tick mark is 4 pixels less than half a character
170 int xpos = (mpos * charWidth) + (charWidth / 2);
171 g.drawLine(xpos, (ypos + 2) - (charHeight / 2), xpos, ypos - 2);
177 * Draw the scale to the left or right of a wrapped alignment
180 * graphics context, positioned at the start of the scale to be drawn
182 * first column of wrapped width (0.. excluding any hidden columns)
184 * last column of wrapped width (0.. excluding any hidden columns)
186 * vertical offset at which to begin the scale
188 * if true, scale is left of residues, if false, scale is right
190 void drawVerticalScale(Graphics g, final int startx, final int endx,
191 final int ypos, final boolean left)
193 int charHeight = av.getCharHeight();
194 int charWidth = av.getCharWidth();
196 int yPos = ypos + charHeight;
200 if (av.hasHiddenColumns())
202 HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
203 startX = hiddenColumns.visibleToAbsoluteColumn(startx);
204 endX = hiddenColumns.visibleToAbsoluteColumn(endx);
206 FontMetrics fm = getFontMetrics(av.getFont());
208 for (int i = 0; i < av.getAlignment().getHeight(); i++)
210 SequenceI seq = av.getAlignment().getSequenceAt(i);
213 * find sequence position of first non-gapped position -
214 * to the right if scale left, to the left if scale right
216 int index = left ? startX : endX;
218 while (index >= startX && index <= endX)
220 if (!Comparison.isGap(seq.getCharAt(index)))
222 value = seq.findPosition(index);
236 * white fill the space for the scale
238 g.setColor(Color.white);
239 int y = (yPos + (i * charHeight)) - (charHeight / 5);
240 // fillRect origin is top left of rectangle
241 g.fillRect(0, y - charHeight, left ? labelWidthWest : labelWidthEast,
247 * draw scale value, right justified within its width less half a
248 * character width padding on the right
250 int labelSpace = left ? labelWidthWest : labelWidthEast;
251 labelSpace -= charWidth / 2; // leave space to the right
252 String valueAsString = String.valueOf(value);
253 int labelLength = fm.stringWidth(valueAsString);
254 int xOffset = labelSpace - labelLength;
255 g.setColor(Color.black);
256 g.drawString(valueAsString, xOffset, y);
262 * Does a fast paint of an alignment in response to a scroll. Most of the
263 * visible region is simply copied and shifted, and then any newly visible
264 * columns or rows are drawn. The scroll may be horizontal or vertical, but
265 * not both at once. Scrolling may be the result of
267 * <li>dragging a scroll bar</li>
268 * <li>clicking in the scroll bar</li>
269 * <li>scrolling by trackpad, middle mouse button, or other device</li>
270 * <li>by moving the box in the Overview window</li>
271 * <li>programmatically to make a highlighted position visible</li>
275 * columns to shift right (positive) or left (negative)
277 * rows to shift down (positive) or up (negative)
279 public void fastPaint(int horizontal, int vertical)
281 if (fastpainting || gg == null || img == null)
290 int charHeight = av.getCharHeight();
291 int charWidth = av.getCharWidth();
293 ViewportRanges ranges = av.getRanges();
294 int startRes = ranges.getStartRes();
295 int endRes = ranges.getEndRes();
296 int startSeq = ranges.getStartSeq();
297 int endSeq = ranges.getEndSeq();
301 gg.copyArea(horizontal * charWidth, vertical * charHeight,
302 img.getWidth(), img.getHeight(), -horizontal * charWidth,
303 -vertical * charHeight);
305 if (horizontal > 0) // scrollbar pulled right, image to the left
307 transX = (endRes - startRes - horizontal) * charWidth;
308 startRes = endRes - horizontal;
310 else if (horizontal < 0)
312 endRes = startRes - horizontal;
315 if (vertical > 0) // scroll down
317 startSeq = endSeq - vertical;
319 if (startSeq < ranges.getStartSeq())
320 { // ie scrolling too fast, more than a page at a time
321 startSeq = ranges.getStartSeq();
325 transY = img.getHeight() - ((vertical + 1) * charHeight);
328 else if (vertical < 0)
330 endSeq = startSeq - vertical;
332 if (endSeq > ranges.getEndSeq())
334 endSeq = ranges.getEndSeq();
338 gg.translate(transX, transY);
339 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
340 gg.translate(-transX, -transY);
342 // Call repaint on alignment panel so that repaints from other alignment
343 // panel components can be aggregated. Otherwise performance of the
344 // overview window and others may be adversely affected.
345 av.getAlignPanel().repaint();
348 fastpainting = false;
353 public void paintComponent(Graphics g)
355 super.paintComponent(g);
357 int charHeight = av.getCharHeight();
358 int charWidth = av.getCharWidth();
360 ViewportRanges ranges = av.getRanges();
362 int width = getWidth();
363 int height = getHeight();
365 width -= (width % charWidth);
366 height -= (height % charHeight);
368 // selectImage is the selection group outline image
369 BufferedImage selectImage = drawSelectionGroup(
370 ranges.getStartRes(), ranges.getEndRes(),
371 ranges.getStartSeq(), ranges.getEndSeq());
373 if ((img != null) && (fastPaint
374 || (getVisibleRect().width != g.getClipBounds().width)
375 || (getVisibleRect().height != g.getClipBounds().height)))
377 BufferedImage lcimg = buildLocalImage(selectImage);
378 g.drawImage(lcimg, 0, 0, this);
381 else if ((width > 0) && (height > 0))
383 // img is a cached version of the last view we drew, if any
384 // if we have no img or the size has changed, make a new one
385 if (img == null || width != img.getWidth()
386 || height != img.getHeight())
393 gg = (Graphics2D) img.getGraphics();
394 gg.setFont(av.getFont());
399 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
400 RenderingHints.VALUE_ANTIALIAS_ON);
403 gg.setColor(Color.white);
404 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
406 if (av.getWrapAlignment())
408 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
412 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
413 ranges.getStartSeq(), ranges.getEndSeq(), 0);
416 // lcimg is a local *copy* of img which we'll draw selectImage on top of
417 BufferedImage lcimg = buildLocalImage(selectImage);
418 g.drawImage(lcimg, 0, 0, this);
424 drawCursor(g, ranges.getStartRes(), ranges.getEndRes(),
425 ranges.getStartSeq(), ranges.getEndSeq());
430 * Draw an alignment panel for printing
433 * Graphics object to draw with
435 * start residue of print area
437 * end residue of print area
439 * start sequence of print area
441 * end sequence of print area
443 public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
444 int startSeq, int endSeq)
446 drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
448 BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
450 if (selectImage != null)
452 ((Graphics2D) g1).setComposite(AlphaComposite
453 .getInstance(AlphaComposite.SRC_OVER));
454 g1.drawImage(selectImage, 0, 0, this);
459 * Draw a wrapped alignment panel for printing
462 * Graphics object to draw with
464 * width of drawing area
465 * @param canvasHeight
466 * height of drawing area
468 * start residue of print area
470 public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
471 int canvasHeight, int startRes)
473 SequenceGroup group = av.getSelectionGroup();
475 drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
479 BufferedImage selectImage = null;
482 selectImage = new BufferedImage(canvasWidth, canvasHeight,
483 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
484 } catch (OutOfMemoryError er)
487 System.err.println("Print image OutOfMemory Error.\n" + er);
488 new OOMWarning("Creating wrapped alignment image for printing", er);
490 if (selectImage != null)
492 Graphics2D g2 = selectImage.createGraphics();
493 setupSelectionGroup(g2, selectImage);
494 drawWrappedSelection(g2, group, canvasWidth, canvasHeight,
498 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
499 g.drawImage(selectImage, 0, 0, this);
506 * Make a local image by combining the cached image img
509 private BufferedImage buildLocalImage(BufferedImage selectImage)
511 // clone the cached image
512 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
515 // BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
517 Graphics2D g2d = lcimg.createGraphics();
518 g2d.drawImage(img, 0, 0, null);
520 // overlay selection group on lcimg
521 if (selectImage != null)
524 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
525 g2d.drawImage(selectImage, 0, 0, this);
534 * Set up a buffered image of the correct height and size for the sequence canvas
536 private BufferedImage setupImage()
538 BufferedImage lcimg = null;
540 int charWidth = av.getCharWidth();
541 int charHeight = av.getCharHeight();
543 int width = getWidth();
544 int height = getHeight();
546 width -= (width % charWidth);
547 height -= (height % charHeight);
549 if ((width < 1) || (height < 1))
556 lcimg = new BufferedImage(width, height,
557 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
558 } catch (OutOfMemoryError er)
562 "Group image OutOfMemory Redraw Error.\n" + er);
563 new OOMWarning("Creating alignment image for display", er);
572 * Returns the visible width of the canvas in residues, after allowing for
573 * East or West scales (if shown)
576 * the width in pixels (possibly including scales)
580 public int getWrappedCanvasWidth(int canvasWidth)
582 int charWidth = av.getCharWidth();
584 FontMetrics fm = getFontMetrics(av.getFont());
588 if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
590 labelWidth = getLabelWidth(fm);
593 labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
595 labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
597 return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
601 * Returns a pixel width sufficient to show the largest sequence coordinate
602 * (end position) in the alignment, calculated as the FontMetrics width of
603 * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
604 * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
605 * half a character width space on either side.
610 protected int getLabelWidth(FontMetrics fm)
613 * find the biggest sequence end position we need to show
614 * (note this is not necessarily the sequence length)
617 AlignmentI alignment = av.getAlignment();
618 for (int i = 0; i < alignment.getHeight(); i++)
620 maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
624 for (int i = maxWidth; i > 0; i /= 10)
629 return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
633 * Draws as many widths of a wrapped alignment as can fit in the visible
638 * available width in pixels
639 * @param canvasHeight
640 * available height in pixels
642 * the first column (0...) of the alignment to draw
644 public void drawWrappedPanel(Graphics g, int canvasWidth,
645 int canvasHeight, final int startColumn)
647 int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
650 av.setWrappedWidth(wrappedWidthInResidues);
652 ViewportRanges ranges = av.getRanges();
653 ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
656 * draw one width at a time (including any scales or annotation shown),
657 * until we have run out of either alignment or vertical space available
659 int ypos = wrappedSpaceAboveAlignment;
660 int maxWidth = ranges.getVisibleAlignmentWidth();
662 int start = startColumn;
663 int currentWidth = 0;
664 while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
667 .min(maxWidth, start + wrappedWidthInResidues - 1);
668 drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
669 ypos += wrappedRepeatHeightPx;
670 start += wrappedWidthInResidues;
674 drawWrappedDecorators(g, startColumn);
678 * Calculates and saves values needed when rendering a wrapped alignment.
679 * These depend on many factors, including
681 * <li>canvas width and height</li>
682 * <li>number of visible sequences, and height of annotations if shown</li>
683 * <li>font and character width</li>
684 * <li>whether scales are shown left, right or above the alignment</li>
688 * @param canvasHeight
689 * @return the number of residue columns in each width
691 protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
693 int charHeight = av.getCharHeight();
696 * vertical space in pixels between wrapped widths of alignment
697 * - one character height, or two if scale above is drawn
699 wrappedSpaceAboveAlignment = charHeight
700 * (av.getScaleAboveWrapped() ? 2 : 1);
703 * height in pixels of the wrapped widths
705 wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
707 wrappedRepeatHeightPx += av.getRanges().getViewportHeight()
709 // add annotations panel height if shown
710 wrappedRepeatHeightPx += getAnnotationHeight();
713 * number of visible widths (the last one may be part height),
714 * ensuring a part height includes at least one sequence
716 ViewportRanges ranges = av.getRanges();
717 wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
718 int remainder = canvasHeight % wrappedRepeatHeightPx;
719 if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
721 wrappedVisibleWidths++;
725 * compute width in residues; this also sets East and West label widths
727 int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
730 * limit visibleWidths to not exceed width of alignment
732 int xMax = ranges.getVisibleAlignmentWidth();
733 int startToEnd = xMax - ranges.getStartRes();
734 int maxWidths = startToEnd / wrappedWidthInResidues;
735 if (startToEnd % wrappedWidthInResidues > 0)
739 wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
741 return wrappedWidthInResidues;
745 * Draws one width of a wrapped alignment, including sequences and
746 * annnotations, if shown, but not scales or hidden column markers
752 * @param canvasHeight
754 protected void drawWrappedWidth(Graphics g, int ypos, int startColumn,
755 int endColumn, int canvasHeight)
757 ViewportRanges ranges = av.getRanges();
758 int viewportWidth = ranges.getViewportWidth();
760 int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
763 * move right before drawing by the width of the scale left (if any)
764 * plus column offset from left margin (usually zero, but may be non-zero
765 * when fast painting is drawing just a few columns)
767 int charWidth = av.getCharWidth();
768 int xOffset = labelWidthWest
769 + ((startColumn - ranges.getStartRes()) % viewportWidth)
771 g.translate(xOffset, 0);
773 // When printing we have an extra clipped region,
774 // the Printable page which we need to account for here
775 Shape clip = g.getClip();
779 g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
783 g.setClip(0, (int) clip.getBounds().getY(),
784 viewportWidth * charWidth, (int) clip.getBounds().getHeight());
788 * white fill the region to be drawn (so incremental fast paint doesn't
789 * scribble over an existing image)
791 g.setColor(Color.white);
792 g.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
793 wrappedRepeatHeightPx);
795 drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
798 int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
800 if (av.isShowAnnotation())
802 g.translate(0, cHeight + ypos + 3);
803 if (annotations == null)
805 annotations = new AnnotationPanel(av);
808 annotations.renderer.drawComponent(annotations, av, g, -1,
809 startColumn, endx + 1);
810 g.translate(0, -cHeight - ypos - 3);
813 g.translate(-xOffset, 0);
817 * Draws scales left, right and above (if shown), and any hidden column
818 * markers, on all widths of the wrapped alignment
823 protected void drawWrappedDecorators(Graphics g, final int startColumn)
825 int charWidth = av.getCharWidth();
827 g.setFont(av.getFont());
828 g.setColor(Color.black);
830 int ypos = wrappedSpaceAboveAlignment;
831 ViewportRanges ranges = av.getRanges();
832 int viewportWidth = ranges.getViewportWidth();
833 int maxWidth = ranges.getVisibleAlignmentWidth();
835 int startCol = startColumn;
837 while (widthsDrawn < wrappedVisibleWidths)
839 int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
841 if (av.getScaleLeftWrapped())
843 drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
846 if (av.getScaleRightWrapped())
848 int x = labelWidthWest + viewportWidth * charWidth;
850 drawVerticalScale(g, startCol, endColumn, ypos, false);
855 * white fill region of scale above and hidden column markers
856 * (to support incremental fast paint of image)
858 g.translate(labelWidthWest, 0);
859 g.setColor(Color.white);
860 g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
861 * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
862 g.setColor(Color.black);
863 g.translate(-labelWidthWest, 0);
865 g.translate(labelWidthWest, 0);
867 if (av.getScaleAboveWrapped())
869 drawNorthScale(g, startCol, endColumn, ypos);
872 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
874 drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
877 g.translate(-labelWidthWest, 0);
879 ypos += wrappedRepeatHeightPx;
880 startCol += viewportWidth;
886 * Draws markers (triangles) above hidden column positions between startColumn
894 protected void drawHiddenColumnMarkers(Graphics g, int ypos,
895 int startColumn, int endColumn)
897 int charHeight = av.getCharHeight();
898 int charWidth = av.getCharWidth();
900 g.setColor(Color.blue);
902 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
904 Iterator<Integer> it = hidden.getStartRegionIterator(startColumn,
908 res = it.next() - startColumn;
910 if (res < 0 || res > endColumn - startColumn + 1)
916 * draw a downward-pointing triangle at the hidden columns location
917 * (before the following visible column)
919 int xMiddle = res * charWidth;
920 int[] xPoints = new int[] { xMiddle - charHeight / 4,
921 xMiddle + charHeight / 4, xMiddle };
922 int yTop = ypos - (charHeight / 2);
923 int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
924 g.fillPolygon(xPoints, yPoints, 3);
929 * Draw a selection group over a wrapped alignment
931 private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
933 int canvasHeight, int startRes)
935 int charHeight = av.getCharHeight();
936 int charWidth = av.getCharWidth();
938 // height gap above each panel
939 int hgap = charHeight;
940 if (av.getScaleAboveWrapped())
945 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
947 int cHeight = av.getAlignment().getHeight() * charHeight;
949 int startx = startRes;
951 int ypos = hgap; // vertical offset
952 int maxwidth = av.getAlignment().getWidth();
954 if (av.hasHiddenColumns())
956 maxwidth = av.getAlignment().getHiddenColumns()
957 .absoluteToVisibleColumn(maxwidth);
960 // chop the wrapped alignment extent up into panel-sized blocks and treat
961 // each block as if it were a block from an unwrapped alignment
962 while ((ypos <= canvasHeight) && (startx < maxwidth))
964 // set end value to be start + width, or maxwidth, whichever is smaller
965 endx = startx + cWidth - 1;
972 g.translate(labelWidthWest, 0);
974 drawUnwrappedSelection(g, group, startx, endx, 0,
975 av.getAlignment().getHeight() - 1,
978 g.translate(-labelWidthWest, 0);
980 // update vertical offset
981 ypos += cHeight + getAnnotationHeight() + hgap;
983 // update horizontal offset
988 int getAnnotationHeight()
990 if (!av.isShowAnnotation())
995 if (annotations == null)
997 annotations = new AnnotationPanel(av);
1000 return annotations.adjustPanelHeight();
1004 * Draws the visible region of the alignment on the graphics context. If there
1005 * are hidden column markers in the visible region, then each sub-region
1006 * between the markers is drawn separately, followed by the hidden column
1010 * the graphics context, positioned at the first residue to be drawn
1012 * offset of the first column to draw (0..)
1014 * offset of the last column to draw (0..)
1016 * offset of the first sequence to draw (0..)
1018 * offset of the last sequence to draw (0..)
1020 * vertical offset at which to draw (for wrapped alignments)
1022 public void drawPanel(Graphics g1, final int startRes, final int endRes,
1023 final int startSeq, final int endSeq, final int yOffset)
1025 int charHeight = av.getCharHeight();
1026 int charWidth = av.getCharWidth();
1028 if (!av.hasHiddenColumns())
1030 draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
1038 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
1039 VisibleContigsIterator regions = hidden
1040 .getVisContigsIterator(startRes, endRes + 1, true);
1042 while (regions.hasNext())
1044 int[] region = regions.next();
1045 blockEnd = region[1];
1046 blockStart = region[0];
1049 * draw up to just before the next hidden region, or the end of
1050 * the visible region, whichever comes first
1052 g1.translate(screenY * charWidth, 0);
1054 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1057 * draw the downline of the hidden column marker (ScalePanel draws the
1058 * triangle on top) if we reached it
1060 if (av.getShowHiddenMarkers()
1061 && (regions.hasNext() || regions.endsAtHidden()))
1063 g1.setColor(Color.blue);
1065 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1066 0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1067 (endSeq - startSeq + 1) * charHeight + yOffset);
1070 g1.translate(-screenY * charWidth, 0);
1071 screenY += blockEnd - blockStart + 1;
1078 * Draws a region of the visible alignment
1082 * offset of the first column in the visible region (0..)
1084 * offset of the last column in the visible region (0..)
1086 * offset of the first sequence in the visible region (0..)
1088 * offset of the last sequence in the visible region (0..)
1090 * vertical offset at which to draw (for wrapped alignments)
1092 private void draw(Graphics g, int startRes, int endRes, int startSeq,
1093 int endSeq, int offset)
1095 int charHeight = av.getCharHeight();
1096 int charWidth = av.getCharWidth();
1098 g.setFont(av.getFont());
1099 seqRdr.prepare(g, av.isRenderGaps());
1103 // / First draw the sequences
1104 // ///////////////////////////
1105 for (int i = startSeq; i <= endSeq; i++)
1107 nextSeq = av.getAlignment().getSequenceAt(i);
1108 if (nextSeq == null)
1110 // occasionally, a race condition occurs such that the alignment row is
1114 seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1115 startRes, endRes, offset + ((i - startSeq) * charHeight));
1117 if (av.isShowSequenceFeatures())
1119 fr.drawSequence(g, nextSeq, startRes, endRes,
1120 offset + ((i - startSeq) * charHeight), false);
1124 * highlight search Results once sequence has been drawn
1126 if (av.hasSearchResults())
1128 SearchResultsI searchResults = av.getSearchResults();
1129 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
1131 if (visibleResults != null)
1133 for (int r = 0; r < visibleResults.length; r += 2)
1135 seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1136 visibleResults[r + 1],
1137 (visibleResults[r] - startRes) * charWidth,
1138 offset + ((i - startSeq) * charHeight));
1144 if (av.getSelectionGroup() != null
1145 || av.getAlignment().getGroups().size() > 0)
1147 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1152 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1153 int startSeq, int endSeq, int offset)
1155 Graphics2D g = (Graphics2D) g1;
1157 // ///////////////////////////////////
1158 // Now outline any areas if necessary
1159 // ///////////////////////////////////
1161 SequenceGroup group = null;
1162 int groupIndex = -1;
1164 if (av.getAlignment().getGroups().size() > 0)
1166 group = av.getAlignment().getGroups().get(0);
1172 g.setStroke(new BasicStroke());
1173 g.setColor(group.getOutlineColour());
1177 drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1182 g.setStroke(new BasicStroke());
1184 if (groupIndex >= av.getAlignment().getGroups().size())
1189 group = av.getAlignment().getGroups().get(groupIndex);
1191 } while (groupIndex < av.getAlignment().getGroups().size());
1199 * Draw the selection group as a separate image and overlay
1201 private BufferedImage drawSelectionGroup(int startRes, int endRes,
1202 int startSeq, int endSeq)
1204 // get a new image of the correct size
1205 BufferedImage selectionImage = setupImage();
1207 if (selectionImage == null)
1212 SequenceGroup group = av.getSelectionGroup();
1219 // set up drawing colour
1220 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1222 setupSelectionGroup(g, selectionImage);
1224 if (!av.getWrapAlignment())
1226 drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1231 drawWrappedSelection(g, group, getWidth(), getHeight(),
1232 av.getRanges().getStartRes());
1236 return selectionImage;
1240 * Draw the cursor as a separate image and overlay
1243 * start residue of area to draw cursor in
1245 * end residue of area to draw cursor in
1247 * start sequence of area to draw cursor in
1249 * end sequence of are to draw cursor in
1250 * @return a transparent image of the same size as the sequence canvas, with
1251 * the cursor drawn on it, if any
1253 private void drawCursor(Graphics g, int startRes, int endRes,
1257 // convert the cursorY into a position on the visible alignment
1258 int cursor_ypos = cursorY;
1260 // don't do work unless we have to
1261 if (cursor_ypos >= startSeq && cursor_ypos <= endSeq)
1265 int startx = startRes;
1268 // convert the cursorX into a position on the visible alignment
1269 int cursor_xpos = av.getAlignment().getHiddenColumns()
1270 .absoluteToVisibleColumn(cursorX);
1272 if (av.getAlignment().getHiddenColumns().isVisible(cursorX))
1275 if (av.getWrapAlignment())
1277 // work out the correct offsets for the cursor
1278 int charHeight = av.getCharHeight();
1279 int charWidth = av.getCharWidth();
1280 int canvasWidth = getWidth();
1281 int canvasHeight = getHeight();
1283 // height gap above each panel
1284 int hgap = charHeight;
1285 if (av.getScaleAboveWrapped())
1290 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
1292 int cHeight = av.getAlignment().getHeight() * charHeight;
1294 endx = startx + cWidth - 1;
1295 int ypos = hgap; // vertical offset
1297 // iterate down the wrapped panels
1298 while ((ypos <= canvasHeight) && (endx < cursor_xpos))
1300 // update vertical offset
1301 ypos += cHeight + getAnnotationHeight() + hgap;
1303 // update horizontal offset
1305 endx = startx + cWidth - 1;
1308 xoffset = labelWidthWest;
1311 // now check if cursor is within range for x values
1312 if (cursor_xpos >= startx && cursor_xpos <= endx)
1314 // get the character the cursor is drawn at
1315 SequenceI seq = av.getAlignment().getSequenceAt(cursorY);
1316 char s = seq.getCharAt(cursorX);
1318 seqRdr.drawCursor(g, s,
1319 xoffset + (cursor_xpos - startx) * av.getCharWidth(),
1320 yoffset + (cursor_ypos - startSeq) * av.getCharHeight());
1328 * Set up graphics for selection group
1330 private void setupSelectionGroup(Graphics2D g,
1331 BufferedImage selectionImage)
1333 // set background to transparent
1334 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1335 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1337 // set up foreground to draw red dashed line
1338 g.setComposite(AlphaComposite.Src);
1339 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1340 BasicStroke.JOIN_ROUND, 3f, new float[]
1342 g.setColor(Color.RED);
1346 * Draw a selection group over an unwrapped alignment
1347 * @param g graphics object to draw with
1348 * @param group selection group
1349 * @param startRes start residue of area to draw
1350 * @param endRes end residue of area to draw
1351 * @param startSeq start sequence of area to draw
1352 * @param endSeq end sequence of area to draw
1353 * @param offset vertical offset (used when called from wrapped alignment code)
1355 private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1356 int startRes, int endRes, int startSeq, int endSeq, int offset)
1358 int charWidth = av.getCharWidth();
1360 if (!av.hasHiddenColumns())
1362 drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1367 // package into blocks of visible columns
1372 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
1373 VisibleContigsIterator regions = hidden
1374 .getVisContigsIterator(startRes, endRes + 1, true);
1375 while (regions.hasNext())
1377 int[] region = regions.next();
1378 blockEnd = region[1];
1379 blockStart = region[0];
1381 g.translate(screenY * charWidth, 0);
1382 drawPartialGroupOutline(g, group,
1383 blockStart, blockEnd, startSeq, endSeq, offset);
1385 g.translate(-screenY * charWidth, 0);
1386 screenY += blockEnd - blockStart + 1;
1392 * Draw the selection group as a separate image and overlay
1394 private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1395 int startRes, int endRes, int startSeq, int endSeq,
1398 int charHeight = av.getCharHeight();
1399 int charWidth = av.getCharWidth();
1400 int visWidth = (endRes - startRes + 1) * charWidth;
1404 boolean inGroup = false;
1409 List<SequenceI> seqs = group.getSequences(null);
1411 // position of start residue of group relative to startRes, in pixels
1412 int sx = (group.getStartRes() - startRes) * charWidth;
1414 // width of group in pixels
1415 int xwidth = (((group.getEndRes() + 1) - group.getStartRes())
1418 if (!(sx + xwidth < 0 || sx > visWidth))
1420 for (i = startSeq; i <= endSeq; i++)
1422 sy = verticalOffset + (i - startSeq) * charHeight;
1424 if ((sx <= (endRes - startRes) * charWidth)
1425 && seqs.contains(av.getAlignment().getSequenceAt(i)))
1428 && !seqs.contains(av.getAlignment().getSequenceAt(i + 1)))
1430 bottom = sy + charHeight;
1435 if (((top == -1) && (i == 0)) || !seqs
1436 .contains(av.getAlignment().getSequenceAt(i - 1)))
1447 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1448 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1450 // reset top and bottom
1458 sy = verticalOffset + ((i - startSeq) * charHeight);
1459 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1460 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1466 * Draw horizontal selection group boundaries at top and bottom positions
1469 * graphics object to draw on
1475 * visWidth maximum available width
1477 * position to draw top of group at
1479 * position to draw bottom of group at
1481 private void drawHorizontals(Graphics2D g, int sx, int xwidth,
1482 int visWidth, int top, int bottom)
1492 // don't let width extend beyond current block, or group extent
1494 if (startx + width >= visWidth)
1496 width = visWidth - startx;
1501 g.drawLine(startx, top, startx + width, top);
1506 g.drawLine(startx, bottom - 1, startx + width, bottom - 1);
1511 * Draw vertical lines at sx and sx+xwidth providing they lie within
1515 * graphics object to draw on
1521 * visWidth maximum available width
1527 private void drawVerticals(Graphics2D g, int sx, int xwidth, int visWidth,
1530 // if start position is visible, draw vertical line to left of
1532 if (sx >= 0 && sx < visWidth)
1534 g.drawLine(sx, oldY, sx, sy);
1537 // if end position is visible, draw vertical line to right of
1539 if (sx + xwidth < visWidth)
1541 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1546 * Highlights search results in the visible region by rendering as white text
1547 * on a black background. Any previous highlighting is removed. Answers true
1548 * if any highlight was left on the visible alignment (so status bar should be
1549 * set to match), else false.
1551 * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
1552 * alignment had to be scrolled to show the highlighted region, then it should
1553 * be fully redrawn, otherwise a fast paint can be performed. This argument
1554 * could be removed if fast paint of scrolled wrapped alignment is coded in
1555 * future (JAL-2609).
1558 * @param noFastPaint
1561 public boolean highlightSearchResults(SearchResultsI results,
1562 boolean noFastPaint)
1568 boolean wrapped = av.getWrapAlignment();
1571 fastPaint = !noFastPaint;
1572 fastpainting = fastPaint;
1575 * to avoid redrawing the whole visible region, we instead
1576 * redraw just the minimal regions to remove previous highlights
1579 SearchResultsI previous = av.getSearchResults();
1580 av.setSearchResults(results);
1581 boolean redrawn = false;
1582 boolean drawn = false;
1585 redrawn = drawMappedPositionsWrapped(previous);
1586 drawn = drawMappedPositionsWrapped(results);
1591 redrawn = drawMappedPositions(previous);
1592 drawn = drawMappedPositions(results);
1597 * if highlights were either removed or added, repaint
1605 * return true only if highlights were added
1611 fastpainting = false;
1616 * Redraws the minimal rectangle in the visible region (if any) that includes
1617 * mapped positions of the given search results. Whether or not positions are
1618 * highlighted depends on the SearchResults set on the Viewport. This allows
1619 * this method to be called to either clear or set highlighting. Answers true
1620 * if any positions were drawn (in which case a repaint is still required),
1626 protected boolean drawMappedPositions(SearchResultsI results)
1628 if ((results == null) || (gg == null)) // JAL-2784 check gg is not null
1634 * calculate the minimal rectangle to redraw that
1635 * includes both new and existing search results
1637 int firstSeq = Integer.MAX_VALUE;
1639 int firstCol = Integer.MAX_VALUE;
1641 boolean matchFound = false;
1643 ViewportRanges ranges = av.getRanges();
1644 int firstVisibleColumn = ranges.getStartRes();
1645 int lastVisibleColumn = ranges.getEndRes();
1646 AlignmentI alignment = av.getAlignment();
1647 if (av.hasHiddenColumns())
1649 firstVisibleColumn = alignment.getHiddenColumns()
1650 .visibleToAbsoluteColumn(firstVisibleColumn);
1651 lastVisibleColumn = alignment.getHiddenColumns()
1652 .visibleToAbsoluteColumn(lastVisibleColumn);
1655 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1656 .getEndSeq(); seqNo++)
1658 SequenceI seq = alignment.getSequenceAt(seqNo);
1660 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1662 if (visibleResults != null)
1664 for (int i = 0; i < visibleResults.length - 1; i += 2)
1666 int firstMatchedColumn = visibleResults[i];
1667 int lastMatchedColumn = visibleResults[i + 1];
1668 if (firstMatchedColumn <= lastVisibleColumn
1669 && lastMatchedColumn >= firstVisibleColumn)
1672 * found a search results match in the visible region -
1673 * remember the first and last sequence matched, and the first
1674 * and last visible columns in the matched positions
1677 firstSeq = Math.min(firstSeq, seqNo);
1678 lastSeq = Math.max(lastSeq, seqNo);
1679 firstMatchedColumn = Math.max(firstMatchedColumn,
1680 firstVisibleColumn);
1681 lastMatchedColumn = Math.min(lastMatchedColumn,
1683 firstCol = Math.min(firstCol, firstMatchedColumn);
1684 lastCol = Math.max(lastCol, lastMatchedColumn);
1692 if (av.hasHiddenColumns())
1694 firstCol = alignment.getHiddenColumns()
1695 .absoluteToVisibleColumn(firstCol);
1696 lastCol = alignment.getHiddenColumns().absoluteToVisibleColumn(lastCol);
1698 int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1699 int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1700 gg.translate(transX, transY);
1701 drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1702 gg.translate(-transX, -transY);
1709 public void propertyChange(PropertyChangeEvent evt)
1711 String eventName = evt.getPropertyName();
1713 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1719 else if (eventName.equals(ViewportRanges.MOVE_VIEWPORT))
1727 if (eventName.equals(ViewportRanges.STARTRES)
1728 || eventName.equals(ViewportRanges.STARTRESANDSEQ))
1730 // Make sure we're not trying to draw a panel
1731 // larger than the visible window
1732 if (eventName.equals(ViewportRanges.STARTRES))
1734 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1738 scrollX = ((int[]) evt.getNewValue())[0]
1739 - ((int[]) evt.getOldValue())[0];
1741 ViewportRanges vpRanges = av.getRanges();
1743 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1744 if (scrollX > range)
1748 else if (scrollX < -range)
1753 // Both scrolling and resizing change viewport ranges: scrolling changes
1754 // both start and end points, but resize only changes end values.
1755 // Here we only want to fastpaint on a scroll, with resize using a normal
1756 // paint, so scroll events are identified as changes to the horizontal or
1757 // vertical start value.
1758 if (eventName.equals(ViewportRanges.STARTRES))
1760 if (av.getWrapAlignment())
1762 fastPaintWrapped(scrollX);
1766 fastPaint(scrollX, 0);
1769 else if (eventName.equals(ViewportRanges.STARTSEQ))
1772 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1774 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1776 if (av.getWrapAlignment())
1778 fastPaintWrapped(scrollX);
1782 fastPaint(scrollX, 0);
1785 else if (eventName.equals(ViewportRanges.STARTSEQ))
1788 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1790 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1792 if (av.getWrapAlignment())
1794 fastPaintWrapped(scrollX);
1800 * Does a minimal update of the image for a scroll movement. This method
1801 * handles scroll movements of up to one width of the wrapped alignment (one
1802 * click in the vertical scrollbar). Larger movements (for example after a
1803 * scroll to highlight a mapped position) trigger a full redraw instead.
1806 * number of positions scrolled (right if positive, left if negative)
1808 protected void fastPaintWrapped(int scrollX)
1810 ViewportRanges ranges = av.getRanges();
1812 if (Math.abs(scrollX) > ranges.getViewportWidth())
1815 * shift of more than one view width is
1816 * overcomplicated to handle in this method
1823 if (fastpainting || gg == null)
1829 fastpainting = true;
1833 calculateWrappedGeometry(getWidth(), getHeight());
1836 * relocate the regions of the alignment that are still visible
1838 shiftWrappedAlignment(-scrollX);
1841 * add new columns (sequence, annotation)
1842 * - at top left if scrollX < 0
1843 * - at right of last two widths if scrollX > 0
1847 int startRes = ranges.getStartRes();
1848 drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1849 - scrollX - 1, getHeight());
1853 fastPaintWrappedAddRight(scrollX);
1857 * draw all scales (if shown) and hidden column markers
1859 drawWrappedDecorators(gg, ranges.getStartRes());
1864 fastpainting = false;
1869 * Draws the specified number of columns at the 'end' (bottom right) of a
1870 * wrapped alignment view, including sequences and annotations if shown, but
1871 * not scales. Also draws the same number of columns at the right hand end of
1872 * the second last width shown, if the last width is not full height (so
1873 * cannot simply be copied from the graphics image).
1877 protected void fastPaintWrappedAddRight(int columns)
1884 ViewportRanges ranges = av.getRanges();
1885 int viewportWidth = ranges.getViewportWidth();
1886 int charWidth = av.getCharWidth();
1889 * draw full height alignment in the second last row, last columns, if the
1890 * last row was not full height
1892 int visibleWidths = wrappedVisibleWidths;
1893 int canvasHeight = getHeight();
1894 boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1896 if (lastWidthPartHeight)
1898 int widthsAbove = Math.max(0, visibleWidths - 2);
1899 int ypos = wrappedRepeatHeightPx * widthsAbove
1900 + wrappedSpaceAboveAlignment;
1901 int endRes = ranges.getEndRes();
1902 endRes += widthsAbove * viewportWidth;
1903 int startRes = endRes - columns;
1904 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1908 * white fill first to erase annotations
1910 gg.translate(xOffset, 0);
1911 gg.setColor(Color.white);
1912 gg.fillRect(labelWidthWest, ypos,
1913 (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1914 gg.translate(-xOffset, 0);
1916 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1920 * draw newly visible columns in last wrapped width (none if we
1921 * have reached the end of the alignment)
1922 * y-offset for drawing last width is height of widths above,
1925 int widthsAbove = visibleWidths - 1;
1926 int ypos = wrappedRepeatHeightPx * widthsAbove
1927 + wrappedSpaceAboveAlignment;
1928 int endRes = ranges.getEndRes();
1929 endRes += widthsAbove * viewportWidth;
1930 int startRes = endRes - columns + 1;
1933 * white fill first to erase annotations
1935 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1937 gg.translate(xOffset, 0);
1938 gg.setColor(Color.white);
1939 int width = viewportWidth * charWidth - xOffset;
1940 gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1941 gg.translate(-xOffset, 0);
1943 gg.setFont(av.getFont());
1944 gg.setColor(Color.black);
1946 if (startRes < ranges.getVisibleAlignmentWidth())
1948 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1952 * and finally, white fill any space below the visible alignment
1954 int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1955 if (heightBelow > 0)
1957 gg.setColor(Color.white);
1958 gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1963 * Shifts the visible alignment by the specified number of columns - left if
1964 * negative, right if positive. Copies and moves sequences and annotations (if
1965 * shown). Scales, hidden column markers and any newly visible columns must be
1970 protected void shiftWrappedAlignment(int positions)
1976 int charWidth = av.getCharWidth();
1978 int canvasHeight = getHeight();
1979 ViewportRanges ranges = av.getRanges();
1980 int viewportWidth = ranges.getViewportWidth();
1981 int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
1983 int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
1984 int xMax = ranges.getVisibleAlignmentWidth();
1989 * shift right (after scroll left)
1990 * for each wrapped width (starting with the last), copy (width-positions)
1991 * columns from the left margin to the right margin, and copy positions
1992 * columns from the right margin of the row above (if any) to the
1993 * left margin of the current row
1997 * get y-offset of last wrapped width, first row of sequences
1999 int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
2000 y += wrappedSpaceAboveAlignment;
2001 int copyFromLeftStart = labelWidthWest;
2002 int copyFromRightStart = copyFromLeftStart + widthToCopy;
2006 gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
2007 positions * charWidth, 0);
2010 gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
2011 positions * charWidth, heightToCopy, -widthToCopy,
2012 wrappedRepeatHeightPx);
2015 y -= wrappedRepeatHeightPx;
2021 * shift left (after scroll right)
2022 * for each wrapped width (starting with the first), copy (width-positions)
2023 * columns from the right margin to the left margin, and copy positions
2024 * columns from the left margin of the row below (if any) to the
2025 * right margin of the current row
2027 int xpos = av.getRanges().getStartRes();
2028 int y = wrappedSpaceAboveAlignment;
2029 int copyFromRightStart = labelWidthWest - positions * charWidth;
2031 while (y < canvasHeight)
2033 gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
2034 positions * charWidth, 0);
2035 if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
2036 && (xpos + viewportWidth <= xMax))
2038 gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
2039 * charWidth, heightToCopy, widthToCopy,
2040 -wrappedRepeatHeightPx);
2043 y += wrappedRepeatHeightPx;
2044 xpos += viewportWidth;
2051 * Redraws any positions in the search results in the visible region of a
2052 * wrapped alignment. Any highlights are drawn depending on the search results
2053 * set on the Viewport, not the <code>results</code> argument. This allows
2054 * this method to be called either to clear highlights (passing the previous
2055 * search results), or to draw new highlights.
2060 protected boolean drawMappedPositionsWrapped(SearchResultsI results)
2062 if ((results == null) || (gg == null)) // JAL-2784 check gg is not null
2066 int charHeight = av.getCharHeight();
2068 boolean matchFound = false;
2070 calculateWrappedGeometry(getWidth(), getHeight());
2071 int wrappedWidth = av.getWrappedWidth();
2072 int wrappedHeight = wrappedRepeatHeightPx;
2074 ViewportRanges ranges = av.getRanges();
2075 int canvasHeight = getHeight();
2076 int repeats = canvasHeight / wrappedHeight;
2077 if (canvasHeight / wrappedHeight > 0)
2082 int firstVisibleColumn = ranges.getStartRes();
2083 int lastVisibleColumn = ranges.getStartRes() + repeats
2084 * ranges.getViewportWidth() - 1;
2086 AlignmentI alignment = av.getAlignment();
2087 if (av.hasHiddenColumns())
2089 firstVisibleColumn = alignment.getHiddenColumns()
2090 .visibleToAbsoluteColumn(firstVisibleColumn);
2091 lastVisibleColumn = alignment.getHiddenColumns()
2092 .visibleToAbsoluteColumn(lastVisibleColumn);
2095 int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
2097 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
2098 .getEndSeq(); seqNo++)
2100 SequenceI seq = alignment.getSequenceAt(seqNo);
2102 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2104 if (visibleResults != null)
2106 for (int i = 0; i < visibleResults.length - 1; i += 2)
2108 int firstMatchedColumn = visibleResults[i];
2109 int lastMatchedColumn = visibleResults[i + 1];
2110 if (firstMatchedColumn <= lastVisibleColumn
2111 && lastMatchedColumn >= firstVisibleColumn)
2114 * found a search results match in the visible region
2116 firstMatchedColumn = Math.max(firstMatchedColumn,
2117 firstVisibleColumn);
2118 lastMatchedColumn = Math.min(lastMatchedColumn,
2122 * draw each mapped position separately (as contiguous positions may
2123 * wrap across lines)
2125 for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2127 int displayColumn = mappedPos;
2128 if (av.hasHiddenColumns())
2130 displayColumn = alignment.getHiddenColumns()
2131 .absoluteToVisibleColumn(displayColumn);
2135 * transX: offset from left edge of canvas to residue position
2137 int transX = labelWidthWest
2138 + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2139 * av.getCharWidth();
2142 * transY: offset from top edge of canvas to residue position
2144 int transY = gapHeight;
2145 transY += (displayColumn - ranges.getStartRes())
2146 / wrappedWidth * wrappedHeight;
2147 transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2150 * yOffset is from graphics origin to start of visible region
2152 int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2153 if (transY < getHeight())
2156 gg.translate(transX, transY);
2157 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2159 gg.translate(-transX, -transY);
2171 * Answers the width in pixels of the left scale labels (0 if not shown)
2175 int getLabelWidthWest()
2177 return labelWidthWest;