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.SearchResultsI;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
29 import jalview.viewmodel.ViewportRanges;
31 import java.awt.BasicStroke;
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Graphics2D;
37 import java.awt.RenderingHints;
38 import java.awt.Shape;
39 import java.awt.image.BufferedImage;
40 import java.util.List;
42 import javax.swing.JComponent;
50 public class SeqCanvas extends JComponent
52 final FeatureRenderer fr;
54 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);
93 public SequenceRenderer getSequenceRenderer()
98 public FeatureRenderer getFeatureRenderer()
103 int charHeight = 0, charWidth = 0;
105 private void updateViewport()
107 charHeight = av.getCharHeight();
108 charWidth = av.getCharWidth();
123 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
126 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
129 int mpos = mark.column; // (i - startx - 1)
134 String mstring = mark.text;
140 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
142 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
143 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
161 void drawWestScale(Graphics g, int startx, int endx, int ypos)
163 FontMetrics fm = getFontMetrics(av.getFont());
166 if (av.hasHiddenColumns())
168 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
169 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
172 int maxwidth = av.getAlignment().getWidth();
173 if (av.hasHiddenColumns())
175 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
179 for (int i = 0; i < av.getAlignment().getHeight(); i++)
181 SequenceI seq = av.getAlignment().getSequenceAt(i);
187 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
194 value = av.getAlignment().getSequenceAt(i).findPosition(index);
201 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
203 g.drawString(value + "", x, (ypos + (i * charHeight))
221 void drawEastScale(Graphics g, int startx, int endx, int ypos)
225 if (av.hasHiddenColumns())
227 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
232 for (int i = 0; i < av.getAlignment().getHeight(); i++)
234 seq = av.getAlignment().getSequenceAt(i);
238 while (index > startx)
240 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
247 value = seq.findPosition(index);
254 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
260 boolean fastpainting = false;
263 * need to make this thread safe move alignment rendering in response to
269 * shift up or down in repaint
271 public void fastPaint(int horizontal, int vertical)
273 if (fastpainting || gg == null)
280 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
281 imgHeight, -horizontal * charWidth, -vertical * charHeight);
283 ViewportRanges ranges = av.getRanges();
284 int sr = ranges.getStartRes();
285 int er = ranges.getEndRes();
286 int ss = ranges.getStartSeq();
287 int es = ranges.getEndSeq();
291 if (horizontal > 0) // scrollbar pulled right, image to the left
294 transX = (er - sr - horizontal) * charWidth;
295 sr = er - horizontal;
297 else if (horizontal < 0)
299 er = sr - horizontal - 1;
301 else if (vertical > 0) // scroll down
305 if (ss < ranges.getStartSeq())
306 { // ie scrolling too fast, more than a page at a time
307 ss = ranges.getStartSeq();
311 transY = imgHeight - ((vertical + 1) * charHeight);
314 else if (vertical < 0)
318 if (es > ranges.getEndSeq())
320 es = ranges.getEndSeq();
324 gg.translate(transX, transY);
325 drawPanel(gg, sr, er, ss, es, 0);
326 gg.translate(-transX, -transY);
329 fastpainting = false;
333 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
334 * towards! startx is the first residue (starting at 0) to display. endx is
335 * the last residue to display (starting at 0). starty is the first sequence
336 * to display (starting at 0). endy is the last sequence to display (starting
337 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
338 * adjustment listener in SeqPanel when the scrollbars move.
341 // Set this to false to force a full panel paint
343 public void paintComponent(Graphics g)
346 BufferedImage lcimg = img; // take reference since other threads may null
347 // img and call later.
348 super.paintComponent(g);
352 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
353 .getClipBounds().height)))
355 g.drawImage(lcimg, 0, 0, this);
360 // this draws the whole of the alignment
361 imgWidth = getWidth();
362 imgHeight = getHeight();
364 imgWidth -= (imgWidth % charWidth);
365 imgHeight -= (imgHeight % charHeight);
367 if ((imgWidth < 1) || (imgHeight < 1))
372 if (lcimg == null || imgWidth != lcimg.getWidth()
373 || imgHeight != lcimg.getHeight())
377 lcimg = img = new BufferedImage(imgWidth, imgHeight,
378 BufferedImage.TYPE_INT_RGB);
379 gg = (Graphics2D) img.getGraphics();
380 gg.setFont(av.getFont());
381 } catch (OutOfMemoryError er)
384 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
385 new OOMWarning("Creating alignment image for display", er);
393 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
394 RenderingHints.VALUE_ANTIALIAS_ON);
397 gg.setColor(Color.white);
398 gg.fillRect(0, 0, imgWidth, imgHeight);
400 ViewportRanges ranges = av.getRanges();
401 if (av.getWrapAlignment())
403 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
407 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
408 ranges.getStartSeq(), ranges.getEndSeq(), 0);
411 g.drawImage(lcimg, 0, 0, this);
421 * @return DOCUMENT ME!
423 public int getWrappedCanvasWidth(int cwidth)
425 FontMetrics fm = getFontMetrics(av.getFont());
430 if (av.getScaleRightWrapped())
432 LABEL_EAST = fm.stringWidth(getMask());
435 if (av.getScaleLeftWrapped())
437 LABEL_WEST = fm.stringWidth(getMask());
440 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
444 * Generates a string of zeroes.
453 for (int i = 0; i < av.getAlignment().getHeight(); i++)
455 tmp = av.getAlignment().getSequenceAt(i).getEnd();
462 for (int i = maxWidth; i > 0; i /= 10)
476 * @param canvasHeight
481 public void drawWrappedPanel(Graphics g, int canvasWidth,
482 int canvasHeight, int startRes)
485 AlignmentI al = av.getAlignment();
487 FontMetrics fm = getFontMetrics(av.getFont());
489 if (av.getScaleRightWrapped())
491 LABEL_EAST = fm.stringWidth(getMask());
494 if (av.getScaleLeftWrapped())
496 LABEL_WEST = fm.stringWidth(getMask());
499 int hgap = charHeight;
500 if (av.getScaleAboveWrapped())
505 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
506 int cHeight = av.getAlignment().getHeight() * charHeight;
508 av.setWrappedWidth(cWidth);
510 av.getRanges().setEndRes(av.getRanges().getStartRes() + cWidth);
514 int maxwidth = av.getAlignment().getWidth() - 1;
516 if (av.hasHiddenColumns())
518 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
521 while ((ypos <= canvasHeight) && (startRes < maxwidth))
523 endx = startRes + cWidth - 1;
530 g.setFont(av.getFont());
531 g.setColor(Color.black);
533 if (av.getScaleLeftWrapped())
535 drawWestScale(g, startRes, endx, ypos);
538 if (av.getScaleRightWrapped())
540 g.translate(canvasWidth - LABEL_EAST, 0);
541 drawEastScale(g, startRes, endx, ypos);
542 g.translate(-(canvasWidth - LABEL_EAST), 0);
545 g.translate(LABEL_WEST, 0);
547 if (av.getScaleAboveWrapped())
549 drawNorthScale(g, startRes, endx, ypos);
552 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
554 g.setColor(Color.blue);
556 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
559 res = av.getColumnSelection().findHiddenRegionPosition(i)
562 if (res < 0 || res > endx - startRes)
568 new int[] { res * charWidth - charHeight / 4,
569 res * charWidth + charHeight / 4, res * charWidth },
570 new int[] { ypos - (charHeight / 2),
571 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
577 // When printing we have an extra clipped region,
578 // the Printable page which we need to account for here
579 Shape clip = g.getClip();
583 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
587 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
588 (int) clip.getBounds().getHeight());
591 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
593 if (av.isShowAnnotation())
595 g.translate(0, cHeight + ypos + 3);
596 if (annotations == null)
598 annotations = new AnnotationPanel(av);
601 annotations.renderer.drawComponent(annotations, av, g, -1,
603 g.translate(0, -cHeight - ypos - 3);
606 g.translate(-LABEL_WEST, 0);
608 ypos += cHeight + getAnnotationHeight() + hgap;
614 AnnotationPanel annotations;
616 int getAnnotationHeight()
618 if (!av.isShowAnnotation())
623 if (annotations == null)
625 annotations = new AnnotationPanel(av);
628 return annotations.adjustPanelHeight();
647 public void drawPanel(Graphics g1, int startRes, int endRes,
648 int startSeq, int endSeq, int offset)
651 if (!av.hasHiddenColumns())
653 draw(g1, startRes, endRes, startSeq, endSeq, offset);
657 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
660 int blockStart = startRes;
661 int blockEnd = endRes;
663 for (int[] region : regions)
665 int hideStart = region[0];
666 int hideEnd = region[1];
668 if (hideStart <= blockStart)
670 blockStart += (hideEnd - hideStart) + 1;
674 blockEnd = hideStart - 1;
676 g1.translate(screenY * charWidth, 0);
678 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
680 if (av.getShowHiddenMarkers())
682 g1.setColor(Color.blue);
684 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
685 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
686 (endSeq - startSeq + 1) * charHeight + offset);
689 g1.translate(-screenY * charWidth, 0);
690 screenY += blockEnd - blockStart + 1;
691 blockStart = hideEnd + 1;
693 if (screenY > (endRes - startRes))
695 // already rendered last block
700 if (screenY <= (endRes - startRes))
702 // remaining visible region to render
703 blockEnd = blockStart + (endRes - startRes) - screenY;
704 g1.translate(screenY * charWidth, 0);
705 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
707 g1.translate(-screenY * charWidth, 0);
713 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
714 // int x1, int x2, int y1, int y2, int startx, int starty,
715 private void draw(Graphics g, int startRes, int endRes, int startSeq,
716 int endSeq, int offset)
718 g.setFont(av.getFont());
719 sr.prepare(g, av.isRenderGaps());
723 // / First draw the sequences
724 // ///////////////////////////
725 for (int i = startSeq; i <= endSeq; i++)
727 nextSeq = av.getAlignment().getSequenceAt(i);
730 // occasionally, a race condition occurs such that the alignment row is
734 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
735 startRes, endRes, offset + ((i - startSeq) * charHeight));
737 if (av.isShowSequenceFeatures())
739 fr.drawSequence(g, nextSeq, startRes, endRes, offset
740 + ((i - startSeq) * charHeight), false);
743 // / Highlight search Results once all sequences have been drawn
744 // ////////////////////////////////////////////////////////
745 if (av.hasSearchResults())
747 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
749 if (visibleResults != null)
751 for (int r = 0; r < visibleResults.length; r += 2)
753 sr.drawHighlightedText(nextSeq, visibleResults[r],
754 visibleResults[r + 1], (visibleResults[r] - startRes)
756 + ((i - startSeq) * charHeight));
761 if (av.cursorMode && cursorY == i && cursorX >= startRes
762 && cursorX <= endRes)
764 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
765 offset + ((i - startSeq) * charHeight));
769 if (av.getSelectionGroup() != null
770 || av.getAlignment().getGroups().size() > 0)
772 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
777 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
778 int startSeq, int endSeq, int offset)
780 Graphics2D g = (Graphics2D) g1;
782 // ///////////////////////////////////
783 // Now outline any areas if necessary
784 // ///////////////////////////////////
785 SequenceGroup group = av.getSelectionGroup();
791 int visWidth = (endRes - startRes + 1) * charWidth;
793 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
795 group = av.getAlignment().getGroups().get(0);
805 boolean inGroup = false;
809 for (i = startSeq; i <= endSeq; i++)
811 sx = (group.getStartRes() - startRes) * charWidth;
812 sy = offset + ((i - startSeq) * charHeight);
813 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
815 if (sx + ex < 0 || sx > visWidth)
820 if ((sx <= (endRes - startRes) * charWidth)
821 && group.getSequences(null).contains(
822 av.getAlignment().getSequenceAt(i)))
825 && !group.getSequences(null).contains(
826 av.getAlignment().getSequenceAt(i + 1)))
828 bottom = sy + charHeight;
833 if (((top == -1) && (i == 0))
834 || !group.getSequences(null).contains(
835 av.getAlignment().getSequenceAt(i - 1)))
843 if (group == av.getSelectionGroup())
845 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
846 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
848 g.setColor(Color.RED);
852 g.setStroke(new BasicStroke());
853 g.setColor(group.getOutlineColour());
861 if (sx >= 0 && sx < visWidth)
863 g.drawLine(sx, oldY, sx, sy);
866 if (sx + ex < visWidth)
868 g.drawLine(sx + ex, oldY, sx + ex, sy);
877 if (sx + ex > visWidth)
882 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
884 ex = (endRes - startRes + 1) * charWidth;
889 g.drawLine(sx, top, sx + ex, top);
895 g.drawLine(sx, bottom, sx + ex, bottom);
906 sy = offset + ((i - startSeq) * charHeight);
907 if (sx >= 0 && sx < visWidth)
909 g.drawLine(sx, oldY, sx, sy);
912 if (sx + ex < visWidth)
914 g.drawLine(sx + ex, oldY, sx + ex, sy);
923 if (sx + ex > visWidth)
927 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
929 ex = (endRes - startRes + 1) * charWidth;
934 g.drawLine(sx, top, sx + ex, top);
940 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
949 g.setStroke(new BasicStroke());
951 if (groupIndex >= av.getAlignment().getGroups().size())
956 group = av.getAlignment().getGroups().get(groupIndex);
958 } while (groupIndex < av.getAlignment().getGroups().size());
970 public void highlightSearchResults(SearchResultsI results)
974 av.setSearchResults(results);