2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import java.awt.image.*;
25 import jalview.datamodel.*;
33 public class SeqCanvas extends JComponent
35 final FeatureRenderer fr;
37 final SequenceRenderer sr;
49 SearchResults searchResults = null;
51 boolean fastPaint = false;
62 * Creates a new SeqCanvas object.
67 public SeqCanvas(AlignmentPanel ap)
70 fr = new FeatureRenderer(ap);
71 sr = new SequenceRenderer(av);
72 setLayout(new BorderLayout());
73 PaintRefresher.Register(this, av.getSequenceSetId());
74 setBackground(Color.white);
77 public SequenceRenderer getSequenceRenderer()
82 public FeatureRenderer getFeatureRenderer()
99 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
101 int scalestartx = startx - (startx % 10) + 10;
103 g.setColor(Color.black);
106 for (int i = scalestartx; i < endx; i += 10)
109 if (av.hasHiddenColumns)
111 value = av.getColumnSelection().adjustForHiddenColumns(value);
114 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
115 ypos - (av.charHeight / 2));
117 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
118 (ypos + 2) - (av.charHeight / 2),
119 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
136 void drawWestScale(Graphics g, int startx, int endx, int ypos)
138 FontMetrics fm = getFontMetrics(av.getFont());
139 ypos += av.charHeight;
141 if (av.hasHiddenColumns)
143 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
144 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
147 int maxwidth = av.alignment.getWidth();
148 if (av.hasHiddenColumns)
150 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
154 for (int i = 0; i < av.alignment.getHeight(); i++)
156 SequenceI seq = av.alignment.getSequenceAt(i);
162 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
169 value = av.alignment.getSequenceAt(i).findPosition(index);
176 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
178 g.drawString(value + "", x, (ypos + (i * av.charHeight))
179 - (av.charHeight / 5));
196 void drawEastScale(Graphics g, int startx, int endx, int ypos)
198 ypos += av.charHeight;
200 if (av.hasHiddenColumns)
202 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
207 for (int i = 0; i < av.alignment.getHeight(); i++)
209 seq = av.alignment.getSequenceAt(i);
213 while (index > startx)
215 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
222 value = seq.findPosition(index);
229 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
230 - (av.charHeight / 5));
235 boolean fastpainting = false;
238 * need to make this thread safe move alignment rendering in response to
244 * shift up or down in repaint
246 public void fastPaint(int horizontal, int vertical)
260 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
261 imgWidth, imgHeight, -horizontal * av.charWidth, -vertical
264 int sr = av.startRes;
266 int ss = av.startSeq;
271 if (horizontal > 0) // scrollbar pulled right, image to the left
274 transX = (er - sr - horizontal) * av.charWidth;
275 sr = er - horizontal;
277 else if (horizontal < 0)
279 er = sr - horizontal - 1;
281 else if (vertical > 0) // scroll down
285 if (ss < av.startSeq)
286 { // ie scrolling too fast, more than a page at a time
291 transY = imgHeight - (vertical * av.charHeight);
294 else if (vertical < 0)
304 gg.translate(transX, transY);
305 drawPanel(gg, sr, er, ss, es, 0);
306 gg.translate(-transX, -transY);
309 fastpainting = false;
313 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
314 * towards! startx is the first residue (starting at 0) to display. endx is
315 * the last residue to display (starting at 0). starty is the first sequence
316 * to display (starting at 0). endy is the last sequence to display (starting
317 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
318 * adjustment listener in SeqPanel when the scrollbars move.
321 // Set this to false to force a full panel paint
322 public void paintComponent(Graphics g)
324 BufferedImage lcimg = img; // take reference since other threads may null
325 // img and call later.
326 super.paintComponent(g);
330 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
331 .getClipBounds().height)))
333 g.drawImage(lcimg, 0, 0, this);
338 // this draws the whole of the alignment
339 imgWidth = getWidth();
340 imgHeight = getHeight();
342 imgWidth -= (imgWidth % av.charWidth);
343 imgHeight -= (imgHeight % av.charHeight);
345 if ((imgWidth < 1) || (imgHeight < 1))
350 if (lcimg == null || imgWidth != lcimg.getWidth()
351 || imgHeight != lcimg.getHeight())
355 lcimg = img = new BufferedImage(imgWidth, imgHeight,
356 BufferedImage.TYPE_INT_RGB);
357 gg = (Graphics2D) img.getGraphics();
358 gg.setFont(av.getFont());
359 } catch (OutOfMemoryError er)
362 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
363 new OOMWarning("Creating alignment image for display", er);
371 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
372 RenderingHints.VALUE_ANTIALIAS_ON);
375 gg.setColor(Color.white);
376 gg.fillRect(0, 0, imgWidth, imgHeight);
378 if (av.getWrapAlignment())
380 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
384 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
387 g.drawImage(lcimg, 0, 0, this);
397 * @return DOCUMENT ME!
399 public int getWrappedCanvasWidth(int cwidth)
401 FontMetrics fm = getFontMetrics(av.getFont());
406 if (av.scaleRightWrapped)
408 LABEL_EAST = fm.stringWidth(getMask());
411 if (av.scaleLeftWrapped)
413 LABEL_WEST = fm.stringWidth(getMask());
416 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
420 * Generates a string of zeroes.
429 for (int i = 0; i < av.alignment.getHeight(); i++)
431 tmp = av.alignment.getSequenceAt(i).getEnd();
438 for (int i = maxWidth; i > 0; i /= 10)
452 * @param canvasHeight
457 public void drawWrappedPanel(Graphics g, int canvasWidth,
458 int canvasHeight, int startRes)
460 AlignmentI al = av.getAlignment();
462 FontMetrics fm = getFontMetrics(av.getFont());
464 if (av.scaleRightWrapped)
466 LABEL_EAST = fm.stringWidth(getMask());
469 if (av.scaleLeftWrapped)
471 LABEL_WEST = fm.stringWidth(getMask());
474 int hgap = av.charHeight;
475 if (av.scaleAboveWrapped)
477 hgap += av.charHeight;
480 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
481 int cHeight = av.getAlignment().getHeight() * av.charHeight;
483 av.setWrappedWidth(cWidth);
485 av.endRes = av.startRes + cWidth;
489 int maxwidth = av.alignment.getWidth() - 1;
491 if (av.hasHiddenColumns)
493 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
496 while ((ypos <= canvasHeight) && (startRes < maxwidth))
498 endx = startRes + cWidth - 1;
505 g.setFont(av.getFont());
506 g.setColor(Color.black);
508 if (av.scaleLeftWrapped)
510 drawWestScale(g, startRes, endx, ypos);
513 if (av.scaleRightWrapped)
515 g.translate(canvasWidth - LABEL_EAST, 0);
516 drawEastScale(g, startRes, endx, ypos);
517 g.translate(-(canvasWidth - LABEL_EAST), 0);
520 g.translate(LABEL_WEST, 0);
522 if (av.scaleAboveWrapped)
524 drawNorthScale(g, startRes, endx, ypos);
527 if (av.hasHiddenColumns && av.showHiddenMarkers)
529 g.setColor(Color.blue);
531 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
534 res = av.getColumnSelection().findHiddenRegionPosition(i)
537 if (res < 0 || res > endx - startRes)
542 gg.fillPolygon(new int[]
543 { res * av.charWidth - av.charHeight / 4,
544 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
546 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
547 ypos - (av.charHeight / 2) + 8 }, 3);
552 // When printing we have an extra clipped region,
553 // the Printable page which we need to account for here
554 Shape clip = g.getClip();
558 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
562 g.setClip(0, (int) clip.getBounds().getY(), cWidth * av.charWidth,
563 (int) clip.getBounds().getHeight());
566 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
568 if (av.showAnnotation)
570 g.translate(0, cHeight + ypos + 3);
571 if (annotations == null)
573 annotations = new AnnotationPanel(av);
576 annotations.drawComponent((Graphics2D) g, 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; 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);
659 .drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
660 0 + offset, (blockEnd - blockStart + 1)
661 * av.charWidth - 1, (endSeq - startSeq)
662 * av.charHeight + offset);
665 g1.translate(-screenY * av.charWidth, 0);
666 screenY += blockEnd - blockStart + 1;
667 blockStart = hideEnd + 1;
670 if (screenY <= (endRes - startRes))
672 blockEnd = blockStart + (endRes - startRes) - screenY;
673 g1.translate(screenY * av.charWidth, 0);
674 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
676 g1.translate(-screenY * av.charWidth, 0);
682 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
683 // int x1, int x2, int y1, int y2, int startx, int starty,
684 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
687 g.setFont(av.getFont());
688 sr.prepare(g, av.renderGaps);
692 // / First draw the sequences
693 // ///////////////////////////
694 for (int i = startSeq; i < endSeq; i++)
696 nextSeq = av.alignment.getSequenceAt(i);
698 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
699 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
701 if (av.showSequenceFeatures)
703 fr.drawSequence(g, nextSeq, startRes, endRes, offset
704 + ((i - startSeq) * av.charHeight));
707 // / Highlight search Results once all sequences have been drawn
708 // ////////////////////////////////////////////////////////
709 if (searchResults != null)
711 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
713 if (visibleResults != null)
715 for (int r = 0; r < visibleResults.length; r += 2)
717 sr.drawHighlightedText(nextSeq, visibleResults[r],
718 visibleResults[r + 1], (visibleResults[r] - startRes)
719 * av.charWidth, offset
720 + ((i - startSeq) * av.charHeight));
725 if (av.cursorMode && cursorY == i && cursorX >= startRes
726 && cursorX <= endRes)
728 sr.drawCursor(nextSeq, cursorX,
729 (cursorX - startRes) * av.charWidth, offset
730 + ((i - startSeq) * av.charHeight));
734 if (av.getSelectionGroup() != null
735 || av.alignment.getGroups().size() > 0)
737 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
742 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
743 int startSeq, int endSeq, int offset)
745 Graphics2D g = (Graphics2D) g1;
747 // ///////////////////////////////////
748 // Now outline any areas if necessary
749 // ///////////////////////////////////
750 SequenceGroup group = av.getSelectionGroup();
756 int visWidth = (endRes - startRes + 1) * av.charWidth;
758 if ((group == null) && (av.alignment.getGroups().size() > 0))
760 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
770 boolean inGroup = false;
774 for (i = startSeq; i < endSeq; i++)
776 sx = (group.getStartRes() - startRes) * av.charWidth;
777 sy = offset + ((i - startSeq) * av.charHeight);
778 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
780 if (sx + ex < 0 || sx > visWidth)
785 if ((sx <= (endRes - startRes) * av.charWidth)
786 && group.getSequences(null).contains(
787 av.alignment.getSequenceAt(i)))
790 && !group.getSequences(null).contains(
791 av.alignment.getSequenceAt(i + 1)))
793 bottom = sy + av.charHeight;
798 if (((top == -1) && (i == 0))
799 || !group.getSequences(null).contains(
800 av.alignment.getSequenceAt(i - 1)))
808 if (group == av.getSelectionGroup())
810 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
811 BasicStroke.JOIN_ROUND, 3f, new float[]
813 g.setColor(Color.RED);
817 g.setStroke(new BasicStroke());
818 g.setColor(group.getOutlineColour());
826 if (sx >= 0 && sx < visWidth)
828 g.drawLine(sx, oldY, sx, sy);
831 if (sx + ex < visWidth)
833 g.drawLine(sx + ex, oldY, sx + ex, sy);
842 if (sx + ex > visWidth)
847 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
849 ex = (endRes - startRes + 1) * av.charWidth;
854 g.drawLine(sx, top, sx + ex, top);
860 g.drawLine(sx, bottom, sx + ex, bottom);
871 sy = offset + ((i - startSeq) * av.charHeight);
872 if (sx >= 0 && sx < visWidth)
874 g.drawLine(sx, oldY, sx, sy);
877 if (sx + ex < visWidth)
879 g.drawLine(sx + ex, oldY, sx + ex, sy);
888 if (sx + ex > visWidth)
892 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
894 ex = (endRes - startRes + 1) * av.charWidth;
899 g.drawLine(sx, top, sx + ex, top);
905 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
914 g.setStroke(new BasicStroke());
916 if (groupIndex >= av.alignment.getGroups().size())
921 group = (SequenceGroup) av.alignment.getGroups().elementAt(
924 } while (groupIndex < av.alignment.getGroups().size());
936 public void highlightSearchResults(SearchResults results)
940 searchResults = results;