2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.awt.image.*;
24 import jalview.datamodel.*;
32 public class SeqCanvas extends JComponent
34 final FeatureRenderer fr;
36 final SequenceRenderer sr;
48 SearchResults searchResults = null;
50 boolean fastPaint = false;
61 * Creates a new SeqCanvas object.
66 public SeqCanvas(AlignmentPanel ap)
69 fr = new FeatureRenderer(ap);
70 sr = new SequenceRenderer(av);
71 setLayout(new BorderLayout());
72 PaintRefresher.Register(this, av.getSequenceSetId());
73 setBackground(Color.white);
76 public SequenceRenderer getSequenceRenderer()
81 public FeatureRenderer getFeatureRenderer()
98 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
100 int scalestartx = startx - (startx % 10) + 10;
102 g.setColor(Color.black);
105 for (int i = scalestartx; i < endx; i += 10)
108 if (av.hasHiddenColumns())
110 value = av.getColumnSelection().adjustForHiddenColumns(value);
113 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
114 ypos - (av.charHeight / 2));
116 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
117 (ypos + 2) - (av.charHeight / 2),
118 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
135 void drawWestScale(Graphics g, int startx, int endx, int ypos)
137 FontMetrics fm = getFontMetrics(av.getFont());
138 ypos += av.charHeight;
140 if (av.hasHiddenColumns())
142 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
143 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
146 int maxwidth = av.getAlignment().getWidth();
147 if (av.hasHiddenColumns())
149 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
153 for (int i = 0; i < av.getAlignment().getHeight(); i++)
155 SequenceI seq = av.getAlignment().getSequenceAt(i);
161 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
168 value = av.getAlignment().getSequenceAt(i).findPosition(index);
175 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
177 g.drawString(value + "", x, (ypos + (i * av.charHeight))
178 - (av.charHeight / 5));
195 void drawEastScale(Graphics g, int startx, int endx, int ypos)
197 ypos += av.charHeight;
199 if (av.hasHiddenColumns())
201 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
206 for (int i = 0; i < av.getAlignment().getHeight(); i++)
208 seq = av.getAlignment().getSequenceAt(i);
212 while (index > startx)
214 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
221 value = seq.findPosition(index);
228 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
229 - (av.charHeight / 5));
234 boolean fastpainting = false;
237 * need to make this thread safe move alignment rendering in response to
243 * shift up or down in repaint
245 public void fastPaint(int horizontal, int vertical)
259 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
260 imgWidth, imgHeight, -horizontal * av.charWidth, -vertical
263 int sr = av.startRes;
265 int ss = av.startSeq;
270 if (horizontal > 0) // scrollbar pulled right, image to the left
273 transX = (er - sr - horizontal) * av.charWidth;
274 sr = er - horizontal;
276 else if (horizontal < 0)
278 er = sr - horizontal - 1;
280 else if (vertical > 0) // scroll down
284 if (ss < av.startSeq)
285 { // ie scrolling too fast, more than a page at a time
290 transY = imgHeight - (vertical * av.charHeight);
293 else if (vertical < 0)
303 gg.translate(transX, transY);
304 drawPanel(gg, sr, er, ss, es, 0);
305 gg.translate(-transX, -transY);
308 fastpainting = false;
312 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
313 * towards! startx is the first residue (starting at 0) to display. endx is
314 * the last residue to display (starting at 0). starty is the first sequence
315 * to display (starting at 0). endy is the last sequence to display (starting
316 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
317 * adjustment listener in SeqPanel when the scrollbars move.
320 // Set this to false to force a full panel paint
321 public void paintComponent(Graphics g)
323 BufferedImage lcimg = img; // take reference since other threads may null
324 // img and call later.
325 super.paintComponent(g);
329 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
330 .getClipBounds().height)))
332 g.drawImage(lcimg, 0, 0, this);
337 // this draws the whole of the alignment
338 imgWidth = getWidth();
339 imgHeight = getHeight();
341 imgWidth -= (imgWidth % av.charWidth);
342 imgHeight -= (imgHeight % av.charHeight);
344 if ((imgWidth < 1) || (imgHeight < 1))
349 if (lcimg == null || imgWidth != lcimg.getWidth()
350 || imgHeight != lcimg.getHeight())
354 lcimg = img = new BufferedImage(imgWidth, imgHeight,
355 BufferedImage.TYPE_INT_RGB);
356 gg = (Graphics2D) img.getGraphics();
357 gg.setFont(av.getFont());
358 } catch (OutOfMemoryError er)
361 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
362 new OOMWarning("Creating alignment image for display", er);
370 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
371 RenderingHints.VALUE_ANTIALIAS_ON);
374 gg.setColor(Color.white);
375 gg.fillRect(0, 0, imgWidth, imgHeight);
377 if (av.getWrapAlignment())
379 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
383 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
386 g.drawImage(lcimg, 0, 0, this);
396 * @return DOCUMENT ME!
398 public int getWrappedCanvasWidth(int cwidth)
400 FontMetrics fm = getFontMetrics(av.getFont());
405 if (av.scaleRightWrapped)
407 LABEL_EAST = fm.stringWidth(getMask());
410 if (av.scaleLeftWrapped)
412 LABEL_WEST = fm.stringWidth(getMask());
415 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
419 * Generates a string of zeroes.
428 for (int i = 0; i < av.getAlignment().getHeight(); i++)
430 tmp = av.getAlignment().getSequenceAt(i).getEnd();
437 for (int i = maxWidth; i > 0; i /= 10)
451 * @param canvasHeight
456 public void drawWrappedPanel(Graphics g, int canvasWidth,
457 int canvasHeight, int startRes)
459 AlignmentI al = av.getAlignment();
461 FontMetrics fm = getFontMetrics(av.getFont());
463 if (av.scaleRightWrapped)
465 LABEL_EAST = fm.stringWidth(getMask());
468 if (av.scaleLeftWrapped)
470 LABEL_WEST = fm.stringWidth(getMask());
473 int hgap = av.charHeight;
474 if (av.scaleAboveWrapped)
476 hgap += av.charHeight;
479 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
480 int cHeight = av.getAlignment().getHeight() * av.charHeight;
482 av.setWrappedWidth(cWidth);
484 av.endRes = av.startRes + cWidth;
488 int maxwidth = av.getAlignment().getWidth() - 1;
490 if (av.hasHiddenColumns())
492 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
495 while ((ypos <= canvasHeight) && (startRes < maxwidth))
497 endx = startRes + cWidth - 1;
504 g.setFont(av.getFont());
505 g.setColor(Color.black);
507 if (av.scaleLeftWrapped)
509 drawWestScale(g, startRes, endx, ypos);
512 if (av.scaleRightWrapped)
514 g.translate(canvasWidth - LABEL_EAST, 0);
515 drawEastScale(g, startRes, endx, ypos);
516 g.translate(-(canvasWidth - LABEL_EAST), 0);
519 g.translate(LABEL_WEST, 0);
521 if (av.scaleAboveWrapped)
523 drawNorthScale(g, startRes, endx, ypos);
526 if (av.hasHiddenColumns() && av.showHiddenMarkers)
528 g.setColor(Color.blue);
530 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
533 res = av.getColumnSelection().findHiddenRegionPosition(i)
536 if (res < 0 || res > endx - startRes)
541 gg.fillPolygon(new int[]
542 { res * av.charWidth - av.charHeight / 4,
543 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
545 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
546 ypos - (av.charHeight / 2) + 8 }, 3);
551 // When printing we have an extra clipped region,
552 // the Printable page which we need to account for here
553 Shape clip = g.getClip();
557 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
561 g.setClip(0, (int) clip.getBounds().getY(), cWidth * av.charWidth,
562 (int) clip.getBounds().getHeight());
565 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
567 if (av.showAnnotation)
569 g.translate(0, cHeight + ypos + 3);
570 if (annotations == null)
572 annotations = new AnnotationPanel(av);
575 annotations.renderer.drawComponent(annotations, av, (Graphics2D) g,
576 -1, startRes, endx + 1);
577 g.translate(0, -cHeight - ypos - 3);
580 g.translate(-LABEL_WEST, 0);
582 ypos += cHeight + getAnnotationHeight() + hgap;
588 AnnotationPanel annotations;
590 int getAnnotationHeight()
592 if (!av.showAnnotation)
597 if (annotations == null)
599 annotations = new AnnotationPanel(av);
602 return annotations.adjustPanelHeight();
621 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
622 int endSeq, int offset)
624 if (!av.hasHiddenColumns())
626 draw(g1, startRes, endRes, startSeq, endSeq, offset);
630 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
633 int blockStart = startRes;
634 int blockEnd = endRes;
636 for (int i = 0; regions != null && i < regions.size(); i++)
638 int[] region = (int[]) regions.elementAt(i);
639 int hideStart = region[0];
640 int hideEnd = region[1];
642 if (hideStart <= blockStart)
644 blockStart += (hideEnd - hideStart) + 1;
648 blockEnd = hideStart - 1;
650 g1.translate(screenY * av.charWidth, 0);
652 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
654 if (av.getShowHiddenMarkers())
656 g1.setColor(Color.blue);
658 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
659 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
660 - 1, (endSeq - startSeq) * av.charHeight + offset);
663 g1.translate(-screenY * av.charWidth, 0);
664 screenY += blockEnd - blockStart + 1;
665 blockStart = hideEnd + 1;
668 if (screenY <= (endRes - startRes))
670 blockEnd = blockStart + (endRes - startRes) - screenY;
671 g1.translate(screenY * av.charWidth, 0);
672 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
674 g1.translate(-screenY * av.charWidth, 0);
680 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
681 // int x1, int x2, int y1, int y2, int startx, int starty,
682 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
685 g.setFont(av.getFont());
686 sr.prepare(g, av.renderGaps);
690 // / First draw the sequences
691 // ///////////////////////////
692 for (int i = startSeq; i < endSeq; i++)
694 nextSeq = av.getAlignment().getSequenceAt(i);
697 // occasionally, a race condition occurs such that the alignment row is
701 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
702 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
704 if (av.showSequenceFeatures)
706 fr.drawSequence(g, nextSeq, startRes, endRes, offset
707 + ((i - startSeq) * av.charHeight));
710 // / Highlight search Results once all sequences have been drawn
711 // ////////////////////////////////////////////////////////
712 if (searchResults != null)
714 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
716 if (visibleResults != null)
718 for (int r = 0; r < visibleResults.length; r += 2)
720 sr.drawHighlightedText(nextSeq, visibleResults[r],
721 visibleResults[r + 1], (visibleResults[r] - startRes)
722 * av.charWidth, offset
723 + ((i - startSeq) * av.charHeight));
728 if (av.cursorMode && cursorY == i && cursorX >= startRes
729 && cursorX <= endRes)
731 sr.drawCursor(nextSeq, cursorX,
732 (cursorX - startRes) * av.charWidth, offset
733 + ((i - startSeq) * av.charHeight));
737 if (av.getSelectionGroup() != null
738 || av.getAlignment().getGroups().size() > 0)
740 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
745 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
746 int startSeq, int endSeq, int offset)
748 Graphics2D g = (Graphics2D) g1;
750 // ///////////////////////////////////
751 // Now outline any areas if necessary
752 // ///////////////////////////////////
753 SequenceGroup group = av.getSelectionGroup();
759 int visWidth = (endRes - startRes + 1) * av.charWidth;
761 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
763 group = (SequenceGroup) av.getAlignment().getGroups().get(0);
773 boolean inGroup = false;
777 for (i = startSeq; i < endSeq; i++)
779 sx = (group.getStartRes() - startRes) * av.charWidth;
780 sy = offset + ((i - startSeq) * av.charHeight);
781 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
783 if (sx + ex < 0 || sx > visWidth)
788 if ((sx <= (endRes - startRes) * av.charWidth)
789 && group.getSequences(null).contains(
790 av.getAlignment().getSequenceAt(i)))
793 && !group.getSequences(null).contains(
794 av.getAlignment().getSequenceAt(i + 1)))
796 bottom = sy + av.charHeight;
801 if (((top == -1) && (i == 0))
802 || !group.getSequences(null).contains(
803 av.getAlignment().getSequenceAt(i - 1)))
811 if (group == av.getSelectionGroup())
813 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
814 BasicStroke.JOIN_ROUND, 3f, new float[]
816 g.setColor(Color.RED);
820 g.setStroke(new BasicStroke());
821 g.setColor(group.getOutlineColour());
829 if (sx >= 0 && sx < visWidth)
831 g.drawLine(sx, oldY, sx, sy);
834 if (sx + ex < visWidth)
836 g.drawLine(sx + ex, oldY, sx + ex, sy);
845 if (sx + ex > visWidth)
850 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
852 ex = (endRes - startRes + 1) * av.charWidth;
857 g.drawLine(sx, top, sx + ex, top);
863 g.drawLine(sx, bottom, sx + ex, bottom);
874 sy = offset + ((i - startSeq) * av.charHeight);
875 if (sx >= 0 && sx < visWidth)
877 g.drawLine(sx, oldY, sx, sy);
880 if (sx + ex < visWidth)
882 g.drawLine(sx + ex, oldY, sx + ex, sy);
891 if (sx + ex > visWidth)
895 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
897 ex = (endRes - startRes + 1) * av.charWidth;
902 g.drawLine(sx, top, sx + ex, top);
908 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
917 g.setStroke(new BasicStroke());
919 if (groupIndex >= av.getAlignment().getGroups().size())
924 group = (SequenceGroup) av.getAlignment().getGroups()
927 } while (groupIndex < av.getAlignment().getGroups().size());
939 public void highlightSearchResults(SearchResults results)
943 searchResults = results;