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;
313 if (vertical > 0) // scroll down
315 startSeq = endSeq - vertical;
317 if (startSeq < ranges.getStartSeq())
318 { // ie scrolling too fast, more than a page at a time
319 startSeq = ranges.getStartSeq();
323 transY = img.getHeight() - ((vertical + 1) * charHeight);
326 else if (vertical < 0)
328 endSeq = startSeq - vertical;
330 if (endSeq > ranges.getEndSeq())
332 endSeq = ranges.getEndSeq();
336 gg.translate(transX, transY);
337 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
338 gg.translate(-transX, -transY);
343 fastpainting = false;
348 public void paintComponent(Graphics g)
350 super.paintComponent(g);
352 int charHeight = av.getCharHeight();
353 int charWidth = av.getCharWidth();
355 ViewportRanges ranges = av.getRanges();
357 int width = getWidth();
358 int height = getHeight();
360 width -= (width % charWidth);
361 height -= (height % charHeight);
363 // selectImage is the selection group outline image
364 BufferedImage selectImage = drawSelectionGroup(
365 ranges.getStartRes(), ranges.getEndRes(),
366 ranges.getStartSeq(), ranges.getEndSeq());
368 BufferedImage cursorImage = drawCursor(ranges.getStartRes(),
369 ranges.getEndRes(), ranges.getStartSeq(), ranges.getEndSeq());
371 if ((img != null) && (fastPaint
372 || (getVisibleRect().width != g.getClipBounds().width)
373 || (getVisibleRect().height != g.getClipBounds().height)))
375 BufferedImage lcimg = buildLocalImage(selectImage, cursorImage);
376 g.drawImage(lcimg, 0, 0, this);
379 else if ((width > 0) && (height > 0))
381 // img is a cached version of the last view we drew, if any
382 // if we have no img or the size has changed, make a new one
383 if (img == null || width != img.getWidth()
384 || height != img.getHeight())
391 gg = (Graphics2D) img.getGraphics();
392 gg.setFont(av.getFont());
397 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
398 RenderingHints.VALUE_ANTIALIAS_ON);
401 gg.setColor(Color.white);
402 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
404 if (av.getWrapAlignment())
406 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
410 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
411 ranges.getStartSeq(), ranges.getEndSeq(), 0);
414 // lcimg is a local *copy* of img which we'll draw selectImage on top of
415 BufferedImage lcimg = buildLocalImage(selectImage, cursorImage);
416 g.drawImage(lcimg, 0, 0, this);
421 * Draw an alignment panel for printing
424 * Graphics object to draw with
426 * start residue of print area
428 * end residue of print area
430 * start sequence of print area
432 * end sequence of print area
434 public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
435 int startSeq, int endSeq)
437 drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
439 BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
441 if (selectImage != null)
443 ((Graphics2D) g1).setComposite(AlphaComposite
444 .getInstance(AlphaComposite.SRC_OVER));
445 g1.drawImage(selectImage, 0, 0, this);
450 * Draw a wrapped alignment panel for printing
453 * Graphics object to draw with
455 * width of drawing area
456 * @param canvasHeight
457 * height of drawing area
459 * start residue of print area
461 public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
462 int canvasHeight, int startRes)
464 SequenceGroup group = av.getSelectionGroup();
466 drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
470 BufferedImage selectImage = null;
473 selectImage = new BufferedImage(canvasWidth, canvasHeight,
474 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
475 } catch (OutOfMemoryError er)
478 System.err.println("Print image OutOfMemory Error.\n" + er);
479 new OOMWarning("Creating wrapped alignment image for printing", er);
481 if (selectImage != null)
483 Graphics2D g2 = selectImage.createGraphics();
484 setupSelectionGroup(g2, selectImage);
485 drawWrappedSelection(g2, group, canvasWidth, canvasHeight,
489 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
490 g.drawImage(selectImage, 0, 0, this);
497 * Make a local image by combining the cached image img
500 private BufferedImage buildLocalImage(BufferedImage selectImage,
501 BufferedImage cursorImage)
503 // clone the cached image
504 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
506 Graphics2D g2d = lcimg.createGraphics();
507 g2d.drawImage(img, 0, 0, null);
509 // overlay selection group on lcimg
510 if (selectImage != null)
513 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
514 g2d.drawImage(selectImage, 0, 0, this);
516 // overlay cursor on lcimg
517 if (cursorImage != null)
519 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
520 g2d.drawImage(cursorImage, 0, 0, this);
528 * Set up a buffered image of the correct height and size for the sequence canvas
530 private BufferedImage setupImage()
532 BufferedImage lcimg = null;
534 int charWidth = av.getCharWidth();
535 int charHeight = av.getCharHeight();
537 int width = getWidth();
538 int height = getHeight();
540 width -= (width % charWidth);
541 height -= (height % charHeight);
543 if ((width < 1) || (height < 1))
550 lcimg = new BufferedImage(width, height,
551 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
552 } catch (OutOfMemoryError er)
556 "Group image OutOfMemory Redraw Error.\n" + er);
557 new OOMWarning("Creating alignment image for display", er);
566 * Returns the visible width of the canvas in residues, after allowing for
567 * East or West scales (if shown)
570 * the width in pixels (possibly including scales)
574 public int getWrappedCanvasWidth(int canvasWidth)
576 int charWidth = av.getCharWidth();
578 FontMetrics fm = getFontMetrics(av.getFont());
582 if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
584 labelWidth = getLabelWidth(fm);
587 labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
589 labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
591 return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
595 * Returns a pixel width sufficient to show the largest sequence coordinate
596 * (end position) in the alignment, calculated as the FontMetrics width of
597 * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
598 * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
599 * half a character width space on either side.
604 protected int getLabelWidth(FontMetrics fm)
607 * find the biggest sequence end position we need to show
608 * (note this is not necessarily the sequence length)
611 AlignmentI alignment = av.getAlignment();
612 for (int i = 0; i < alignment.getHeight(); i++)
614 maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
618 for (int i = maxWidth; i > 0; i /= 10)
623 return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
627 * Draws as many widths of a wrapped alignment as can fit in the visible
632 * available width in pixels
633 * @param canvasHeight
634 * available height in pixels
636 * the first column (0...) of the alignment to draw
638 public void drawWrappedPanel(Graphics g, int canvasWidth,
639 int canvasHeight, final int startColumn)
641 int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
644 av.setWrappedWidth(wrappedWidthInResidues);
646 ViewportRanges ranges = av.getRanges();
647 ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
650 * draw one width at a time (including any scales or annotation shown),
651 * until we have run out of either alignment or vertical space available
653 int ypos = wrappedSpaceAboveAlignment;
654 int maxWidth = ranges.getVisibleAlignmentWidth();
656 int start = startColumn;
657 int currentWidth = 0;
658 while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
661 .min(maxWidth, start + wrappedWidthInResidues - 1);
662 drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
663 ypos += wrappedRepeatHeightPx;
664 start += wrappedWidthInResidues;
668 drawWrappedDecorators(g, startColumn);
672 * Calculates and saves values needed when rendering a wrapped alignment.
673 * These depend on many factors, including
675 * <li>canvas width and height</li>
676 * <li>number of visible sequences, and height of annotations if shown</li>
677 * <li>font and character width</li>
678 * <li>whether scales are shown left, right or above the alignment</li>
682 * @param canvasHeight
683 * @return the number of residue columns in each width
685 protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
687 int charHeight = av.getCharHeight();
690 * vertical space in pixels between wrapped widths of alignment
691 * - one character height, or two if scale above is drawn
693 wrappedSpaceAboveAlignment = charHeight
694 * (av.getScaleAboveWrapped() ? 2 : 1);
697 * height in pixels of the wrapped widths
699 wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
701 wrappedRepeatHeightPx += av.getRanges().getViewportHeight()
703 // add annotations panel height if shown
704 wrappedRepeatHeightPx += getAnnotationHeight();
707 * number of visible widths (the last one may be part height),
708 * ensuring a part height includes at least one sequence
710 ViewportRanges ranges = av.getRanges();
711 wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
712 int remainder = canvasHeight % wrappedRepeatHeightPx;
713 if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
715 wrappedVisibleWidths++;
719 * compute width in residues; this also sets East and West label widths
721 int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
724 * limit visibleWidths to not exceed width of alignment
726 int xMax = ranges.getVisibleAlignmentWidth();
727 int startToEnd = xMax - ranges.getStartRes();
728 int maxWidths = startToEnd / wrappedWidthInResidues;
729 if (startToEnd % wrappedWidthInResidues > 0)
733 wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
735 return wrappedWidthInResidues;
739 * Draws one width of a wrapped alignment, including sequences and
740 * annnotations, if shown, but not scales or hidden column markers
746 * @param canvasHeight
748 protected void drawWrappedWidth(Graphics g, int ypos, int startColumn,
749 int endColumn, int canvasHeight)
751 ViewportRanges ranges = av.getRanges();
752 int viewportWidth = ranges.getViewportWidth();
754 int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
757 * move right before drawing by the width of the scale left (if any)
758 * plus column offset from left margin (usually zero, but may be non-zero
759 * when fast painting is drawing just a few columns)
761 int charWidth = av.getCharWidth();
762 int xOffset = labelWidthWest
763 + ((startColumn - ranges.getStartRes()) % viewportWidth)
765 g.translate(xOffset, 0);
767 // When printing we have an extra clipped region,
768 // the Printable page which we need to account for here
769 Shape clip = g.getClip();
773 g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
777 g.setClip(0, (int) clip.getBounds().getY(),
778 viewportWidth * charWidth, (int) clip.getBounds().getHeight());
782 * white fill the region to be drawn (so incremental fast paint doesn't
783 * scribble over an existing image)
785 gg.setColor(Color.white);
786 gg.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
787 wrappedRepeatHeightPx);
789 drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
792 int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
794 if (av.isShowAnnotation())
796 g.translate(0, cHeight + ypos + 3);
797 if (annotations == null)
799 annotations = new AnnotationPanel(av);
802 annotations.renderer.drawComponent(annotations, av, g, -1,
803 startColumn, endx + 1);
804 g.translate(0, -cHeight - ypos - 3);
807 g.translate(-xOffset, 0);
811 * Draws scales left, right and above (if shown), and any hidden column
812 * markers, on all widths of the wrapped alignment
817 protected void drawWrappedDecorators(Graphics g, final int startColumn)
819 int charWidth = av.getCharWidth();
821 g.setFont(av.getFont());
822 g.setColor(Color.black);
824 int ypos = wrappedSpaceAboveAlignment;
825 ViewportRanges ranges = av.getRanges();
826 int viewportWidth = ranges.getViewportWidth();
827 int maxWidth = ranges.getVisibleAlignmentWidth();
829 int startCol = startColumn;
831 while (widthsDrawn < wrappedVisibleWidths)
833 int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
835 if (av.getScaleLeftWrapped())
837 drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
840 if (av.getScaleRightWrapped())
842 int x = labelWidthWest + viewportWidth * charWidth;
844 drawVerticalScale(g, startCol, endColumn, ypos, false);
849 * white fill region of scale above and hidden column markers
850 * (to support incremental fast paint of image)
852 g.translate(labelWidthWest, 0);
853 g.setColor(Color.white);
854 g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
855 * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
856 g.setColor(Color.black);
857 g.translate(-labelWidthWest, 0);
859 g.translate(labelWidthWest, 0);
861 if (av.getScaleAboveWrapped())
863 drawNorthScale(g, startCol, endColumn, ypos);
866 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
868 drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
871 g.translate(-labelWidthWest, 0);
873 ypos += wrappedRepeatHeightPx;
874 startCol += viewportWidth;
880 * Draws markers (triangles) above hidden column positions between startColumn
888 protected void drawHiddenColumnMarkers(Graphics g, int ypos,
889 int startColumn, int endColumn)
891 int charHeight = av.getCharHeight();
892 int charWidth = av.getCharWidth();
894 g.setColor(Color.blue);
895 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
896 List<Integer> positions = hidden.findHiddenRegionPositions();
897 for (int pos : positions)
899 int res = pos - startColumn;
901 if (res < 0 || res > endColumn - startColumn + 1)
907 * draw a downward-pointing triangle at the hidden columns location
908 * (before the following visible column)
910 int xMiddle = res * charWidth;
911 int[] xPoints = new int[] { xMiddle - charHeight / 4,
912 xMiddle + charHeight / 4, xMiddle };
913 int yTop = ypos - (charHeight / 2);
914 int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
915 g.fillPolygon(xPoints, yPoints, 3);
920 * Draw a selection group over a wrapped alignment
922 private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
924 int canvasHeight, int startRes)
926 int charHeight = av.getCharHeight();
927 int charWidth = av.getCharWidth();
929 // height gap above each panel
930 int hgap = charHeight;
931 if (av.getScaleAboveWrapped())
936 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
938 int cHeight = av.getAlignment().getHeight() * charHeight;
940 int startx = startRes;
942 int ypos = hgap; // vertical offset
943 int maxwidth = av.getAlignment().getWidth();
945 if (av.hasHiddenColumns())
947 maxwidth = av.getAlignment().getHiddenColumns()
948 .findColumnPosition(maxwidth);
951 // chop the wrapped alignment extent up into panel-sized blocks and treat
952 // each block as if it were a block from an unwrapped alignment
953 while ((ypos <= canvasHeight) && (startx < maxwidth))
955 // set end value to be start + width, or maxwidth, whichever is smaller
956 endx = startx + cWidth - 1;
963 g.translate(labelWidthWest, 0);
965 drawUnwrappedSelection(g, group, startx, endx, 0,
966 av.getAlignment().getHeight() - 1,
969 g.translate(-labelWidthWest, 0);
971 // update vertical offset
972 ypos += cHeight + getAnnotationHeight() + hgap;
974 // update horizontal offset
979 int getAnnotationHeight()
981 if (!av.isShowAnnotation())
986 if (annotations == null)
988 annotations = new AnnotationPanel(av);
991 return annotations.adjustPanelHeight();
995 * Draws the visible region of the alignment on the graphics context. If there
996 * are hidden column markers in the visible region, then each sub-region
997 * between the markers is drawn separately, followed by the hidden column
1001 * the graphics context, positioned at the first residue to be drawn
1003 * offset of the first column to draw (0..)
1005 * offset of the last column to draw (0..)
1007 * offset of the first sequence to draw (0..)
1009 * offset of the last sequence to draw (0..)
1011 * vertical offset at which to draw (for wrapped alignments)
1013 public void drawPanel(Graphics g1, final int startRes, final int endRes,
1014 final int startSeq, final int endSeq, final int yOffset)
1016 int charHeight = av.getCharHeight();
1017 int charWidth = av.getCharWidth();
1019 if (!av.hasHiddenColumns())
1021 draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
1026 final int screenYMax = endRes - startRes;
1027 int blockStart = startRes;
1028 int blockEnd = endRes;
1030 for (int[] region : av.getAlignment().getHiddenColumns()
1031 .getHiddenColumnsCopy())
1033 int hideStart = region[0];
1034 int hideEnd = region[1];
1036 if (hideStart <= blockStart)
1038 blockStart += (hideEnd - hideStart) + 1;
1043 * draw up to just before the next hidden region, or the end of
1044 * the visible region, whichever comes first
1046 blockEnd = Math.min(hideStart - 1, blockStart + screenYMax
1049 g1.translate(screenY * charWidth, 0);
1051 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1054 * draw the downline of the hidden column marker (ScalePanel draws the
1055 * triangle on top) if we reached it
1057 if (av.getShowHiddenMarkers() && blockEnd == hideStart - 1)
1059 g1.setColor(Color.blue);
1061 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1062 0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1063 (endSeq - startSeq + 1) * charHeight + yOffset);
1066 g1.translate(-screenY * charWidth, 0);
1067 screenY += blockEnd - blockStart + 1;
1068 blockStart = hideEnd + 1;
1070 if (screenY > screenYMax)
1072 // already rendered last block
1077 if (screenY <= screenYMax)
1079 // remaining visible region to render
1080 blockEnd = blockStart + screenYMax - screenY;
1081 g1.translate(screenY * charWidth, 0);
1082 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1084 g1.translate(-screenY * charWidth, 0);
1091 * Draws a region of the visible alignment
1095 * offset of the first column in the visible region (0..)
1097 * offset of the last column in the visible region (0..)
1099 * offset of the first sequence in the visible region (0..)
1101 * offset of the last sequence in the visible region (0..)
1103 * vertical offset at which to draw (for wrapped alignments)
1105 private void draw(Graphics g, int startRes, int endRes, int startSeq,
1106 int endSeq, int offset)
1108 int charHeight = av.getCharHeight();
1109 int charWidth = av.getCharWidth();
1111 g.setFont(av.getFont());
1112 seqRdr.prepare(g, av.isRenderGaps());
1116 // / First draw the sequences
1117 // ///////////////////////////
1118 for (int i = startSeq; i <= endSeq; i++)
1120 nextSeq = av.getAlignment().getSequenceAt(i);
1121 if (nextSeq == null)
1123 // occasionally, a race condition occurs such that the alignment row is
1127 seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1128 startRes, endRes, offset + ((i - startSeq) * charHeight));
1130 if (av.isShowSequenceFeatures())
1132 fr.drawSequence(g, nextSeq, startRes, endRes,
1133 offset + ((i - startSeq) * charHeight), false);
1137 * highlight search Results once sequence has been drawn
1139 if (av.hasSearchResults())
1141 SearchResultsI searchResults = av.getSearchResults();
1142 int[] visibleResults = searchResults.getResults(nextSeq,
1144 if (visibleResults != null)
1146 for (int r = 0; r < visibleResults.length; r += 2)
1148 seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1149 visibleResults[r + 1], (visibleResults[r] - startRes)
1151 + ((i - startSeq) * charHeight));
1156 /* if (av.cursorMode && cursorY == i && cursorX >= startRes
1157 && cursorX <= endRes)
1159 seqRdr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
1160 offset + ((i - startSeq) * charHeight));
1164 if (av.getSelectionGroup() != null
1165 || av.getAlignment().getGroups().size() > 0)
1167 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1172 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1173 int startSeq, int endSeq, int offset)
1175 Graphics2D g = (Graphics2D) g1;
1177 // ///////////////////////////////////
1178 // Now outline any areas if necessary
1179 // ///////////////////////////////////
1181 SequenceGroup group = null;
1182 int groupIndex = -1;
1184 if (av.getAlignment().getGroups().size() > 0)
1186 group = av.getAlignment().getGroups().get(0);
1192 g.setStroke(new BasicStroke());
1193 g.setColor(group.getOutlineColour());
1197 drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1202 g.setStroke(new BasicStroke());
1204 if (groupIndex >= av.getAlignment().getGroups().size())
1209 group = av.getAlignment().getGroups().get(groupIndex);
1211 } while (groupIndex < av.getAlignment().getGroups().size());
1219 * Draw the selection group as a separate image and overlay
1221 private BufferedImage drawSelectionGroup(int startRes, int endRes,
1222 int startSeq, int endSeq)
1224 // get a new image of the correct size
1225 BufferedImage selectionImage = setupImage();
1227 if (selectionImage == null)
1232 SequenceGroup group = av.getSelectionGroup();
1239 // set up drawing colour
1240 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1242 setupSelectionGroup(g, selectionImage);
1244 if (!av.getWrapAlignment())
1246 drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1251 drawWrappedSelection(g, group, getWidth(), getHeight(),
1252 av.getRanges().getStartRes());
1256 return selectionImage;
1260 * Draw the cursor as a separate image and overlay
1263 * start residue of area to draw cursor in
1265 * end residue of area to draw cursor in
1267 * start sequence of area to draw cursor in
1269 * end sequence of are to draw cursor in
1270 * @return a transparent image of the same size as the sequence canvas, with
1271 * the cursor drawn on it, if any
1273 private BufferedImage drawCursor(int startRes, int endRes, int startSeq,
1276 // define our cursor image
1277 BufferedImage cursorImage = null;
1279 // don't do work unless we have to
1280 if (av.cursorMode && cursorY >= startSeq && cursorY <= endSeq)
1284 int startx = startRes;
1286 if (av.getWrapAlignment())
1288 // work out the correct offsets for the cursor
1289 int charHeight = av.getCharHeight();
1290 int charWidth = av.getCharWidth();
1291 int canvasWidth = getWidth();
1292 int canvasHeight = getHeight();
1294 // height gap above each panel
1295 int hgap = charHeight;
1296 if (av.getScaleAboveWrapped())
1301 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
1303 int cHeight = av.getAlignment().getHeight() * charHeight;
1305 endx = startx + cWidth - 1;
1306 int ypos = hgap; // vertical offset
1308 // iterate down the wrapped panels
1309 while ((ypos <= canvasHeight) && (endx < cursorX))
1311 // update vertical offset
1312 ypos += cHeight + getAnnotationHeight() + hgap;
1314 // update horizontal offset
1316 endx = startx + cWidth - 1;
1319 xoffset = labelWidthWest;
1322 // now check if cursor is within range for x values
1323 if (cursorX >= startx && cursorX <= endx)
1325 // get a new image of the correct size
1326 cursorImage = setupImage();
1327 Graphics2D g = (Graphics2D) cursorImage.getGraphics();
1329 // get the character the cursor is drawn at
1330 SequenceI seq = av.getAlignment().getSequenceAt(cursorY);
1331 char s = seq.getCharAt(cursorX);
1333 seqRdr.drawCursor(g, s,
1334 xoffset + (cursorX - startx) * av.getCharWidth(),
1335 yoffset + (cursorY - startSeq) * av.getCharHeight());
1345 * Set up graphics for selection group
1347 private void setupSelectionGroup(Graphics2D g,
1348 BufferedImage selectionImage)
1350 // set background to transparent
1351 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1352 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1354 // set up foreground to draw red dashed line
1355 g.setComposite(AlphaComposite.Src);
1356 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1357 BasicStroke.JOIN_ROUND, 3f, new float[]
1359 g.setColor(Color.RED);
1363 * Draw a selection group over an unwrapped alignment
1364 * @param g graphics object to draw with
1365 * @param group selection group
1366 * @param startRes start residue of area to draw
1367 * @param endRes end residue of area to draw
1368 * @param startSeq start sequence of area to draw
1369 * @param endSeq end sequence of area to draw
1370 * @param offset vertical offset (used when called from wrapped alignment code)
1372 private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1373 int startRes, int endRes, int startSeq, int endSeq, int offset)
1375 int charWidth = av.getCharWidth();
1377 if (!av.hasHiddenColumns())
1379 drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1384 // package into blocks of visible columns
1386 int blockStart = startRes;
1387 int blockEnd = endRes;
1389 for (int[] region : av.getAlignment().getHiddenColumns()
1390 .getHiddenColumnsCopy())
1392 int hideStart = region[0];
1393 int hideEnd = region[1];
1395 if (hideStart <= blockStart)
1397 blockStart += (hideEnd - hideStart) + 1;
1401 blockEnd = hideStart - 1;
1403 g.translate(screenY * charWidth, 0);
1404 drawPartialGroupOutline(g, group,
1405 blockStart, blockEnd, startSeq, endSeq, offset);
1407 g.translate(-screenY * charWidth, 0);
1408 screenY += blockEnd - blockStart + 1;
1409 blockStart = hideEnd + 1;
1411 if (screenY > (endRes - startRes))
1413 // already rendered last block
1418 if (screenY <= (endRes - startRes))
1420 // remaining visible region to render
1421 blockEnd = blockStart + (endRes - startRes) - screenY;
1422 g.translate(screenY * charWidth, 0);
1423 drawPartialGroupOutline(g, group,
1424 blockStart, blockEnd, startSeq, endSeq, offset);
1426 g.translate(-screenY * charWidth, 0);
1432 * Draw the selection group as a separate image and overlay
1434 private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1435 int startRes, int endRes, int startSeq, int endSeq,
1438 int charHeight = av.getCharHeight();
1439 int charWidth = av.getCharWidth();
1441 int visWidth = (endRes - startRes + 1) * charWidth;
1445 boolean inGroup = false;
1453 for (i = startSeq; i <= endSeq; i++)
1455 // position of start residue of group relative to startRes, in pixels
1456 sx = (group.getStartRes() - startRes) * charWidth;
1458 // width of group in pixels
1459 xwidth = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth)
1462 sy = verticalOffset + (i - startSeq) * charHeight;
1464 if (sx + xwidth < 0 || sx > visWidth)
1469 if ((sx <= (endRes - startRes) * charWidth)
1470 && group.getSequences(null)
1471 .contains(av.getAlignment().getSequenceAt(i)))
1473 if ((bottom == -1) && !group.getSequences(null)
1474 .contains(av.getAlignment().getSequenceAt(i + 1)))
1476 bottom = sy + charHeight;
1481 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1482 .contains(av.getAlignment().getSequenceAt(i - 1)))
1495 // if start position is visible, draw vertical line to left of
1497 if (sx >= 0 && sx < visWidth)
1499 g.drawLine(sx, oldY, sx, sy);
1502 // if end position is visible, draw vertical line to right of
1504 if (sx + xwidth < visWidth)
1506 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1515 // don't let width extend beyond current block, or group extent
1517 if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1519 xwidth = (endRes - startRes + 1) * charWidth - sx;
1522 // draw horizontal line at top of group
1525 g.drawLine(sx, top, sx + xwidth, top);
1529 // draw horizontal line at bottom of group
1532 g.drawLine(sx, bottom, sx + xwidth, bottom);
1543 sy = verticalOffset + ((i - startSeq) * charHeight);
1544 if (sx >= 0 && sx < visWidth)
1546 g.drawLine(sx, oldY, sx, sy);
1549 if (sx + xwidth < visWidth)
1551 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1560 if (sx + xwidth > visWidth)
1564 else if (sx + xwidth >= (endRes - startRes + 1) * charWidth)
1566 xwidth = (endRes - startRes + 1) * charWidth;
1571 g.drawLine(sx, top, sx + xwidth, top);
1577 g.drawLine(sx, bottom - 1, sx + xwidth, bottom - 1);
1586 * Highlights search results in the visible region by rendering as white text
1587 * on a black background. Any previous highlighting is removed. Answers true
1588 * if any highlight was left on the visible alignment (so status bar should be
1589 * set to match), else false.
1591 * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
1592 * alignment had to be scrolled to show the highlighted region, then it should
1593 * be fully redrawn, otherwise a fast paint can be performed. This argument
1594 * could be removed if fast paint of scrolled wrapped alignment is coded in
1595 * future (JAL-2609).
1598 * @param noFastPaint
1601 public boolean highlightSearchResults(SearchResultsI results,
1602 boolean noFastPaint)
1608 boolean wrapped = av.getWrapAlignment();
1611 fastPaint = !noFastPaint;
1612 fastpainting = fastPaint;
1615 * to avoid redrawing the whole visible region, we instead
1616 * redraw just the minimal regions to remove previous highlights
1619 SearchResultsI previous = av.getSearchResults();
1620 av.setSearchResults(results);
1621 boolean redrawn = false;
1622 boolean drawn = false;
1625 redrawn = drawMappedPositionsWrapped(previous);
1626 drawn = drawMappedPositionsWrapped(results);
1631 redrawn = drawMappedPositions(previous);
1632 drawn = drawMappedPositions(results);
1637 * if highlights were either removed or added, repaint
1645 * return true only if highlights were added
1651 fastpainting = false;
1656 * Redraws the minimal rectangle in the visible region (if any) that includes
1657 * mapped positions of the given search results. Whether or not positions are
1658 * highlighted depends on the SearchResults set on the Viewport. This allows
1659 * this method to be called to either clear or set highlighting. Answers true
1660 * if any positions were drawn (in which case a repaint is still required),
1666 protected boolean drawMappedPositions(SearchResultsI results)
1668 if (results == null)
1674 * calculate the minimal rectangle to redraw that
1675 * includes both new and existing search results
1677 int firstSeq = Integer.MAX_VALUE;
1679 int firstCol = Integer.MAX_VALUE;
1681 boolean matchFound = false;
1683 ViewportRanges ranges = av.getRanges();
1684 int firstVisibleColumn = ranges.getStartRes();
1685 int lastVisibleColumn = ranges.getEndRes();
1686 AlignmentI alignment = av.getAlignment();
1687 if (av.hasHiddenColumns())
1689 firstVisibleColumn = alignment.getHiddenColumns()
1690 .adjustForHiddenColumns(firstVisibleColumn);
1691 lastVisibleColumn = alignment.getHiddenColumns()
1692 .adjustForHiddenColumns(lastVisibleColumn);
1695 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1696 .getEndSeq(); seqNo++)
1698 SequenceI seq = alignment.getSequenceAt(seqNo);
1700 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1702 if (visibleResults != null)
1704 for (int i = 0; i < visibleResults.length - 1; i += 2)
1706 int firstMatchedColumn = visibleResults[i];
1707 int lastMatchedColumn = visibleResults[i + 1];
1708 if (firstMatchedColumn <= lastVisibleColumn
1709 && lastMatchedColumn >= firstVisibleColumn)
1712 * found a search results match in the visible region -
1713 * remember the first and last sequence matched, and the first
1714 * and last visible columns in the matched positions
1717 firstSeq = Math.min(firstSeq, seqNo);
1718 lastSeq = Math.max(lastSeq, seqNo);
1719 firstMatchedColumn = Math.max(firstMatchedColumn,
1720 firstVisibleColumn);
1721 lastMatchedColumn = Math.min(lastMatchedColumn,
1723 firstCol = Math.min(firstCol, firstMatchedColumn);
1724 lastCol = Math.max(lastCol, lastMatchedColumn);
1732 if (av.hasHiddenColumns())
1734 firstCol = alignment.getHiddenColumns()
1735 .findColumnPosition(firstCol);
1736 lastCol = alignment.getHiddenColumns().findColumnPosition(lastCol);
1738 int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1739 int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1740 gg.translate(transX, transY);
1741 drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1742 gg.translate(-transX, -transY);
1749 public void propertyChange(PropertyChangeEvent evt)
1751 String eventName = evt.getPropertyName();
1753 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1759 else if (eventName.equals(ViewportRanges.MOVE_VIEWPORT))
1767 if (eventName.equals(ViewportRanges.STARTRES)
1768 || eventName.equals(ViewportRanges.STARTRESANDSEQ))
1770 // Make sure we're not trying to draw a panel
1771 // larger than the visible window
1772 if (eventName.equals(ViewportRanges.STARTRES))
1774 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1778 scrollX = ((int[]) evt.getNewValue())[0]
1779 - ((int[]) evt.getOldValue())[0];
1781 ViewportRanges vpRanges = av.getRanges();
1783 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1784 if (scrollX > range)
1788 else if (scrollX < -range)
1793 // Both scrolling and resizing change viewport ranges: scrolling changes
1794 // both start and end points, but resize only changes end values.
1795 // Here we only want to fastpaint on a scroll, with resize using a normal
1796 // paint, so scroll events are identified as changes to the horizontal or
1797 // vertical start value.
1798 if (eventName.equals(ViewportRanges.STARTRES))
1800 if (av.getWrapAlignment())
1802 fastPaintWrapped(scrollX);
1806 fastPaint(scrollX, 0);
1809 else if (eventName.equals(ViewportRanges.STARTSEQ))
1812 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1814 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1816 if (av.getWrapAlignment())
1818 fastPaintWrapped(scrollX);
1822 fastPaint(scrollX, 0);
1824 // bizarrely, we only need to scroll on the x value here as fastpaint
1825 // copies the full height of the image anyway. Passing in the y value
1826 // causes nasty repaint artefacts, which only disappear on a full
1833 * Does a minimal update of the image for a scroll movement. This method
1834 * handles scroll movements of up to one width of the wrapped alignment (one
1835 * click in the vertical scrollbar). Larger movements (for example after a
1836 * scroll to highlight a mapped position) trigger a full redraw instead.
1839 * number of positions scrolled (right if positive, left if negative)
1841 protected void fastPaintWrapped(int scrollX)
1843 ViewportRanges ranges = av.getRanges();
1845 if (Math.abs(scrollX) > ranges.getViewportWidth())
1848 * shift of more than one view width is
1849 * overcomplicated to handle in this method
1856 if (fastpainting || gg == null)
1862 fastpainting = true;
1866 calculateWrappedGeometry(getWidth(), getHeight());
1869 * relocate the regions of the alignment that are still visible
1871 shiftWrappedAlignment(-scrollX);
1874 * add new columns (sequence, annotation)
1875 * - at top left if scrollX < 0
1876 * - at right of last two widths if scrollX > 0
1880 int startRes = ranges.getStartRes();
1881 drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1882 - scrollX - 1, getHeight());
1886 fastPaintWrappedAddRight(scrollX);
1890 * draw all scales (if shown) and hidden column markers
1892 drawWrappedDecorators(gg, ranges.getStartRes());
1897 fastpainting = false;
1902 * Draws the specified number of columns at the 'end' (bottom right) of a
1903 * wrapped alignment view, including sequences and annotations if shown, but
1904 * not scales. Also draws the same number of columns at the right hand end of
1905 * the second last width shown, if the last width is not full height (so
1906 * cannot simply be copied from the graphics image).
1910 protected void fastPaintWrappedAddRight(int columns)
1917 ViewportRanges ranges = av.getRanges();
1918 int viewportWidth = ranges.getViewportWidth();
1919 int charWidth = av.getCharWidth();
1922 * draw full height alignment in the second last row, last columns, if the
1923 * last row was not full height
1925 int visibleWidths = wrappedVisibleWidths;
1926 int canvasHeight = getHeight();
1927 boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1929 if (lastWidthPartHeight)
1931 int widthsAbove = Math.max(0, visibleWidths - 2);
1932 int ypos = wrappedRepeatHeightPx * widthsAbove
1933 + wrappedSpaceAboveAlignment;
1934 int endRes = ranges.getEndRes();
1935 endRes += widthsAbove * viewportWidth;
1936 int startRes = endRes - columns;
1937 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1941 * white fill first to erase annotations
1943 gg.translate(xOffset, 0);
1944 gg.setColor(Color.white);
1945 gg.fillRect(labelWidthWest, ypos,
1946 (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1947 gg.translate(-xOffset, 0);
1949 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1953 * draw newly visible columns in last wrapped width (none if we
1954 * have reached the end of the alignment)
1955 * y-offset for drawing last width is height of widths above,
1958 int widthsAbove = visibleWidths - 1;
1959 int ypos = wrappedRepeatHeightPx * widthsAbove
1960 + wrappedSpaceAboveAlignment;
1961 int endRes = ranges.getEndRes();
1962 endRes += widthsAbove * viewportWidth;
1963 int startRes = endRes - columns + 1;
1966 * white fill first to erase annotations
1968 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1970 gg.translate(xOffset, 0);
1971 gg.setColor(Color.white);
1972 int width = viewportWidth * charWidth - xOffset;
1973 gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1974 gg.translate(-xOffset, 0);
1976 gg.setFont(av.getFont());
1977 gg.setColor(Color.black);
1979 if (startRes < ranges.getVisibleAlignmentWidth())
1981 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1985 * and finally, white fill any space below the visible alignment
1987 int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1988 if (heightBelow > 0)
1990 gg.setColor(Color.white);
1991 gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1996 * Shifts the visible alignment by the specified number of columns - left if
1997 * negative, right if positive. Copies and moves sequences and annotations (if
1998 * shown). Scales, hidden column markers and any newly visible columns must be
2003 protected void shiftWrappedAlignment(int positions)
2009 int charWidth = av.getCharWidth();
2011 int canvasHeight = getHeight();
2012 ViewportRanges ranges = av.getRanges();
2013 int viewportWidth = ranges.getViewportWidth();
2014 int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
2016 int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
2017 int xMax = ranges.getVisibleAlignmentWidth();
2022 * shift right (after scroll left)
2023 * for each wrapped width (starting with the last), copy (width-positions)
2024 * columns from the left margin to the right margin, and copy positions
2025 * columns from the right margin of the row above (if any) to the
2026 * left margin of the current row
2030 * get y-offset of last wrapped width, first row of sequences
2032 int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
2033 y += wrappedSpaceAboveAlignment;
2034 int copyFromLeftStart = labelWidthWest;
2035 int copyFromRightStart = copyFromLeftStart + widthToCopy;
2039 gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
2040 positions * charWidth, 0);
2043 gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
2044 positions * charWidth, heightToCopy, -widthToCopy,
2045 wrappedRepeatHeightPx);
2048 y -= wrappedRepeatHeightPx;
2054 * shift left (after scroll right)
2055 * for each wrapped width (starting with the first), copy (width-positions)
2056 * columns from the right margin to the left margin, and copy positions
2057 * columns from the left margin of the row below (if any) to the
2058 * right margin of the current row
2060 int xpos = av.getRanges().getStartRes();
2061 int y = wrappedSpaceAboveAlignment;
2062 int copyFromRightStart = labelWidthWest - positions * charWidth;
2064 while (y < canvasHeight)
2066 gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
2067 positions * charWidth, 0);
2068 if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
2069 && (xpos + viewportWidth <= xMax))
2071 gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
2072 * charWidth, heightToCopy, widthToCopy,
2073 -wrappedRepeatHeightPx);
2076 y += wrappedRepeatHeightPx;
2077 xpos += viewportWidth;
2084 * Redraws any positions in the search results in the visible region of a
2085 * wrapped alignment. Any highlights are drawn depending on the search results
2086 * set on the Viewport, not the <code>results</code> argument. This allows
2087 * this method to be called either to clear highlights (passing the previous
2088 * search results), or to draw new highlights.
2093 protected boolean drawMappedPositionsWrapped(SearchResultsI results)
2095 if (results == null)
2099 int charHeight = av.getCharHeight();
2101 boolean matchFound = false;
2103 calculateWrappedGeometry(getWidth(), getHeight());
2104 int wrappedWidth = av.getWrappedWidth();
2105 int wrappedHeight = wrappedRepeatHeightPx;
2107 ViewportRanges ranges = av.getRanges();
2108 int canvasHeight = getHeight();
2109 int repeats = canvasHeight / wrappedHeight;
2110 if (canvasHeight / wrappedHeight > 0)
2115 int firstVisibleColumn = ranges.getStartRes();
2116 int lastVisibleColumn = ranges.getStartRes() + repeats
2117 * ranges.getViewportWidth() - 1;
2119 AlignmentI alignment = av.getAlignment();
2120 if (av.hasHiddenColumns())
2122 firstVisibleColumn = alignment.getHiddenColumns()
2123 .adjustForHiddenColumns(firstVisibleColumn);
2124 lastVisibleColumn = alignment.getHiddenColumns()
2125 .adjustForHiddenColumns(lastVisibleColumn);
2128 int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
2130 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
2131 .getEndSeq(); seqNo++)
2133 SequenceI seq = alignment.getSequenceAt(seqNo);
2135 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2137 if (visibleResults != null)
2139 for (int i = 0; i < visibleResults.length - 1; i += 2)
2141 int firstMatchedColumn = visibleResults[i];
2142 int lastMatchedColumn = visibleResults[i + 1];
2143 if (firstMatchedColumn <= lastVisibleColumn
2144 && lastMatchedColumn >= firstVisibleColumn)
2147 * found a search results match in the visible region
2149 firstMatchedColumn = Math.max(firstMatchedColumn,
2150 firstVisibleColumn);
2151 lastMatchedColumn = Math.min(lastMatchedColumn,
2155 * draw each mapped position separately (as contiguous positions may
2156 * wrap across lines)
2158 for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2160 int displayColumn = mappedPos;
2161 if (av.hasHiddenColumns())
2163 displayColumn = alignment.getHiddenColumns()
2164 .findColumnPosition(displayColumn);
2168 * transX: offset from left edge of canvas to residue position
2170 int transX = labelWidthWest
2171 + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2172 * av.getCharWidth();
2175 * transY: offset from top edge of canvas to residue position
2177 int transY = gapHeight;
2178 transY += (displayColumn - ranges.getStartRes())
2179 / wrappedWidth * wrappedHeight;
2180 transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2183 * yOffset is from graphics origin to start of visible region
2185 int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2186 if (transY < getHeight())
2189 gg.translate(transX, transY);
2190 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2192 gg.translate(-transX, -transY);
2204 * Answers the width in pixels of the left scale labels (0 if not shown)
2208 int getLabelWidthWest()
2210 return labelWidthWest;