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.viewmodel.ViewportListenerI;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.AlphaComposite;
34 import java.awt.BasicStroke;
35 import java.awt.BorderLayout;
36 import java.awt.Color;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.Graphics2D;
40 import java.awt.RenderingHints;
41 import java.awt.Shape;
42 import java.awt.image.BufferedImage;
43 import java.beans.PropertyChangeEvent;
44 import java.util.List;
46 import javax.swing.JComponent;
54 public class SeqCanvas extends JComponent implements ViewportListenerI
56 final FeatureRenderer fr;
58 final SequenceRenderer sr;
70 boolean fastPaint = false;
81 * Creates a new SeqCanvas object.
86 public SeqCanvas(AlignmentPanel ap)
90 fr = new FeatureRenderer(ap);
91 sr = new SequenceRenderer(av);
92 setLayout(new BorderLayout());
93 PaintRefresher.Register(this, av.getSequenceSetId());
94 setBackground(Color.white);
96 av.getRanges().addPropertyChangeListener(this);
99 public SequenceRenderer getSequenceRenderer()
104 public FeatureRenderer getFeatureRenderer()
109 int charHeight = 0, charWidth = 0;
111 private void updateViewport()
113 charHeight = av.getCharHeight();
114 charWidth = av.getCharWidth();
129 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
132 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
135 int mpos = mark.column; // (i - startx - 1)
140 String mstring = mark.text;
146 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
148 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
149 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
167 void drawWestScale(Graphics g, int startx, int endx, int ypos)
169 FontMetrics fm = getFontMetrics(av.getFont());
172 if (av.hasHiddenColumns())
174 startx = av.getAlignment().getHiddenColumns()
175 .adjustForHiddenColumns(startx);
176 endx = av.getAlignment().getHiddenColumns()
177 .adjustForHiddenColumns(endx);
180 int maxwidth = av.getAlignment().getWidth();
181 if (av.hasHiddenColumns())
183 maxwidth = av.getAlignment().getHiddenColumns()
184 .findColumnPosition(maxwidth) - 1;
188 for (int i = 0; i < av.getAlignment().getHeight(); i++)
190 SequenceI seq = av.getAlignment().getSequenceAt(i);
196 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
203 value = av.getAlignment().getSequenceAt(i).findPosition(index);
210 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
212 g.drawString(value + "", x, (ypos + (i * charHeight))
230 void drawEastScale(Graphics g, int startx, int endx, int ypos)
234 if (av.hasHiddenColumns())
236 endx = av.getAlignment().getHiddenColumns()
237 .adjustForHiddenColumns(endx);
242 for (int i = 0; i < av.getAlignment().getHeight(); i++)
244 seq = av.getAlignment().getSequenceAt(i);
248 while (index > startx)
250 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
257 value = seq.findPosition(index);
264 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
270 boolean fastpainting = false;
273 * need to make this thread safe move alignment rendering in response to
279 * shift up or down in repaint
281 public void fastPaint(int horizontal, int vertical)
283 if (fastpainting || gg == null)
291 ViewportRanges ranges = av.getRanges();
292 int sr = ranges.getStartRes();
293 int er = ranges.getEndRes();
294 int ss = ranges.getStartSeq();
295 int es = ranges.getEndSeq();
299 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
300 imgHeight, -horizontal * charWidth, -vertical * charHeight);
302 if (horizontal > 0) // scrollbar pulled right, image to the left
304 transX = (er - sr - horizontal) * charWidth;
305 sr = er - horizontal;
307 else if (horizontal < 0)
309 er = sr - horizontal;
311 else if (vertical > 0) // scroll down
315 if (ss < ranges.getStartSeq())
316 { // ie scrolling too fast, more than a page at a time
317 ss = ranges.getStartSeq();
321 transY = imgHeight - ((vertical + 1) * charHeight);
324 else if (vertical < 0)
328 if (es > ranges.getEndSeq())
330 es = ranges.getEndSeq();
334 gg.translate(transX, transY);
335 drawPanel(gg, sr, er, ss, es, 0);
336 gg.translate(-transX, -transY);
339 fastpainting = false;
343 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
344 * towards! startx is the first residue (starting at 0) to display. endx is
345 * the last residue to display (starting at 0). starty is the first sequence
346 * to display (starting at 0). endy is the last sequence to display (starting
347 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
348 * adjustment listener in SeqPanel when the scrollbars move.
351 // Set this to false to force a full panel paint
353 public void paintComponent(Graphics g)
357 // img is a cached version of the last view we drew
358 // selectImage will hold any selection we have
359 // lcimg is a local *copy* of img which we'll draw selectImage on top of
369 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
371 Graphics2D g2d = lcimg.createGraphics();
372 g2d.drawImage(img, 0, 0, null);
376 // BufferedImage lcimg = img; // take reference since other threads may null
377 // img and call later.
378 super.paintComponent(g);
380 BufferedImage selectImage = drawSelectionGroup();
384 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
385 .getClipBounds().height)))
387 Graphics2D g2 = (Graphics2D) lcimg.getGraphics();
389 // overlay selection group on lcimg
390 if (selectImage != null)
393 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
394 g2.drawImage(selectImage, 0, 0, this);
396 g.drawImage(lcimg, 0, 0, this);
404 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
405 RenderingHints.VALUE_ANTIALIAS_ON);
408 gg.setColor(Color.white);
409 gg.fillRect(0, 0, imgWidth, imgHeight);
411 ViewportRanges ranges = av.getRanges();
412 if (av.getWrapAlignment())
414 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
418 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
419 ranges.getStartSeq(), ranges.getEndSeq(), 0);
422 lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
424 g2d = lcimg.createGraphics();
425 g2d.drawImage(img, 0, 0, null);
428 Graphics2D g2 = (Graphics2D) lcimg.getGraphics();
430 // overlay selection group on lcimg
431 if (selectImage != null)
434 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
435 g2.drawImage(selectImage, 0, 0, this);
437 g.drawImage(lcimg, 0, 0, this);
440 private void paintSeqGroup()
446 private void setupImage()
448 // this draws the whole of the alignment
449 imgWidth = getWidth();
450 imgHeight = getHeight();
452 imgWidth -= (imgWidth % charWidth);
453 imgHeight -= (imgHeight % charHeight);
455 if ((imgWidth < 1) || (imgHeight < 1))
463 img = new BufferedImage(imgWidth, imgHeight,
464 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
465 gg = (Graphics2D) img.getGraphics();
466 gg.setFont(av.getFont());
467 } catch (OutOfMemoryError er)
470 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
471 new OOMWarning("Creating alignment image for display", er);
478 private BufferedImage setupSelectionImage()
480 BufferedImage lcimg = null;
482 int width = getWidth();
483 int height = getHeight();
485 width -= (width % charWidth);
486 height -= (height % charHeight);
488 if ((width < 1) || (height < 1))
495 lcimg = new BufferedImage(width, height,
496 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
497 } catch (OutOfMemoryError er)
501 "Selection Group image OutOfMemory Redraw Error.\n" + er);
502 new OOMWarning("Creating alignment image for display", er);
516 * @return DOCUMENT ME!
518 public int getWrappedCanvasWidth(int cwidth)
520 FontMetrics fm = getFontMetrics(av.getFont());
525 if (av.getScaleRightWrapped())
527 LABEL_EAST = fm.stringWidth(getMask());
530 if (av.getScaleLeftWrapped())
532 LABEL_WEST = fm.stringWidth(getMask());
535 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
539 * Generates a string of zeroes.
548 for (int i = 0; i < av.getAlignment().getHeight(); i++)
550 tmp = av.getAlignment().getSequenceAt(i).getEnd();
557 for (int i = maxWidth; i > 0; i /= 10)
571 * @param canvasHeight
576 public void drawWrappedPanel(Graphics g, int canvasWidth,
577 int canvasHeight, int startRes)
580 AlignmentI al = av.getAlignment();
582 FontMetrics fm = getFontMetrics(av.getFont());
587 if (av.getScaleRightWrapped())
589 LABEL_EAST = fm.stringWidth(getMask());
592 if (av.getScaleLeftWrapped())
594 LABEL_WEST = fm.stringWidth(getMask());
597 int hgap = charHeight;
598 if (av.getScaleAboveWrapped())
603 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
604 int cHeight = av.getAlignment().getHeight() * charHeight;
606 av.setWrappedWidth(cWidth);
608 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
612 int maxwidth = av.getAlignment().getWidth();
614 if (av.hasHiddenColumns())
616 maxwidth = av.getAlignment().getHiddenColumns()
617 .findColumnPosition(maxwidth);
620 while ((ypos <= canvasHeight) && (startRes < maxwidth))
622 endx = startRes + cWidth - 1;
629 g.setFont(av.getFont());
630 g.setColor(Color.black);
632 if (av.getScaleLeftWrapped())
634 drawWestScale(g, startRes, endx, ypos);
637 if (av.getScaleRightWrapped())
639 g.translate(canvasWidth - LABEL_EAST, 0);
640 drawEastScale(g, startRes, endx, ypos);
641 g.translate(-(canvasWidth - LABEL_EAST), 0);
644 g.translate(LABEL_WEST, 0);
646 if (av.getScaleAboveWrapped())
648 drawNorthScale(g, startRes, endx, ypos);
651 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
653 g.setColor(Color.blue);
655 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
656 List<Integer> positions = hidden.findHiddenRegionPositions();
657 for (int pos : positions)
659 res = pos - startRes;
661 if (res < 0 || res > endx - startRes)
667 new int[] { res * charWidth - charHeight / 4,
668 res * charWidth + charHeight / 4, res * charWidth },
669 new int[] { ypos - (charHeight / 2),
670 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
676 // When printing we have an extra clipped region,
677 // the Printable page which we need to account for here
678 Shape clip = g.getClip();
682 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
686 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
687 (int) clip.getBounds().getHeight());
690 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
692 if (av.isShowAnnotation())
694 g.translate(0, cHeight + ypos + 3);
695 if (annotations == null)
697 annotations = new AnnotationPanel(av);
700 annotations.renderer.drawComponent(annotations, av, g, -1,
702 g.translate(0, -cHeight - ypos - 3);
705 g.translate(-LABEL_WEST, 0);
707 ypos += cHeight + getAnnotationHeight() + hgap;
713 AnnotationPanel annotations;
715 int getAnnotationHeight()
717 if (!av.isShowAnnotation())
722 if (annotations == null)
724 annotations = new AnnotationPanel(av);
727 return annotations.adjustPanelHeight();
746 public void drawPanel(Graphics g1, int startRes, int endRes,
747 int startSeq, int endSeq, int offset)
750 if (!av.hasHiddenColumns())
752 draw(g1, startRes, endRes, startSeq, endSeq, offset);
757 int blockStart = startRes;
758 int blockEnd = endRes;
760 for (int[] region : av.getAlignment().getHiddenColumns()
761 .getHiddenColumnsCopy())
763 int hideStart = region[0];
764 int hideEnd = region[1];
766 if (hideStart <= blockStart)
768 blockStart += (hideEnd - hideStart) + 1;
772 blockEnd = hideStart - 1;
774 g1.translate(screenY * charWidth, 0);
776 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
778 if (av.getShowHiddenMarkers())
780 g1.setColor(Color.blue);
782 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
783 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
784 (endSeq - startSeq + 1) * charHeight + offset);
787 g1.translate(-screenY * charWidth, 0);
788 screenY += blockEnd - blockStart + 1;
789 blockStart = hideEnd + 1;
791 if (screenY > (endRes - startRes))
793 // already rendered last block
798 if (screenY <= (endRes - startRes))
800 // remaining visible region to render
801 blockEnd = blockStart + (endRes - startRes) - screenY;
802 g1.translate(screenY * charWidth, 0);
803 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
805 g1.translate(-screenY * charWidth, 0);
811 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
812 // int x1, int x2, int y1, int y2, int startx, int starty,
813 private void draw(Graphics g, int startRes, int endRes, int startSeq,
814 int endSeq, int offset)
816 g.setFont(av.getFont());
817 sr.prepare(g, av.isRenderGaps());
821 // / First draw the sequences
822 // ///////////////////////////
823 for (int i = startSeq; i <= endSeq; i++)
825 nextSeq = av.getAlignment().getSequenceAt(i);
828 // occasionally, a race condition occurs such that the alignment row is
832 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
833 startRes, endRes, offset + ((i - startSeq) * charHeight));
835 if (av.isShowSequenceFeatures())
837 fr.drawSequence(g, nextSeq, startRes, endRes, offset
838 + ((i - startSeq) * charHeight), false);
841 // / Highlight search Results once all sequences have been drawn
842 // ////////////////////////////////////////////////////////
843 if (av.hasSearchResults())
845 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
847 if (visibleResults != null)
849 for (int r = 0; r < visibleResults.length; r += 2)
851 sr.drawHighlightedText(nextSeq, visibleResults[r],
852 visibleResults[r + 1], (visibleResults[r] - startRes)
854 + ((i - startSeq) * charHeight));
859 if (av.cursorMode && cursorY == i && cursorX >= startRes
860 && cursorX <= endRes)
862 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
863 offset + ((i - startSeq) * charHeight));
867 if (av.getSelectionGroup() != null
868 || av.getAlignment().getGroups().size() > 0)
870 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
875 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
876 int startSeq, int endSeq, int offset)
878 Graphics2D g = (Graphics2D) g1;
880 // ///////////////////////////////////
881 // Now outline any areas if necessary
882 // ///////////////////////////////////
884 SequenceGroup group = null;
890 int visWidth = (endRes - startRes + 1) * charWidth;
892 if (av.getAlignment().getGroups().size() > 0)
894 group = av.getAlignment().getGroups().get(0);
904 boolean inGroup = false;
908 for (i = startSeq; i <= endSeq; i++)
910 // position of start residue of group relative to startRes, in pixels
911 sx = (group.getStartRes() - startRes) * charWidth;
912 sy = offset + ((i - startSeq) * charHeight);
913 // width of group in pixels
914 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
916 if (sx + ex < 0 || sx > visWidth)
921 if ((sx <= (endRes - startRes) * charWidth)
922 && group.getSequences(null).contains(
923 av.getAlignment().getSequenceAt(i)))
926 && !group.getSequences(null).contains(
927 av.getAlignment().getSequenceAt(i + 1)))
929 bottom = sy + charHeight;
934 if (((top == -1) && (i == 0))
935 || !group.getSequences(null).contains(
936 av.getAlignment().getSequenceAt(i - 1)))
944 g.setStroke(new BasicStroke());
945 g.setColor(group.getOutlineColour());
952 // if start position is visible, draw vertical line to left of
954 if (sx >= 0 && sx < visWidth)
956 g.drawLine(sx, oldY, sx, sy);
959 // if end position is visible, draw vertical line to right of
961 if (sx + ex < visWidth)
963 g.drawLine(sx + ex, oldY, sx + ex, sy);
972 if (sx + ex > visWidth)
977 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
979 ex = (endRes - startRes + 1) * charWidth;
982 // draw horizontal line at top of group
985 g.drawLine(sx, top, sx + ex, top);
989 // draw horizontal line at bottom of group
992 g.drawLine(sx, bottom, sx + ex, bottom);
1003 sy = offset + ((i - startSeq) * charHeight);
1004 if (sx >= 0 && sx < visWidth)
1006 g.drawLine(sx, oldY, sx, sy);
1009 if (sx + ex < visWidth)
1011 g.drawLine(sx + ex, oldY, sx + ex, sy);
1020 if (sx + ex > visWidth)
1024 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
1026 ex = (endRes - startRes + 1) * charWidth;
1031 g.drawLine(sx, top, sx + ex, top);
1037 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
1046 g.setStroke(new BasicStroke());
1048 if (groupIndex >= av.getAlignment().getGroups().size())
1053 group = av.getAlignment().getGroups().get(groupIndex);
1055 } while (groupIndex < av.getAlignment().getGroups().size());
1062 * Draw the selection group as a separate image and overlay
1064 private BufferedImage drawSelectionGroup()
1066 // get a new image of the correct size
1067 BufferedImage selectionImage = setupSelectionImage();
1069 if (selectionImage == null)
1074 SequenceGroup group = av.getSelectionGroup();
1081 // set up drawing colour
1082 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1083 // set background to transparent
1084 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1085 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1087 g.setComposite(AlphaComposite.Src);
1088 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1089 BasicStroke.JOIN_ROUND, 3f, new float[]
1091 g.setColor(Color.RED);
1093 int visWidth = av.getRanges().getViewportWidth() * charWidth;
1095 int startRes = av.getRanges().getStartRes();
1097 // set x start and end positions of group
1098 int startx = (group.getStartRes() - startRes) * charWidth;
1099 int endx = (group.getEndRes() - startRes + 1) * charWidth;
1103 boolean inGroup = false;
1107 // get sequences to determine y positions of group
1108 int startSeq = av.getRanges().getStartSeq();
1109 for (i = startSeq; i <= av.getRanges().getEndSeq(); ++i)
1111 int sy = (i - startSeq) * charHeight;
1113 if (group.getSequences(null)
1114 .contains(av.getAlignment().getSequenceAt(i)))
1116 if ((bottom == -1) && !group.getSequences(null)
1117 .contains(av.getAlignment().getSequenceAt(i + 1)))
1119 bottom = sy + charHeight;
1124 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1125 .contains(av.getAlignment().getSequenceAt(i - 1)))
1138 // if start position is visible, draw vertical line to left of
1140 if (startx >= 0 && startx < visWidth * charWidth)
1142 g.drawLine(startx, oldY, startx, sy);
1145 // if end position is visible, draw vertical line to right of
1147 if (endx <= visWidth * charWidth)
1149 g.drawLine(endx, oldY, endx, sy);
1152 if (endx > visWidth * charWidth)
1154 endx = visWidth * charWidth;
1157 // draw horizontal line at top of group
1160 g.drawLine(startx, top, endx, top);
1164 // draw horizontal line at bottom of group
1167 g.drawLine(startx, bottom, endx, bottom);
1177 int sy = (i - startSeq) * charHeight;
1178 if (startx >= 0 && startx < visWidth)
1180 g.drawLine(startx, oldY, startx, sy);
1183 if (endx < visWidth)
1185 g.drawLine(endx, oldY, endx, sy);
1188 if (endx > visWidth)
1195 g.drawLine(startx, top, endx, top);
1201 g.drawLine(startx, bottom - 1, endx, bottom - 1);
1208 return selectionImage;
1217 public void highlightSearchResults(SearchResultsI results)
1221 av.setSearchResults(results);
1227 public void propertyChange(PropertyChangeEvent evt)
1229 String eventName = evt.getPropertyName();
1231 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1235 else if (av.getWrapAlignment())
1237 if (eventName.equals(ViewportRanges.STARTRES))
1245 if (eventName.equals(ViewportRanges.STARTRES))
1247 // Make sure we're not trying to draw a panel
1248 // larger than the visible window
1249 ViewportRanges vpRanges = av.getRanges();
1250 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1251 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1252 if (scrollX > range)
1256 else if (scrollX < -range)
1262 // Both scrolling and resizing change viewport ranges: scrolling changes
1263 // both start and end points, but resize only changes end values.
1264 // Here we only want to fastpaint on a scroll, with resize using a normal
1265 // paint, so scroll events are identified as changes to the horizontal or
1266 // vertical start value.
1267 if (eventName.equals(ViewportRanges.STARTRES))
1269 // scroll - startres and endres both change
1270 fastPaint(scrollX, 0);
1272 else if (eventName.equals(ViewportRanges.STARTSEQ))
1275 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());