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.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
29 import jalview.util.ColorUtils;
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 SearchResults searchResults = null;
68 boolean fastPaint = false;
79 * Creates a new SeqCanvas object.
84 public SeqCanvas(AlignmentPanel ap)
88 fr = new FeatureRenderer(ap);
89 sr = new SequenceRenderer(av);
90 setLayout(new BorderLayout());
91 PaintRefresher.Register(this, av.getSequenceSetId());
92 setBackground(Color.white);
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.getColumnSelection().adjustForHiddenColumns(startx);
171 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
174 int maxwidth = av.getAlignment().getWidth();
175 if (av.hasHiddenColumns())
177 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
181 for (int i = 0; i < av.getAlignment().getHeight(); i++)
183 SequenceI seq = av.getAlignment().getSequenceAt(i);
189 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
196 value = av.getAlignment().getSequenceAt(i).findPosition(index);
203 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
205 g.drawString(value + "", x, (ypos + (i * charHeight))
223 void drawEastScale(Graphics g, int startx, int endx, int ypos)
227 if (av.hasHiddenColumns())
229 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
234 for (int i = 0; i < av.getAlignment().getHeight(); i++)
236 seq = av.getAlignment().getSequenceAt(i);
240 while (index > startx)
242 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
249 value = seq.findPosition(index);
256 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
262 boolean fastpainting = false;
265 * need to make this thread safe move alignment rendering in response to
271 * shift up or down in repaint
273 public void fastPaint(int horizontal, int vertical)
275 if (fastpainting || gg == null)
282 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
283 imgHeight, -horizontal * charWidth, -vertical * charHeight);
285 int sr = av.startRes;
287 int ss = av.startSeq;
292 if (horizontal > 0) // scrollbar pulled right, image to the left
295 transX = (er - sr - horizontal) * charWidth;
296 sr = er - horizontal;
298 else if (horizontal < 0)
300 er = sr - horizontal - 1;
302 else if (vertical > 0) // scroll down
306 if (ss < av.startSeq)
307 { // ie scrolling too fast, more than a page at a time
312 transY = imgHeight - (vertical * charHeight);
315 else if (vertical < 0)
325 gg.translate(transX, transY);
326 drawPanel(gg, sr, er, ss, es, 0);
327 gg.translate(-transX, -transY);
330 fastpainting = false;
334 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
335 * towards! startx is the first residue (starting at 0) to display. endx is
336 * the last residue to display (starting at 0). starty is the first sequence
337 * to display (starting at 0). endy is the last sequence to display (starting
338 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
339 * adjustment listener in SeqPanel when the scrollbars move.
342 // Set this to false to force a full panel paint
344 public void paintComponent(Graphics g)
347 BufferedImage lcimg = img; // take reference since other threads may null
348 // img and call later.
349 super.paintComponent(g);
353 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
354 .getClipBounds().height)))
356 g.drawImage(lcimg, 0, 0, this);
361 // this draws the whole of the alignment
362 imgWidth = getWidth();
363 imgHeight = getHeight();
365 imgWidth -= (imgWidth % charWidth);
366 imgHeight -= (imgHeight % charHeight);
368 if ((imgWidth < 1) || (imgHeight < 1))
373 if (lcimg == null || imgWidth != lcimg.getWidth()
374 || imgHeight != lcimg.getHeight())
378 lcimg = img = new BufferedImage(imgWidth, imgHeight,
379 BufferedImage.TYPE_INT_RGB);
380 gg = (Graphics2D) img.getGraphics();
381 gg.setFont(av.getFont());
382 } catch (OutOfMemoryError er)
385 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
386 new OOMWarning("Creating alignment image for display", er);
394 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
395 RenderingHints.VALUE_ANTIALIAS_ON);
398 gg.setColor(Color.white);
399 gg.fillRect(0, 0, imgWidth, imgHeight);
401 if (av.getWrapAlignment())
403 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
407 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
410 g.drawImage(lcimg, 0, 0, this);
420 * @return DOCUMENT ME!
422 public int getWrappedCanvasWidth(int cwidth)
424 FontMetrics fm = getFontMetrics(av.getFont());
429 if (av.getScaleRightWrapped())
431 LABEL_EAST = fm.stringWidth(getMask());
434 if (av.getScaleLeftWrapped())
436 LABEL_WEST = fm.stringWidth(getMask());
439 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
443 * Generates a string of zeroes.
452 for (int i = 0; i < av.getAlignment().getHeight(); i++)
454 tmp = av.getAlignment().getSequenceAt(i).getEnd();
461 for (int i = maxWidth; i > 0; i /= 10)
475 * @param canvasHeight
480 public void drawWrappedPanel(Graphics g, int canvasWidth,
481 int canvasHeight, int startRes)
484 AlignmentI al = av.getAlignment();
486 FontMetrics fm = getFontMetrics(av.getFont());
488 if (av.getScaleRightWrapped())
490 LABEL_EAST = fm.stringWidth(getMask());
493 if (av.getScaleLeftWrapped())
495 LABEL_WEST = fm.stringWidth(getMask());
498 int hgap = charHeight;
499 if (av.getScaleAboveWrapped())
504 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
505 int cHeight = av.getAlignment().getHeight() * charHeight;
507 av.setWrappedWidth(cWidth);
509 av.endRes = av.startRes + cWidth;
513 int maxwidth = av.getAlignment().getWidth() - 1;
515 if (av.hasHiddenColumns())
517 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
520 while ((ypos <= canvasHeight) && (startRes < maxwidth))
522 endx = startRes + cWidth - 1;
529 g.setFont(av.getFont());
530 g.setColor(Color.black);
532 if (av.getScaleLeftWrapped())
534 drawWestScale(g, startRes, endx, ypos);
537 if (av.getScaleRightWrapped())
539 g.translate(canvasWidth - LABEL_EAST, 0);
540 drawEastScale(g, startRes, endx, ypos);
541 g.translate(-(canvasWidth - LABEL_EAST), 0);
544 g.translate(LABEL_WEST, 0);
546 if (av.getScaleAboveWrapped())
548 drawNorthScale(g, startRes, endx, ypos);
551 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
553 g.setColor(Color.blue);
555 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
558 res = av.getColumnSelection().findHiddenRegionPosition(i)
561 if (res < 0 || res > endx - startRes)
567 new int[] { res * charWidth - charHeight / 4,
568 res * charWidth + charHeight / 4, res * charWidth },
569 new int[] { ypos - (charHeight / 2),
570 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
576 // When printing we have an extra clipped region,
577 // the Printable page which we need to account for here
578 Shape clip = g.getClip();
582 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
586 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
587 (int) clip.getBounds().getHeight());
590 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
592 if (av.isShowAnnotation())
594 g.translate(0, cHeight + ypos + 3);
595 if (annotations == null)
597 annotations = new AnnotationPanel(av);
600 annotations.renderer.drawComponent(annotations, av, g, -1,
602 g.translate(0, -cHeight - ypos - 3);
605 g.translate(-LABEL_WEST, 0);
607 ypos += cHeight + getAnnotationHeight() + hgap;
613 AnnotationPanel annotations;
615 int getAnnotationHeight()
617 if (!av.isShowAnnotation())
622 if (annotations == null)
624 annotations = new AnnotationPanel(av);
627 return annotations.adjustPanelHeight();
646 public void drawPanel(Graphics g1, int startRes, int endRes,
647 int startSeq, int endSeq, int offset)
650 if (!av.hasHiddenColumns())
652 draw(g1, startRes, endRes, startSeq, endSeq, offset);
656 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
659 int blockStart = startRes;
660 int blockEnd = endRes;
662 for (int[] region : regions)
664 int hideStart = region[0];
665 int hideEnd = region[1];
667 if (hideStart <= blockStart)
669 blockStart += (hideEnd - hideStart) + 1;
673 blockEnd = hideStart - 1;
675 g1.translate(screenY * charWidth, 0);
677 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
679 if (av.getShowHiddenMarkers())
681 g1.setColor(Color.blue);
683 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
684 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
685 (endSeq - startSeq) * charHeight + offset);
688 g1.translate(-screenY * charWidth, 0);
689 screenY += blockEnd - blockStart + 1;
690 blockStart = hideEnd + 1;
692 if (screenY > (endRes - startRes))
694 // already rendered last block
699 if (screenY <= (endRes - startRes))
701 // remaining visible region to render
702 blockEnd = blockStart + (endRes - startRes) - screenY;
703 g1.translate(screenY * charWidth, 0);
704 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
706 g1.translate(-screenY * charWidth, 0);
712 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
713 // int x1, int x2, int y1, int y2, int startx, int starty,
714 private void draw(Graphics g, int startRes, int endRes, int startSeq,
715 int endSeq, int offset)
717 g.setFont(av.getFont());
718 sr.prepare(g, av.isRenderGaps());
722 // / First draw the sequences
723 // ///////////////////////////
724 for (int i = startSeq; i < endSeq; i++)
726 nextSeq = av.getAlignment().getSequenceAt(i);
729 // occasionally, a race condition occurs such that the alignment row is
733 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
734 startRes, endRes, offset + ((i - startSeq) * charHeight));
736 if (av.isShowSequenceFeatures())
738 fr.drawSequence(g, nextSeq, startRes, endRes, offset
739 + ((i - startSeq) * charHeight));
742 // / Highlight search Results once all sequences have been drawn
743 // ////////////////////////////////////////////////////////
744 if (searchResults != null)
746 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
748 if (visibleResults != null)
750 for (int r = 0; r < visibleResults.length; r += 2)
752 sr.drawHighlightedText(nextSeq, visibleResults[r],
753 visibleResults[r + 1], (visibleResults[r] - startRes)
755 + ((i - startSeq) * charHeight));
760 if (av.cursorMode && cursorY == i && cursorX >= startRes
761 && cursorX <= endRes)
763 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
764 offset + ((i - startSeq) * charHeight));
768 if (av.getSelectionGroup() != null
769 || av.getAlignment().getGroups().size() > 0)
771 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
776 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
777 int startSeq, int endSeq, int offset)
779 Graphics2D g = (Graphics2D) g1;
781 // ///////////////////////////////////
782 // Now outline any areas if necessary
783 // ///////////////////////////////////
784 SequenceGroup group = av.getSelectionGroup();
790 int visWidth = (endRes - startRes + 1) * charWidth;
792 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
794 group = av.getAlignment().getGroups().get(0);
804 boolean inGroup = false;
808 for (i = startSeq; i < endSeq; i++)
810 sx = (group.getStartRes() - startRes) * charWidth;
811 sy = offset + ((i - startSeq) * charHeight);
812 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
814 if (sx + ex < 0 || sx > visWidth)
819 if ((sx <= (endRes - startRes) * charWidth)
820 && group.getSequences(null).contains(
821 av.getAlignment().getSequenceAt(i)))
824 && !group.getSequences(null).contains(
825 av.getAlignment().getSequenceAt(i + 1)))
827 bottom = sy + charHeight;
832 if (((top == -1) && (i == 0))
833 || !group.getSequences(null).contains(
834 av.getAlignment().getSequenceAt(i - 1)))
842 if (group == av.getSelectionGroup())
844 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
845 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
847 g.setColor(Color.RED);
851 g.setStroke(new BasicStroke());
852 g.setColor(ColorUtils.getColor(group.getOutlineColour()));
860 if (sx >= 0 && sx < visWidth)
862 g.drawLine(sx, oldY, sx, sy);
865 if (sx + ex < visWidth)
867 g.drawLine(sx + ex, oldY, sx + ex, sy);
876 if (sx + ex > visWidth)
881 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
883 ex = (endRes - startRes + 1) * charWidth;
888 g.drawLine(sx, top, sx + ex, top);
894 g.drawLine(sx, bottom, sx + ex, bottom);
905 sy = offset + ((i - startSeq) * charHeight);
906 if (sx >= 0 && sx < visWidth)
908 g.drawLine(sx, oldY, sx, sy);
911 if (sx + ex < visWidth)
913 g.drawLine(sx + ex, oldY, sx + ex, sy);
922 if (sx + ex > visWidth)
926 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
928 ex = (endRes - startRes + 1) * charWidth;
933 g.drawLine(sx, top, sx + ex, top);
939 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
948 g.setStroke(new BasicStroke());
950 if (groupIndex >= av.getAlignment().getGroups().size())
955 group = av.getAlignment().getGroups().get(groupIndex);
957 } while (groupIndex < av.getAlignment().getGroups().size());
969 public void highlightSearchResults(SearchResults results)
973 searchResults = results;