2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
28 import java.awt.BasicStroke;
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.RenderingHints;
35 import java.awt.Shape;
36 import java.awt.image.BufferedImage;
38 import javax.swing.JComponent;
46 public class SeqCanvas extends JComponent
48 final FeatureRenderer fr;
50 final SequenceRenderer sr;
62 SearchResults searchResults = null;
64 boolean fastPaint = false;
75 * Creates a new SeqCanvas object.
80 public SeqCanvas(AlignmentPanel ap)
83 fr = new FeatureRenderer(ap);
84 sr = new SequenceRenderer(av);
85 setLayout(new BorderLayout());
86 PaintRefresher.Register(this, av.getSequenceSetId());
87 setBackground(Color.white);
90 public SequenceRenderer getSequenceRenderer()
95 public FeatureRenderer getFeatureRenderer()
112 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
114 int scalestartx = startx - (startx % 10) + 10;
116 g.setColor(Color.black);
119 for (int i = scalestartx; i < endx; i += 10)
122 if (av.hasHiddenColumns())
124 value = av.getColumnSelection().adjustForHiddenColumns(value);
127 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
128 ypos - (av.charHeight / 2));
130 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
131 (ypos + 2) - (av.charHeight / 2),
132 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
149 void drawWestScale(Graphics g, int startx, int endx, int ypos)
151 FontMetrics fm = getFontMetrics(av.getFont());
152 ypos += av.charHeight;
154 if (av.hasHiddenColumns())
156 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
157 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
160 int maxwidth = av.getAlignment().getWidth();
161 if (av.hasHiddenColumns())
163 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
167 for (int i = 0; i < av.getAlignment().getHeight(); i++)
169 SequenceI seq = av.getAlignment().getSequenceAt(i);
175 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
182 value = av.getAlignment().getSequenceAt(i).findPosition(index);
189 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
191 g.drawString(value + "", x, (ypos + (i * av.charHeight))
192 - (av.charHeight / 5));
209 void drawEastScale(Graphics g, int startx, int endx, int ypos)
211 ypos += av.charHeight;
213 if (av.hasHiddenColumns())
215 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
220 for (int i = 0; i < av.getAlignment().getHeight(); i++)
222 seq = av.getAlignment().getSequenceAt(i);
226 while (index > startx)
228 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
235 value = seq.findPosition(index);
242 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
243 - (av.charHeight / 5));
248 boolean fastpainting = false;
251 * need to make this thread safe move alignment rendering in response to
257 * shift up or down in repaint
259 public void fastPaint(int horizontal, int vertical)
273 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
274 imgWidth, imgHeight, -horizontal * av.charWidth, -vertical
277 int sr = av.startRes;
279 int ss = av.startSeq;
284 if (horizontal > 0) // scrollbar pulled right, image to the left
287 transX = (er - sr - horizontal) * av.charWidth;
288 sr = er - horizontal;
290 else if (horizontal < 0)
292 er = sr - horizontal - 1;
294 else if (vertical > 0) // scroll down
298 if (ss < av.startSeq)
299 { // ie scrolling too fast, more than a page at a time
304 transY = imgHeight - (vertical * av.charHeight);
307 else if (vertical < 0)
317 gg.translate(transX, transY);
318 drawPanel(gg, sr, er, ss, es, 0);
319 gg.translate(-transX, -transY);
322 fastpainting = false;
326 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
327 * towards! startx is the first residue (starting at 0) to display. endx is
328 * the last residue to display (starting at 0). starty is the first sequence
329 * to display (starting at 0). endy is the last sequence to display (starting
330 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
331 * adjustment listener in SeqPanel when the scrollbars move.
334 // Set this to false to force a full panel paint
335 public void paintComponent(Graphics g)
337 BufferedImage lcimg = img; // take reference since other threads may null
338 // img and call later.
339 super.paintComponent(g);
343 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
344 .getClipBounds().height)))
346 g.drawImage(lcimg, 0, 0, this);
351 // this draws the whole of the alignment
352 imgWidth = getWidth();
353 imgHeight = getHeight();
355 imgWidth -= (imgWidth % av.charWidth);
356 imgHeight -= (imgHeight % av.charHeight);
358 if ((imgWidth < 1) || (imgHeight < 1))
363 if (lcimg == null || imgWidth != lcimg.getWidth()
364 || imgHeight != lcimg.getHeight())
368 lcimg = img = new BufferedImage(imgWidth, imgHeight,
369 BufferedImage.TYPE_INT_RGB);
370 gg = (Graphics2D) img.getGraphics();
371 gg.setFont(av.getFont());
372 } catch (OutOfMemoryError er)
375 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
376 new OOMWarning("Creating alignment image for display", er);
384 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
385 RenderingHints.VALUE_ANTIALIAS_ON);
388 gg.setColor(Color.white);
389 gg.fillRect(0, 0, imgWidth, imgHeight);
391 if (av.getWrapAlignment())
393 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
397 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
400 g.drawImage(lcimg, 0, 0, this);
410 * @return DOCUMENT ME!
412 public int getWrappedCanvasWidth(int cwidth)
414 FontMetrics fm = getFontMetrics(av.getFont());
419 if (av.scaleRightWrapped)
421 LABEL_EAST = fm.stringWidth(getMask());
424 if (av.scaleLeftWrapped)
426 LABEL_WEST = fm.stringWidth(getMask());
429 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
433 * Generates a string of zeroes.
442 for (int i = 0; i < av.getAlignment().getHeight(); i++)
444 tmp = av.getAlignment().getSequenceAt(i).getEnd();
451 for (int i = maxWidth; i > 0; i /= 10)
465 * @param canvasHeight
470 public void drawWrappedPanel(Graphics g, int canvasWidth,
471 int canvasHeight, int startRes)
473 AlignmentI al = av.getAlignment();
475 FontMetrics fm = getFontMetrics(av.getFont());
477 if (av.scaleRightWrapped)
479 LABEL_EAST = fm.stringWidth(getMask());
482 if (av.scaleLeftWrapped)
484 LABEL_WEST = fm.stringWidth(getMask());
487 int hgap = av.charHeight;
488 if (av.scaleAboveWrapped)
490 hgap += av.charHeight;
493 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
494 int cHeight = av.getAlignment().getHeight() * av.charHeight;
496 av.setWrappedWidth(cWidth);
498 av.endRes = av.startRes + cWidth;
502 int maxwidth = av.getAlignment().getWidth() - 1;
504 if (av.hasHiddenColumns())
506 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
509 while ((ypos <= canvasHeight) && (startRes < maxwidth))
511 endx = startRes + cWidth - 1;
518 g.setFont(av.getFont());
519 g.setColor(Color.black);
521 if (av.scaleLeftWrapped)
523 drawWestScale(g, startRes, endx, ypos);
526 if (av.scaleRightWrapped)
528 g.translate(canvasWidth - LABEL_EAST, 0);
529 drawEastScale(g, startRes, endx, ypos);
530 g.translate(-(canvasWidth - LABEL_EAST), 0);
533 g.translate(LABEL_WEST, 0);
535 if (av.scaleAboveWrapped)
537 drawNorthScale(g, startRes, endx, ypos);
540 if (av.hasHiddenColumns() && av.showHiddenMarkers)
542 g.setColor(Color.blue);
544 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
547 res = av.getColumnSelection().findHiddenRegionPosition(i)
550 if (res < 0 || res > endx - startRes)
555 gg.fillPolygon(new int[]
556 { res * av.charWidth - av.charHeight / 4,
557 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
559 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
560 ypos - (av.charHeight / 2) + 8 }, 3);
565 // When printing we have an extra clipped region,
566 // the Printable page which we need to account for here
567 Shape clip = g.getClip();
571 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
575 g.setClip(0, (int) clip.getBounds().getY(), cWidth * av.charWidth,
576 (int) clip.getBounds().getHeight());
579 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
581 if (av.showAnnotation)
583 g.translate(0, cHeight + ypos + 3);
584 if (annotations == null)
586 annotations = new AnnotationPanel(av);
589 annotations.renderer.drawComponent(annotations, av, g,
590 -1, startRes, endx + 1);
591 g.translate(0, -cHeight - ypos - 3);
594 g.translate(-LABEL_WEST, 0);
596 ypos += cHeight + getAnnotationHeight() + hgap;
602 AnnotationPanel annotations;
604 int getAnnotationHeight()
606 if (!av.showAnnotation)
611 if (annotations == null)
613 annotations = new AnnotationPanel(av);
616 return annotations.adjustPanelHeight();
635 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
636 int endSeq, int offset)
638 if (!av.hasHiddenColumns())
640 draw(g1, startRes, endRes, startSeq, endSeq, offset);
644 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
647 int blockStart = startRes;
648 int blockEnd = endRes;
650 for (int i = 0; regions != null && i < regions.size(); i++)
652 int[] region = (int[]) regions.elementAt(i);
653 int hideStart = region[0];
654 int hideEnd = region[1];
656 if (hideStart <= blockStart)
658 blockStart += (hideEnd - hideStart) + 1;
662 blockEnd = hideStart - 1;
664 g1.translate(screenY * av.charWidth, 0);
666 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
668 if (av.getShowHiddenMarkers())
670 g1.setColor(Color.blue);
672 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
673 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
674 - 1, (endSeq - startSeq) * av.charHeight + offset);
677 g1.translate(-screenY * av.charWidth, 0);
678 screenY += blockEnd - blockStart + 1;
679 blockStart = hideEnd + 1;
682 if (screenY <= (endRes - startRes))
684 blockEnd = blockStart + (endRes - startRes) - screenY;
685 g1.translate(screenY * av.charWidth, 0);
686 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
688 g1.translate(-screenY * av.charWidth, 0);
694 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
695 // int x1, int x2, int y1, int y2, int startx, int starty,
696 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
699 g.setFont(av.getFont());
700 sr.prepare(g, av.renderGaps);
704 // / First draw the sequences
705 // ///////////////////////////
706 for (int i = startSeq; i < endSeq; i++)
708 nextSeq = av.getAlignment().getSequenceAt(i);
711 // occasionally, a race condition occurs such that the alignment row is
715 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
716 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
718 if (av.showSequenceFeatures)
720 fr.drawSequence(g, nextSeq, startRes, endRes, offset
721 + ((i - startSeq) * av.charHeight));
724 // / Highlight search Results once all sequences have been drawn
725 // ////////////////////////////////////////////////////////
726 if (searchResults != null)
728 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
730 if (visibleResults != null)
732 for (int r = 0; r < visibleResults.length; r += 2)
734 sr.drawHighlightedText(nextSeq, visibleResults[r],
735 visibleResults[r + 1], (visibleResults[r] - startRes)
736 * av.charWidth, offset
737 + ((i - startSeq) * av.charHeight));
742 if (av.cursorMode && cursorY == i && cursorX >= startRes
743 && cursorX <= endRes)
745 sr.drawCursor(nextSeq, cursorX,
746 (cursorX - startRes) * av.charWidth, offset
747 + ((i - startSeq) * av.charHeight));
751 if (av.getSelectionGroup() != null
752 || av.getAlignment().getGroups().size() > 0)
754 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
759 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
760 int startSeq, int endSeq, int offset)
762 Graphics2D g = (Graphics2D) g1;
764 // ///////////////////////////////////
765 // Now outline any areas if necessary
766 // ///////////////////////////////////
767 SequenceGroup group = av.getSelectionGroup();
773 int visWidth = (endRes - startRes + 1) * av.charWidth;
775 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
777 group = av.getAlignment().getGroups().get(0);
787 boolean inGroup = false;
791 for (i = startSeq; i < endSeq; i++)
793 sx = (group.getStartRes() - startRes) * av.charWidth;
794 sy = offset + ((i - startSeq) * av.charHeight);
795 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
797 if (sx + ex < 0 || sx > visWidth)
802 if ((sx <= (endRes - startRes) * av.charWidth)
803 && group.getSequences(null).contains(
804 av.getAlignment().getSequenceAt(i)))
807 && !group.getSequences(null).contains(
808 av.getAlignment().getSequenceAt(i + 1)))
810 bottom = sy + av.charHeight;
815 if (((top == -1) && (i == 0))
816 || !group.getSequences(null).contains(
817 av.getAlignment().getSequenceAt(i - 1)))
825 if (group == av.getSelectionGroup())
827 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
828 BasicStroke.JOIN_ROUND, 3f, new float[]
830 g.setColor(Color.RED);
834 g.setStroke(new BasicStroke());
835 g.setColor(group.getOutlineColour());
843 if (sx >= 0 && sx < visWidth)
845 g.drawLine(sx, oldY, sx, sy);
848 if (sx + ex < visWidth)
850 g.drawLine(sx + ex, oldY, sx + ex, sy);
859 if (sx + ex > visWidth)
864 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
866 ex = (endRes - startRes + 1) * av.charWidth;
871 g.drawLine(sx, top, sx + ex, top);
877 g.drawLine(sx, bottom, sx + ex, bottom);
888 sy = offset + ((i - startSeq) * av.charHeight);
889 if (sx >= 0 && sx < visWidth)
891 g.drawLine(sx, oldY, sx, sy);
894 if (sx + ex < visWidth)
896 g.drawLine(sx + ex, oldY, sx + ex, sy);
905 if (sx + ex > visWidth)
909 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
911 ex = (endRes - startRes + 1) * av.charWidth;
916 g.drawLine(sx, top, sx + ex, top);
922 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
931 g.setStroke(new BasicStroke());
933 if (groupIndex >= av.getAlignment().getGroups().size())
938 group = av.getAlignment().getGroups()
941 } while (groupIndex < av.getAlignment().getGroups().size());
953 public void highlightSearchResults(SearchResults results)
957 searchResults = results;