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.
21 package jalview.appletgui;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.Comparison;
28 import jalview.viewmodel.AlignmentViewport;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Image;
35 import javax.swing.JPanel;
37 public class SeqCanvas extends JPanel
53 SearchResults searchResults = null;
55 boolean fastPaint = false;
61 public SeqCanvas(AlignViewport av)
64 fr = new FeatureRenderer(av);
65 sr = new SequenceRenderer(av);
66 PaintRefresher.Register(this, av.getSequenceSetId());
70 int avcharHeight = 0, avcharWidth = 0;
72 private void updateViewport()
74 avcharHeight = av.getCharHeight();
75 avcharWidth = av.getCharWidth();
78 public AlignmentViewport getViewport()
83 public FeatureRenderer getFeatureRenderer()
88 public SequenceRenderer getSequenceRenderer()
93 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
95 int scalestartx = startx - startx % 10 + 10;
97 g.setColor(Color.black);
100 for (int i = scalestartx; i < endx; i += 10)
103 if (av.hasHiddenColumns())
105 value = av.getColumnSelection().adjustForHiddenColumns(value);
108 g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
109 ypos - (avcharHeight / 2));
111 g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
112 (ypos + 2) - (avcharHeight / 2),
113 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
118 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
120 FontMetrics fm = getFontMetrics(av.getFont());
121 ypos += avcharHeight;
122 if (av.hasHiddenColumns())
124 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
125 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
128 int maxwidth = av.getAlignment().getWidth();
129 if (av.hasHiddenColumns())
131 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
135 for (int i = 0; i < av.getAlignment().getHeight(); i++)
137 SequenceI seq = av.getAlignment().getSequenceAt(i);
143 if (Comparison.isGap(seq.getCharAt(index)))
150 value = av.getAlignment().getSequenceAt(i).findPosition(index);
157 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
159 g.drawString(value + "", x, (ypos + (i * avcharHeight))
160 - (avcharHeight / 5));
165 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
167 ypos += avcharHeight;
169 if (av.hasHiddenColumns())
171 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
176 for (int i = 0; i < av.getAlignment().getHeight(); i++)
178 seq = av.getAlignment().getSequenceAt(i);
182 while (index > startx)
184 if (Comparison.isGap(seq.getCharAt(index)))
191 value = seq.findPosition(index);
198 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
199 - (avcharHeight / 5));
206 void fastPaint(int horizontal, int vertical)
208 if (fastPaint || gg == null)
215 // Its possible on certain browsers that the call to fastpaint
216 // is faster than it can paint, so this check here catches
218 if (lastsr + horizontal != av.startRes)
220 horizontal = av.startRes - lastsr;
223 lastsr = av.startRes;
226 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
227 - horizontal * avcharWidth,
228 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
229 -vertical * avcharHeight);
231 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
233 if (horizontal > 0) // scrollbar pulled right, image to the left
235 transX = (er - sr - horizontal) * avcharWidth;
236 sr = er - horizontal;
238 else if (horizontal < 0)
240 er = sr - horizontal;
243 else if (vertical > 0) // scroll down
246 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
252 transY = imgHeight - vertical * avcharHeight;
255 else if (vertical < 0)
264 gg.translate(transX, transY);
266 drawPanel(gg, sr, er, ss, es, 0);
267 gg.translate(-transX, -transY);
274 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
275 * towards! startx is the first residue (starting at 0) to display. endx is
276 * the last residue to display (starting at 0). starty is the first sequence
277 * to display (starting at 0). endy is the last sequence to display (starting
278 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
279 * adjustment listener in SeqPanel when the scrollbars move.
281 public void update(Graphics g)
287 public void paint(Graphics g)
291 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
292 .getClipBounds().height)))
294 g.drawImage(img, 0, 0, this);
301 g.drawImage(img, 0, 0, this);
307 // this draws the whole of the alignment
308 imgWidth = this.getSize().width;
309 imgHeight = this.getSize().height;
311 imgWidth -= imgWidth % avcharWidth;
312 imgHeight -= imgHeight % avcharHeight;
314 if (imgWidth < 1 || imgHeight < 1)
319 if (img == null || imgWidth != img.getWidth(this)
320 || imgHeight != img.getHeight(this))
322 img = createImage(imgWidth, imgHeight);
323 gg = img.getGraphics();
324 gg.setFont(av.getFont());
327 gg.setColor(Color.white);
328 gg.fillRect(0, 0, imgWidth, imgHeight);
330 if (av.getWrapAlignment())
332 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
336 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
339 g.drawImage(img, 0, 0, this);
343 int LABEL_WEST, LABEL_EAST;
345 public int getWrappedCanvasWidth(int cwidth)
347 cwidth -= cwidth % av.getCharWidth();
349 FontMetrics fm = getFontMetrics(av.getFont());
354 if (av.getScaleRightWrapped())
356 LABEL_EAST = fm.stringWidth(getMask());
359 if (av.getScaleLeftWrapped())
361 LABEL_WEST = fm.stringWidth(getMask());
364 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
368 * Generates a string of zeroes.
377 AlignmentI alignment = av.getAlignment();
378 for (int i = 0; i < alignment.getHeight(); i++)
380 tmp = alignment.getSequenceAt(i).getEnd();
387 for (int i = maxWidth; i > 0; i /= 10)
394 private void drawWrappedPanel(Graphics g, int canvasWidth,
395 int canvasHeight, int startRes)
397 AlignmentI al = av.getAlignment();
399 FontMetrics fm = getFontMetrics(av.getFont());
401 if (av.getScaleRightWrapped())
403 LABEL_EAST = fm.stringWidth(getMask());
406 if (av.getScaleLeftWrapped())
408 LABEL_WEST = fm.stringWidth(getMask());
411 int hgap = avcharHeight;
412 if (av.getScaleAboveWrapped())
414 hgap += avcharHeight;
417 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
418 int cHeight = av.getAlignment().getHeight() * avcharHeight;
420 av.setWrappedWidth(cWidth);
422 av.endRes = av.startRes + cWidth;
427 int maxwidth = av.getAlignment().getWidth() - 1;
429 if (av.hasHiddenColumns())
431 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
434 while ((ypos <= canvasHeight) && (startRes < maxwidth))
436 endx = startRes + cWidth - 1;
443 g.setColor(Color.black);
445 if (av.getScaleLeftWrapped())
447 drawWestScale(g, startRes, endx, ypos);
450 if (av.getScaleRightWrapped())
452 g.translate(canvasWidth - LABEL_EAST, 0);
453 drawEastScale(g, startRes, endx, ypos);
454 g.translate(-(canvasWidth - LABEL_EAST), 0);
457 g.translate(LABEL_WEST, 0);
459 if (av.getScaleAboveWrapped())
461 drawNorthScale(g, startRes, endx, ypos);
463 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
465 g.setColor(Color.blue);
467 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
470 res = av.getColumnSelection().findHiddenRegionPosition(i)
473 if (res < 0 || res > endx - startRes)
478 gg.fillPolygon(new int[]
479 { res * avcharWidth - avcharHeight / 4,
480 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
482 { ypos - (avcharHeight / 2), ypos - (avcharHeight / 2),
483 ypos - (avcharHeight / 2) + 8 }, 3);
488 if (g.getClip() == null)
490 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
493 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
496 if (av.isShowAnnotation())
498 g.translate(0, cHeight + ypos + 4);
499 if (annotations == null)
501 annotations = new AnnotationPanel(av);
504 annotations.drawComponent(g, startRes, endx + 1);
505 g.translate(0, -cHeight - ypos - 4);
507 g.translate(-LABEL_WEST, 0);
509 ypos += cHeight + getAnnotationHeight() + hgap;
516 AnnotationPanel annotations;
518 int getAnnotationHeight()
520 if (!av.isShowAnnotation())
525 if (annotations == null)
527 annotations = new AnnotationPanel(av);
530 return annotations.adjustPanelHeight();
533 private void drawPanel(Graphics g1, int startRes, int endRes,
535 int endSeq, int offset)
539 if (!av.hasHiddenColumns())
541 draw(g1, startRes, endRes, startSeq, endSeq, offset);
547 int blockStart = startRes;
548 int blockEnd = endRes;
550 if (av.hasHiddenColumns())
552 for (int[] region : av.getColumnSelection().getHiddenColumns())
554 int hideStart = region[0];
555 int hideEnd = region[1];
557 if (hideStart <= blockStart)
559 blockStart += (hideEnd - hideStart) + 1;
563 blockEnd = hideStart - 1;
565 g1.translate(screenY * avcharWidth, 0);
567 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
569 if (av.getShowHiddenMarkers())
571 g1.setColor(Color.blue);
572 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
573 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
574 - 1, (endSeq - startSeq) * avcharHeight
578 g1.translate(-screenY * avcharWidth, 0);
579 screenY += blockEnd - blockStart + 1;
580 blockStart = hideEnd + 1;
583 if (screenY <= (endRes - startRes))
585 blockEnd = blockStart + (endRes - startRes) - screenY;
586 g1.translate(screenY * avcharWidth, 0);
587 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
589 g1.translate(-screenY * avcharWidth, 0);
595 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
596 // int x1, int x2, int y1, int y2, int startx, int starty,
597 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
600 g.setFont(av.getFont());
601 sr.prepare(g, av.isRenderGaps());
605 // / First draw the sequences
606 // ///////////////////////////
607 for (int i = startSeq; i < endSeq; i++)
609 nextSeq = av.getAlignment().getSequenceAt(i);
616 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
617 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
619 if (av.isShowSequenceFeatures())
621 fr.drawSequence(g, nextSeq, startRes, endRes, offset
622 + ((i - startSeq) * avcharHeight));
625 // / Highlight search Results once all sequences have been drawn
626 // ////////////////////////////////////////////////////////
627 if (searchResults != null)
629 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
631 if (visibleResults != null)
633 for (int r = 0; r < visibleResults.length; r += 2)
635 sr.drawHighlightedText(nextSeq, visibleResults[r],
636 visibleResults[r + 1], (visibleResults[r] - startRes)
637 * avcharWidth, offset
638 + ((i - startSeq) * avcharHeight));
643 if (av.cursorMode && cursorY == i && cursorX >= startRes
644 && cursorX <= endRes)
646 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
647 offset + ((i - startSeq) * avcharHeight));
651 if (av.getSelectionGroup() != null
652 || av.getAlignment().getGroups().size() > 0)
654 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
659 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
660 int startSeq, int endSeq, int offset)
663 // ///////////////////////////////////
664 // Now outline any areas if necessary
665 // ///////////////////////////////////
666 SequenceGroup group = av.getSelectionGroup();
673 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
675 group = av.getAlignment().getGroups().get(0);
685 boolean inGroup = false;
688 int alHeight = av.getAlignment().getHeight() - 1;
690 for (i = startSeq; i < endSeq; i++)
692 sx = (group.getStartRes() - startRes) * avcharWidth;
693 sy = offset + ((i - startSeq) * avcharHeight);
694 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
696 if (sx + ex < 0 || sx > imgWidth)
701 if ((sx <= (endRes - startRes) * avcharWidth)
702 && group.getSequences(null).contains(
703 av.getAlignment().getSequenceAt(i)))
706 && (i >= alHeight || !group.getSequences(null)
708 av.getAlignment().getSequenceAt(i + 1))))
710 bottom = sy + avcharHeight;
715 if (((top == -1) && (i == 0))
716 || !group.getSequences(null).contains(
717 av.getAlignment().getSequenceAt(i - 1)))
725 if (group == av.getSelectionGroup())
727 g.setColor(Color.red);
731 g.setColor(group.getOutlineColour());
739 if (sx >= 0 && sx < imgWidth)
741 g.drawLine(sx, oldY, sx, sy);
744 if (sx + ex < imgWidth)
746 g.drawLine(sx + ex, oldY, sx + ex, sy);
755 if (sx + ex > imgWidth)
760 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
762 ex = (endRes - startRes + 1) * avcharWidth;
767 g.drawLine(sx, top, sx + ex, top);
773 g.drawLine(sx, bottom, sx + ex, bottom);
784 sy = offset + ((i - startSeq) * avcharHeight);
785 if (sx >= 0 && sx < imgWidth)
787 g.drawLine(sx, oldY, sx, sy);
790 if (sx + ex < imgWidth)
792 g.drawLine(sx + ex, oldY, sx + ex, sy);
801 if (sx + ex > imgWidth)
805 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
807 ex = (endRes - startRes + 1) * avcharWidth;
812 g.drawLine(sx, top, sx + ex, top);
818 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
827 if (groupIndex >= av.getAlignment().getGroups().size())
832 group = av.getAlignment().getGroups()
834 } while (groupIndex < av.getAlignment().getGroups().size());
839 public void highlightSearchResults(SearchResults results)
841 searchResults = results;