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;
66 boolean fastPaint = false;
77 * Creates a new SeqCanvas object.
82 public SeqCanvas(AlignmentPanel ap)
86 fr = new FeatureRenderer(ap);
87 sr = new SequenceRenderer(av);
88 setLayout(new BorderLayout());
89 PaintRefresher.Register(this, av.getSequenceSetId());
90 setBackground(Color.white);
92 av.getRanges().addPropertyChangeListener(this);
95 public SequenceRenderer getSequenceRenderer()
100 public FeatureRenderer getFeatureRenderer()
105 int charHeight = 0, charWidth = 0;
107 private void updateViewport()
109 charHeight = av.getCharHeight();
110 charWidth = av.getCharWidth();
125 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
128 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
131 int mpos = mark.column; // (i - startx - 1)
136 String mstring = mark.text;
142 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
144 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
145 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
163 void drawWestScale(Graphics g, int startx, int endx, int ypos)
165 FontMetrics fm = getFontMetrics(av.getFont());
168 if (av.hasHiddenColumns())
170 startx = av.getAlignment().getHiddenColumns()
171 .adjustForHiddenColumns(startx);
172 endx = av.getAlignment().getHiddenColumns()
173 .adjustForHiddenColumns(endx);
176 int maxwidth = av.getAlignment().getWidth();
177 if (av.hasHiddenColumns())
179 maxwidth = av.getAlignment().getHiddenColumns()
180 .findColumnPosition(maxwidth) - 1;
184 for (int i = 0; i < av.getAlignment().getHeight(); i++)
186 SequenceI seq = av.getAlignment().getSequenceAt(i);
192 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
199 value = av.getAlignment().getSequenceAt(i).findPosition(index);
206 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
208 g.drawString(value + "", x, (ypos + (i * charHeight))
226 void drawEastScale(Graphics g, int startx, int endx, int ypos)
230 if (av.hasHiddenColumns())
232 endx = av.getAlignment().getHiddenColumns()
233 .adjustForHiddenColumns(endx);
238 for (int i = 0; i < av.getAlignment().getHeight(); i++)
240 seq = av.getAlignment().getSequenceAt(i);
244 while (index > startx)
246 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
253 value = seq.findPosition(index);
260 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
266 boolean fastpainting = false;
269 * need to make this thread safe move alignment rendering in response to
275 * shift up or down in repaint
277 public void fastPaint(int horizontal, int vertical)
279 if (fastpainting || gg == null)
287 ViewportRanges ranges = av.getRanges();
288 int sr = ranges.getStartRes();
289 int er = ranges.getEndRes();
290 int ss = ranges.getStartSeq();
291 int es = ranges.getEndSeq();
295 gg.copyArea(horizontal * charWidth, vertical * charHeight,
296 img.getWidth(), img.getHeight(), -horizontal * charWidth,
297 -vertical * charHeight);
299 if (horizontal > 0) // scrollbar pulled right, image to the left
301 transX = (er - sr - horizontal) * charWidth;
302 sr = er - horizontal;
304 else if (horizontal < 0)
306 er = sr - horizontal;
308 else if (vertical > 0) // scroll down
312 if (ss < ranges.getStartSeq())
313 { // ie scrolling too fast, more than a page at a time
314 ss = ranges.getStartSeq();
318 transY = img.getHeight() - ((vertical + 1) * charHeight);
321 else if (vertical < 0)
325 if (es > ranges.getEndSeq())
327 es = ranges.getEndSeq();
331 gg.translate(transX, transY);
332 drawPanel(gg, sr, er, ss, es, 0);
333 gg.translate(-transX, -transY);
336 fastpainting = false;
340 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
341 * towards! startx is the first residue (starting at 0) to display. endx is
342 * the last residue to display (starting at 0). starty is the first sequence
343 * to display (starting at 0). endy is the last sequence to display (starting
344 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
345 * adjustment listener in SeqPanel when the scrollbars move.
348 // Set this to false to force a full panel paint
350 public void paintComponent(Graphics g)
352 super.paintComponent(g);
356 // img is a cached version of the last view we drew
357 // selectImage will hold any selection we have
358 // lcimg is a local *copy* of img which we'll draw selectImage on top of
360 BufferedImage selectImage = drawSelectionGroup();
362 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
363 || (getVisibleRect().height != g.getClipBounds().height))
365 BufferedImage lcimg = buildLocalImage(selectImage);
366 g.drawImage(lcimg, 0, 0, this);
372 int width = getWidth();
373 int height = getHeight();
375 width -= (width % charWidth);
376 height -= (height % charHeight);
378 if ((width < 1) || (height < 1))
383 if (img == null || width != img.getWidth() || height != img.getHeight())
386 gg = (Graphics2D) img.getGraphics();
387 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 ViewportRanges ranges = av.getRanges();
405 if (av.getWrapAlignment())
407 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
411 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
412 ranges.getStartSeq(), ranges.getEndSeq(), 0);
415 BufferedImage lcimg = buildLocalImage(selectImage);
416 g.drawImage(lcimg, 0, 0, this);
420 * Make a local image by combining the cached image img
423 private BufferedImage buildLocalImage(BufferedImage selectImage)
425 // clone the cached image
426 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
428 Graphics2D g2d = lcimg.createGraphics();
429 g2d.drawImage(img, 0, 0, null);
431 // overlay selection group on lcimg
432 if (selectImage != null)
435 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
436 g2d.drawImage(selectImage, 0, 0, this);
443 private void paintSeqGroup()
449 private BufferedImage setupImage()
451 BufferedImage lcimg = null;
453 int width = getWidth();
454 int height = getHeight();
456 width -= (width % charWidth);
457 height -= (height % charHeight);
459 if ((width < 1) || (height < 1))
466 lcimg = new BufferedImage(width, height,
467 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
468 } catch (OutOfMemoryError er)
472 "Selection Group image OutOfMemory Redraw Error.\n" + er);
473 new OOMWarning("Creating alignment image for display", er);
487 * @return DOCUMENT ME!
489 public int getWrappedCanvasWidth(int cwidth)
491 FontMetrics fm = getFontMetrics(av.getFont());
496 if (av.getScaleRightWrapped())
498 LABEL_EAST = fm.stringWidth(getMask());
501 if (av.getScaleLeftWrapped())
503 LABEL_WEST = fm.stringWidth(getMask());
506 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
510 * Generates a string of zeroes.
519 for (int i = 0; i < av.getAlignment().getHeight(); i++)
521 tmp = av.getAlignment().getSequenceAt(i).getEnd();
528 for (int i = maxWidth; i > 0; i /= 10)
542 * @param canvasHeight
547 public void drawWrappedPanel(Graphics g, int canvasWidth,
548 int canvasHeight, int startRes)
551 AlignmentI al = av.getAlignment();
553 FontMetrics fm = getFontMetrics(av.getFont());
558 if (av.getScaleRightWrapped())
560 LABEL_EAST = fm.stringWidth(getMask());
563 if (av.getScaleLeftWrapped())
565 LABEL_WEST = fm.stringWidth(getMask());
568 int hgap = charHeight;
569 if (av.getScaleAboveWrapped())
574 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
575 int cHeight = av.getAlignment().getHeight() * charHeight;
577 av.setWrappedWidth(cWidth);
579 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
583 int maxwidth = av.getAlignment().getWidth();
585 if (av.hasHiddenColumns())
587 maxwidth = av.getAlignment().getHiddenColumns()
588 .findColumnPosition(maxwidth);
591 while ((ypos <= canvasHeight) && (startRes < maxwidth))
593 endx = startRes + cWidth - 1;
600 g.setFont(av.getFont());
601 g.setColor(Color.black);
603 if (av.getScaleLeftWrapped())
605 drawWestScale(g, startRes, endx, ypos);
608 if (av.getScaleRightWrapped())
610 g.translate(canvasWidth - LABEL_EAST, 0);
611 drawEastScale(g, startRes, endx, ypos);
612 g.translate(-(canvasWidth - LABEL_EAST), 0);
615 g.translate(LABEL_WEST, 0);
617 if (av.getScaleAboveWrapped())
619 drawNorthScale(g, startRes, endx, ypos);
622 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
624 g.setColor(Color.blue);
626 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
627 List<Integer> positions = hidden.findHiddenRegionPositions();
628 for (int pos : positions)
630 res = pos - startRes;
632 if (res < 0 || res > endx - startRes)
638 new int[] { res * charWidth - charHeight / 4,
639 res * charWidth + charHeight / 4, res * charWidth },
640 new int[] { ypos - (charHeight / 2),
641 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
647 // When printing we have an extra clipped region,
648 // the Printable page which we need to account for here
649 Shape clip = g.getClip();
653 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
657 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
658 (int) clip.getBounds().getHeight());
661 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
663 if (av.isShowAnnotation())
665 g.translate(0, cHeight + ypos + 3);
666 if (annotations == null)
668 annotations = new AnnotationPanel(av);
671 annotations.renderer.drawComponent(annotations, av, g, -1,
673 g.translate(0, -cHeight - ypos - 3);
676 g.translate(-LABEL_WEST, 0);
678 ypos += cHeight + getAnnotationHeight() + hgap;
684 AnnotationPanel annotations;
686 int getAnnotationHeight()
688 if (!av.isShowAnnotation())
693 if (annotations == null)
695 annotations = new AnnotationPanel(av);
698 return annotations.adjustPanelHeight();
717 public void drawPanel(Graphics g1, int startRes, int endRes,
718 int startSeq, int endSeq, int offset)
721 if (!av.hasHiddenColumns())
723 draw(g1, startRes, endRes, startSeq, endSeq, offset);
728 int blockStart = startRes;
729 int blockEnd = endRes;
731 for (int[] region : av.getAlignment().getHiddenColumns()
732 .getHiddenColumnsCopy())
734 int hideStart = region[0];
735 int hideEnd = region[1];
737 if (hideStart <= blockStart)
739 blockStart += (hideEnd - hideStart) + 1;
743 blockEnd = hideStart - 1;
745 g1.translate(screenY * charWidth, 0);
747 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
749 if (av.getShowHiddenMarkers())
751 g1.setColor(Color.blue);
753 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
754 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
755 (endSeq - startSeq + 1) * charHeight + offset);
758 g1.translate(-screenY * charWidth, 0);
759 screenY += blockEnd - blockStart + 1;
760 blockStart = hideEnd + 1;
762 if (screenY > (endRes - startRes))
764 // already rendered last block
769 if (screenY <= (endRes - startRes))
771 // remaining visible region to render
772 blockEnd = blockStart + (endRes - startRes) - screenY;
773 g1.translate(screenY * charWidth, 0);
774 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
776 g1.translate(-screenY * charWidth, 0);
782 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
783 // int x1, int x2, int y1, int y2, int startx, int starty,
784 private void draw(Graphics g, int startRes, int endRes, int startSeq,
785 int endSeq, int offset)
787 g.setFont(av.getFont());
788 sr.prepare(g, av.isRenderGaps());
792 // / First draw the sequences
793 // ///////////////////////////
794 for (int i = startSeq; i <= endSeq; i++)
796 nextSeq = av.getAlignment().getSequenceAt(i);
799 // occasionally, a race condition occurs such that the alignment row is
803 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
804 startRes, endRes, offset + ((i - startSeq) * charHeight));
806 if (av.isShowSequenceFeatures())
808 fr.drawSequence(g, nextSeq, startRes, endRes, offset
809 + ((i - startSeq) * charHeight), false);
812 // / Highlight search Results once all sequences have been drawn
813 // ////////////////////////////////////////////////////////
814 if (av.hasSearchResults())
816 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
818 if (visibleResults != null)
820 for (int r = 0; r < visibleResults.length; r += 2)
822 sr.drawHighlightedText(nextSeq, visibleResults[r],
823 visibleResults[r + 1], (visibleResults[r] - startRes)
825 + ((i - startSeq) * charHeight));
830 if (av.cursorMode && cursorY == i && cursorX >= startRes
831 && cursorX <= endRes)
833 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
834 offset + ((i - startSeq) * charHeight));
838 if (av.getSelectionGroup() != null
839 || av.getAlignment().getGroups().size() > 0)
841 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
846 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
847 int startSeq, int endSeq, int offset)
849 Graphics2D g = (Graphics2D) g1;
851 // ///////////////////////////////////
852 // Now outline any areas if necessary
853 // ///////////////////////////////////
855 SequenceGroup group = null;
861 int visWidth = (endRes - startRes + 1) * charWidth;
863 if (av.getAlignment().getGroups().size() > 0)
865 group = av.getAlignment().getGroups().get(0);
875 boolean inGroup = false;
879 for (i = startSeq; i <= endSeq; i++)
881 // position of start residue of group relative to startRes, in pixels
882 sx = (group.getStartRes() - startRes) * charWidth;
883 sy = offset + ((i - startSeq) * charHeight);
884 // width of group in pixels
885 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
887 if (sx + ex < 0 || sx > visWidth)
892 if ((sx <= (endRes - startRes) * charWidth)
893 && group.getSequences(null).contains(
894 av.getAlignment().getSequenceAt(i)))
897 && !group.getSequences(null).contains(
898 av.getAlignment().getSequenceAt(i + 1)))
900 bottom = sy + charHeight;
905 if (((top == -1) && (i == 0))
906 || !group.getSequences(null).contains(
907 av.getAlignment().getSequenceAt(i - 1)))
915 g.setStroke(new BasicStroke());
916 g.setColor(group.getOutlineColour());
923 // if start position is visible, draw vertical line to left of
925 if (sx >= 0 && sx < visWidth)
927 g.drawLine(sx, oldY, sx, sy);
930 // if end position is visible, draw vertical line to right of
932 if (sx + ex < visWidth)
934 g.drawLine(sx + ex, oldY, sx + ex, sy);
943 if (sx + ex > visWidth)
948 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
950 ex = (endRes - startRes + 1) * charWidth;
953 // draw horizontal line at top of group
956 g.drawLine(sx, top, sx + ex, top);
960 // draw horizontal line at bottom of group
963 g.drawLine(sx, bottom, sx + ex, bottom);
974 sy = offset + ((i - startSeq) * charHeight);
975 if (sx >= 0 && sx < visWidth)
977 g.drawLine(sx, oldY, sx, sy);
980 if (sx + ex < visWidth)
982 g.drawLine(sx + ex, oldY, sx + ex, sy);
991 if (sx + ex > visWidth)
995 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
997 ex = (endRes - startRes + 1) * charWidth;
1002 g.drawLine(sx, top, sx + ex, top);
1008 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
1017 g.setStroke(new BasicStroke());
1019 if (groupIndex >= av.getAlignment().getGroups().size())
1024 group = av.getAlignment().getGroups().get(groupIndex);
1026 } while (groupIndex < av.getAlignment().getGroups().size());
1033 * Draw the selection group as a separate image and overlay
1035 private BufferedImage drawSelectionGroup()
1037 int verticalOffset = 0;
1038 if (av.getWrapAlignment())
1040 verticalOffset = charHeight;
1041 if (av.getScaleAboveWrapped())
1043 verticalOffset += charHeight;
1047 // get a new image of the correct size
1048 BufferedImage selectionImage = setupImage();
1050 if (selectionImage == null)
1055 SequenceGroup group = av.getSelectionGroup();
1062 // set up drawing colour
1063 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1064 g.translate(LABEL_WEST, 0);
1065 // set background to transparent
1066 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1067 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1069 g.setComposite(AlphaComposite.Src);
1070 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1071 BasicStroke.JOIN_ROUND, 3f, new float[]
1073 g.setColor(Color.RED);
1075 int visWidth = av.getRanges().getViewportWidth() * charWidth;
1077 int startRes = av.getRanges().getStartRes();
1079 // set x start and end positions of group
1080 int startx = (group.getStartRes() - startRes) * charWidth;
1081 int endx = (group.getEndRes() - startRes + 1) * charWidth;
1085 boolean inGroup = false;
1089 // get sequences to determine y positions of group
1090 int startSeq = av.getRanges().getStartSeq();
1091 for (i = startSeq; i <= av.getRanges().getEndSeq(); ++i)
1093 int sy = verticalOffset + (i - startSeq) * charHeight;
1095 if (group.getSequences(null)
1096 .contains(av.getAlignment().getSequenceAt(i)))
1098 if ((bottom == -1) && !group.getSequences(null)
1099 .contains(av.getAlignment().getSequenceAt(i + 1)))
1101 bottom = sy + charHeight;
1106 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1107 .contains(av.getAlignment().getSequenceAt(i - 1)))
1120 // if start position is visible, draw vertical line to left of
1122 if (startx >= 0 && startx < visWidth * charWidth)
1124 g.drawLine(startx, oldY, startx, sy);
1127 // if end position is visible, draw vertical line to right of
1129 if (endx <= visWidth * charWidth)
1131 g.drawLine(endx, oldY, endx, sy);
1134 if (endx > visWidth * charWidth)
1136 endx = visWidth * charWidth;
1139 // draw horizontal line at top of group
1142 g.drawLine(startx, top, endx, top);
1146 // draw horizontal line at bottom of group
1149 g.drawLine(startx, bottom, endx, bottom);
1159 int sy = verticalOffset + (i - startSeq) * charHeight;
1160 if (startx >= 0 && startx < visWidth)
1162 g.drawLine(startx, oldY, startx, sy);
1165 if (endx < visWidth)
1167 g.drawLine(endx, oldY, endx, sy);
1170 if (endx > visWidth)
1177 g.drawLine(startx, top, endx, top);
1183 g.drawLine(startx, bottom - 1, endx, bottom - 1);
1189 g.translate(-LABEL_WEST, 0);
1190 return selectionImage;
1199 public void highlightSearchResults(SearchResultsI results)
1203 av.setSearchResults(results);
1209 public void propertyChange(PropertyChangeEvent evt)
1211 String eventName = evt.getPropertyName();
1213 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1217 else if (av.getWrapAlignment())
1219 if (eventName.equals(ViewportRanges.STARTRES))
1227 if (eventName.equals(ViewportRanges.STARTRES))
1229 // Make sure we're not trying to draw a panel
1230 // larger than the visible window
1231 ViewportRanges vpRanges = av.getRanges();
1232 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1233 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1234 if (scrollX > range)
1238 else if (scrollX < -range)
1244 // Both scrolling and resizing change viewport ranges: scrolling changes
1245 // both start and end points, but resize only changes end values.
1246 // Here we only want to fastpaint on a scroll, with resize using a normal
1247 // paint, so scroll events are identified as changes to the horizontal or
1248 // vertical start value.
1249 if (eventName.equals(ViewportRanges.STARTRES))
1251 // scroll - startres and endres both change
1252 fastPaint(scrollX, 0);
1254 else if (eventName.equals(ViewportRanges.STARTSEQ))
1257 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());