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;
30 import java.awt.BasicStroke;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.RenderingHints;
37 import java.awt.Shape;
38 import java.awt.image.BufferedImage;
39 import java.util.List;
41 import javax.swing.JComponent;
49 public class SeqCanvas extends JComponent
51 final FeatureRenderer fr;
53 final SequenceRenderer sr;
65 boolean fastPaint = false;
76 * Creates a new SeqCanvas object.
81 public SeqCanvas(AlignmentPanel ap)
85 fr = new FeatureRenderer(ap);
86 sr = new SequenceRenderer(av);
87 setLayout(new BorderLayout());
88 PaintRefresher.Register(this, av.getSequenceSetId());
89 setBackground(Color.white);
92 public SequenceRenderer getSequenceRenderer()
97 public FeatureRenderer getFeatureRenderer()
102 int charHeight = 0, charWidth = 0;
104 private void updateViewport()
106 charHeight = av.getCharHeight();
107 charWidth = av.getCharWidth();
122 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
125 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
128 int mpos = mark.column; // (i - startx - 1)
133 String mstring = mark.text;
139 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
141 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
142 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
160 void drawWestScale(Graphics g, int startx, int endx, int ypos)
162 FontMetrics fm = getFontMetrics(av.getFont());
165 if (av.hasHiddenColumns())
167 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
168 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
171 int maxwidth = av.getAlignment().getWidth();
172 if (av.hasHiddenColumns())
174 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
178 for (int i = 0; i < av.getAlignment().getHeight(); i++)
180 SequenceI seq = av.getAlignment().getSequenceAt(i);
186 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
193 value = av.getAlignment().getSequenceAt(i).findPosition(index);
200 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
202 g.drawString(value + "", x, (ypos + (i * charHeight))
220 void drawEastScale(Graphics g, int startx, int endx, int ypos)
224 if (av.hasHiddenColumns())
226 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
231 for (int i = 0; i < av.getAlignment().getHeight(); i++)
233 seq = av.getAlignment().getSequenceAt(i);
237 while (index > startx)
239 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
246 value = seq.findPosition(index);
253 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
259 boolean fastpainting = false;
262 * need to make this thread safe move alignment rendering in response to
268 * shift up or down in repaint
270 public void fastPaint(int horizontal, int vertical)
272 if (fastpainting || gg == null)
279 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
280 imgHeight, -horizontal * charWidth, -vertical * charHeight);
282 int sr = av.startRes;
284 int ss = av.startSeq;
289 if (horizontal > 0) // scrollbar pulled right, image to the left
292 transX = (er - sr - horizontal) * charWidth;
293 sr = er - horizontal;
295 else if (horizontal < 0)
297 er = sr - horizontal - 1;
299 else if (vertical > 0) // scroll down
303 if (ss < av.startSeq)
304 { // ie scrolling too fast, more than a page at a time
309 transY = imgHeight - (vertical * charHeight);
312 else if (vertical < 0)
322 gg.translate(transX, transY);
323 drawPanel(gg, sr, er, ss, es, 0);
324 gg.translate(-transX, -transY);
327 fastpainting = false;
331 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
332 * towards! startx is the first residue (starting at 0) to display. endx is
333 * the last residue to display (starting at 0). starty is the first sequence
334 * to display (starting at 0). endy is the last sequence to display (starting
335 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
336 * adjustment listener in SeqPanel when the scrollbars move.
339 // Set this to false to force a full panel paint
341 public void paintComponent(Graphics g)
344 BufferedImage lcimg = img; // take reference since other threads may null
345 // img and call later.
346 super.paintComponent(g);
350 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
351 .getClipBounds().height)))
353 g.drawImage(lcimg, 0, 0, this);
358 // this draws the whole of the alignment
359 imgWidth = getWidth();
360 imgHeight = getHeight();
362 imgWidth -= (imgWidth % charWidth);
363 imgHeight -= (imgHeight % charHeight);
365 if ((imgWidth < 1) || (imgHeight < 1))
370 if (lcimg == null || imgWidth != lcimg.getWidth()
371 || imgHeight != lcimg.getHeight())
375 lcimg = img = new BufferedImage(imgWidth, imgHeight,
376 BufferedImage.TYPE_INT_RGB);
377 gg = (Graphics2D) img.getGraphics();
378 gg.setFont(av.getFont());
379 } catch (OutOfMemoryError er)
382 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
383 new OOMWarning("Creating alignment image for display", er);
391 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
392 RenderingHints.VALUE_ANTIALIAS_ON);
395 gg.setColor(Color.white);
396 gg.fillRect(0, 0, imgWidth, imgHeight);
398 if (av.getWrapAlignment())
400 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
404 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
407 g.drawImage(lcimg, 0, 0, this);
417 * @return DOCUMENT ME!
419 public int getWrappedCanvasWidth(int cwidth)
421 FontMetrics fm = getFontMetrics(av.getFont());
426 if (av.getScaleRightWrapped())
428 LABEL_EAST = fm.stringWidth(getMask());
431 if (av.getScaleLeftWrapped())
433 LABEL_WEST = fm.stringWidth(getMask());
436 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
440 * Generates a string of zeroes.
449 for (int i = 0; i < av.getAlignment().getHeight(); i++)
451 tmp = av.getAlignment().getSequenceAt(i).getEnd();
458 for (int i = maxWidth; i > 0; i /= 10)
472 * @param canvasHeight
477 public void drawWrappedPanel(Graphics g, int canvasWidth,
478 int canvasHeight, int startRes)
481 AlignmentI al = av.getAlignment();
483 FontMetrics fm = getFontMetrics(av.getFont());
485 if (av.getScaleRightWrapped())
487 LABEL_EAST = fm.stringWidth(getMask());
490 if (av.getScaleLeftWrapped())
492 LABEL_WEST = fm.stringWidth(getMask());
495 int hgap = charHeight;
496 if (av.getScaleAboveWrapped())
501 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
502 int cHeight = av.getAlignment().getHeight() * charHeight;
504 av.setWrappedWidth(cWidth);
506 av.endRes = av.startRes + cWidth;
510 int maxwidth = av.getAlignment().getWidth() - 1;
512 if (av.hasHiddenColumns())
514 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
517 while ((ypos <= canvasHeight) && (startRes < maxwidth))
519 endx = startRes + cWidth - 1;
526 g.setFont(av.getFont());
527 g.setColor(Color.black);
529 if (av.getScaleLeftWrapped())
531 drawWestScale(g, startRes, endx, ypos);
534 if (av.getScaleRightWrapped())
536 g.translate(canvasWidth - LABEL_EAST, 0);
537 drawEastScale(g, startRes, endx, ypos);
538 g.translate(-(canvasWidth - LABEL_EAST), 0);
541 g.translate(LABEL_WEST, 0);
543 if (av.getScaleAboveWrapped())
545 drawNorthScale(g, startRes, endx, ypos);
548 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
550 g.setColor(Color.blue);
552 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
555 res = av.getColumnSelection().findHiddenRegionPosition(i)
558 if (res < 0 || res > endx - startRes)
564 new int[] { res * charWidth - charHeight / 4,
565 res * charWidth + charHeight / 4, res * charWidth },
566 new int[] { ypos - (charHeight / 2),
567 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
573 // When printing we have an extra clipped region,
574 // the Printable page which we need to account for here
575 Shape clip = g.getClip();
579 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
583 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
584 (int) clip.getBounds().getHeight());
587 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
589 if (av.isShowAnnotation())
591 g.translate(0, cHeight + ypos + 3);
592 if (annotations == null)
594 annotations = new AnnotationPanel(av);
597 annotations.renderer.drawComponent(annotations, av, g, -1,
599 g.translate(0, -cHeight - ypos - 3);
602 g.translate(-LABEL_WEST, 0);
604 ypos += cHeight + getAnnotationHeight() + hgap;
610 AnnotationPanel annotations;
612 int getAnnotationHeight()
614 if (!av.isShowAnnotation())
619 if (annotations == null)
621 annotations = new AnnotationPanel(av);
624 return annotations.adjustPanelHeight();
643 public void drawPanel(Graphics g1, int startRes, int endRes,
644 int startSeq, int endSeq, int offset)
647 if (!av.hasHiddenColumns())
649 draw(g1, startRes, endRes, startSeq, endSeq, offset);
653 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
656 int blockStart = startRes;
657 int blockEnd = endRes;
659 for (int[] region : regions)
661 int hideStart = region[0];
662 int hideEnd = region[1];
664 if (hideStart <= blockStart)
666 blockStart += (hideEnd - hideStart) + 1;
670 blockEnd = hideStart - 1;
672 g1.translate(screenY * charWidth, 0);
674 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
676 if (av.getShowHiddenMarkers())
678 g1.setColor(Color.blue);
680 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
681 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
682 (endSeq - startSeq) * charHeight + offset);
685 g1.translate(-screenY * charWidth, 0);
686 screenY += blockEnd - blockStart + 1;
687 blockStart = hideEnd + 1;
689 if (screenY > (endRes - startRes))
691 // already rendered last block
696 if (screenY <= (endRes - startRes))
698 // remaining visible region to render
699 blockEnd = blockStart + (endRes - startRes) - screenY;
700 g1.translate(screenY * charWidth, 0);
701 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
703 g1.translate(-screenY * charWidth, 0);
709 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
710 // int x1, int x2, int y1, int y2, int startx, int starty,
711 private void draw(Graphics g, int startRes, int endRes, int startSeq,
712 int endSeq, int offset)
714 g.setFont(av.getFont());
715 sr.prepare(g, av.isRenderGaps());
719 // / First draw the sequences
720 // ///////////////////////////
721 for (int i = startSeq; i < endSeq; i++)
723 nextSeq = av.getAlignment().getSequenceAt(i);
726 // occasionally, a race condition occurs such that the alignment row is
730 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
731 startRes, endRes, offset + ((i - startSeq) * charHeight));
733 if (av.isShowSequenceFeatures())
735 fr.drawSequence(g, nextSeq, startRes, endRes, offset
736 + ((i - startSeq) * charHeight));
739 // / Highlight search Results once all sequences have been drawn
740 // ////////////////////////////////////////////////////////
741 if (av.hasSearchResults())
743 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
745 if (visibleResults != null)
747 for (int r = 0; r < visibleResults.length; r += 2)
749 sr.drawHighlightedText(nextSeq, visibleResults[r],
750 visibleResults[r + 1], (visibleResults[r] - startRes)
752 + ((i - startSeq) * charHeight));
757 if (av.cursorMode && cursorY == i && cursorX >= startRes
758 && cursorX <= endRes)
760 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
761 offset + ((i - startSeq) * charHeight));
765 if (av.getSelectionGroup() != null
766 || av.getAlignment().getGroups().size() > 0)
768 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
773 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
774 int startSeq, int endSeq, int offset)
776 Graphics2D g = (Graphics2D) g1;
778 // ///////////////////////////////////
779 // Now outline any areas if necessary
780 // ///////////////////////////////////
781 SequenceGroup group = av.getSelectionGroup();
787 int visWidth = (endRes - startRes + 1) * charWidth;
789 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
791 group = av.getAlignment().getGroups().get(0);
801 boolean inGroup = false;
805 for (i = startSeq; i < endSeq; i++)
807 sx = (group.getStartRes() - startRes) * charWidth;
808 sy = offset + ((i - startSeq) * charHeight);
809 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
811 if (sx + ex < 0 || sx > visWidth)
816 if ((sx <= (endRes - startRes) * charWidth)
817 && group.getSequences(null).contains(
818 av.getAlignment().getSequenceAt(i)))
821 && !group.getSequences(null).contains(
822 av.getAlignment().getSequenceAt(i + 1)))
824 bottom = sy + charHeight;
829 if (((top == -1) && (i == 0))
830 || !group.getSequences(null).contains(
831 av.getAlignment().getSequenceAt(i - 1)))
839 if (group == av.getSelectionGroup())
841 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
842 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
844 g.setColor(Color.RED);
848 g.setStroke(new BasicStroke());
849 g.setColor(group.getOutlineColour());
857 if (sx >= 0 && sx < visWidth)
859 g.drawLine(sx, oldY, sx, sy);
862 if (sx + ex < visWidth)
864 g.drawLine(sx + ex, oldY, sx + ex, sy);
873 if (sx + ex > visWidth)
878 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
880 ex = (endRes - startRes + 1) * charWidth;
885 g.drawLine(sx, top, sx + ex, top);
891 g.drawLine(sx, bottom, sx + ex, bottom);
902 sy = offset + ((i - startSeq) * charHeight);
903 if (sx >= 0 && sx < visWidth)
905 g.drawLine(sx, oldY, sx, sy);
908 if (sx + ex < visWidth)
910 g.drawLine(sx + ex, oldY, sx + ex, sy);
919 if (sx + ex > visWidth)
923 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
925 ex = (endRes - startRes + 1) * charWidth;
930 g.drawLine(sx, top, sx + ex, top);
936 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
945 g.setStroke(new BasicStroke());
947 if (groupIndex >= av.getAlignment().getGroups().size())
952 group = av.getAlignment().getGroups().get(groupIndex);
954 } while (groupIndex < av.getAlignment().getGroups().size());
966 public void highlightSearchResults(SearchResultsI results)
970 av.setSearchResults(results);