2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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, -1, startRes, endx + 1);
576 g.translate(0, -cHeight - ypos - 3);
579 g.translate(-LABEL_WEST, 0);
581 ypos += cHeight + getAnnotationHeight() + hgap;
587 AnnotationPanel annotations;
589 int getAnnotationHeight()
591 if (!av.showAnnotation)
596 if (annotations == null)
598 annotations = new AnnotationPanel(av);
601 return annotations.adjustPanelHeight();
620 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
621 int endSeq, int offset)
623 if (!av.hasHiddenColumns())
625 draw(g1, startRes, endRes, startSeq, endSeq, offset);
629 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
632 int blockStart = startRes;
633 int blockEnd = endRes;
635 for (int i = 0; regions != null && i < regions.size(); i++)
637 int[] region = (int[]) regions.elementAt(i);
638 int hideStart = region[0];
639 int hideEnd = region[1];
641 if (hideStart <= blockStart)
643 blockStart += (hideEnd - hideStart) + 1;
647 blockEnd = hideStart - 1;
649 g1.translate(screenY * av.charWidth, 0);
651 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
653 if (av.getShowHiddenMarkers())
655 g1.setColor(Color.blue);
657 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
658 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
659 - 1, (endSeq - startSeq) * av.charHeight + offset);
662 g1.translate(-screenY * av.charWidth, 0);
663 screenY += blockEnd - blockStart + 1;
664 blockStart = hideEnd + 1;
667 if (screenY <= (endRes - startRes))
669 blockEnd = blockStart + (endRes - startRes) - screenY;
670 g1.translate(screenY * av.charWidth, 0);
671 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
673 g1.translate(-screenY * av.charWidth, 0);
679 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
680 // int x1, int x2, int y1, int y2, int startx, int starty,
681 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
684 g.setFont(av.getFont());
685 sr.prepare(g, av.renderGaps);
689 // / First draw the sequences
690 // ///////////////////////////
691 for (int i = startSeq; i < endSeq; i++)
693 nextSeq = av.getAlignment().getSequenceAt(i);
696 // occasionally, a race condition occurs such that the alignment row is
700 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
701 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
703 if (av.showSequenceFeatures)
705 fr.drawSequence(g, nextSeq, startRes, endRes, offset
706 + ((i - startSeq) * av.charHeight));
709 // / Highlight search Results once all sequences have been drawn
710 // ////////////////////////////////////////////////////////
711 if (searchResults != null)
713 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
715 if (visibleResults != null)
717 for (int r = 0; r < visibleResults.length; r += 2)
719 sr.drawHighlightedText(nextSeq, visibleResults[r],
720 visibleResults[r + 1], (visibleResults[r] - startRes)
721 * av.charWidth, offset
722 + ((i - startSeq) * av.charHeight));
727 if (av.cursorMode && cursorY == i && cursorX >= startRes
728 && cursorX <= endRes)
730 sr.drawCursor(nextSeq, cursorX,
731 (cursorX - startRes) * av.charWidth, offset
732 + ((i - startSeq) * av.charHeight));
736 if (av.getSelectionGroup() != null
737 || av.getAlignment().getGroups().size() > 0)
739 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
744 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
745 int startSeq, int endSeq, int offset)
747 Graphics2D g = (Graphics2D) g1;
749 // ///////////////////////////////////
750 // Now outline any areas if necessary
751 // ///////////////////////////////////
752 SequenceGroup group = av.getSelectionGroup();
758 int visWidth = (endRes - startRes + 1) * av.charWidth;
760 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
762 group = (SequenceGroup) av.getAlignment().getGroups().elementAt(0);
772 boolean inGroup = false;
776 for (i = startSeq; i < endSeq; i++)
778 sx = (group.getStartRes() - startRes) * av.charWidth;
779 sy = offset + ((i - startSeq) * av.charHeight);
780 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
782 if (sx + ex < 0 || sx > visWidth)
787 if ((sx <= (endRes - startRes) * av.charWidth)
788 && group.getSequences(null).contains(
789 av.getAlignment().getSequenceAt(i)))
792 && !group.getSequences(null).contains(
793 av.getAlignment().getSequenceAt(i + 1)))
795 bottom = sy + av.charHeight;
800 if (((top == -1) && (i == 0))
801 || !group.getSequences(null).contains(
802 av.getAlignment().getSequenceAt(i - 1)))
810 if (group == av.getSelectionGroup())
812 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
813 BasicStroke.JOIN_ROUND, 3f, new float[]
815 g.setColor(Color.RED);
819 g.setStroke(new BasicStroke());
820 g.setColor(group.getOutlineColour());
828 if (sx >= 0 && sx < visWidth)
830 g.drawLine(sx, oldY, sx, sy);
833 if (sx + ex < visWidth)
835 g.drawLine(sx + ex, oldY, sx + ex, sy);
844 if (sx + ex > visWidth)
849 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
851 ex = (endRes - startRes + 1) * av.charWidth;
856 g.drawLine(sx, top, sx + ex, top);
862 g.drawLine(sx, bottom, sx + ex, bottom);
873 sy = offset + ((i - startSeq) * av.charHeight);
874 if (sx >= 0 && sx < visWidth)
876 g.drawLine(sx, oldY, sx, sy);
879 if (sx + ex < visWidth)
881 g.drawLine(sx + ex, oldY, sx + ex, sy);
890 if (sx + ex > visWidth)
894 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
896 ex = (endRes - startRes + 1) * av.charWidth;
901 g.drawLine(sx, top, sx + ex, top);
907 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
916 g.setStroke(new BasicStroke());
918 if (groupIndex >= av.getAlignment().getGroups().size())
923 group = (SequenceGroup) av.getAlignment().getGroups().elementAt(
926 } while (groupIndex < av.getAlignment().getGroups().size());
938 public void highlightSearchResults(SearchResults results)
942 searchResults = results;