2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
22 import java.awt.image.*;
\r
23 import javax.swing.*;
\r
25 import jalview.analysis.*;
\r
26 import jalview.datamodel.*;
\r
28 public class SeqCanvas
\r
32 SequenceRenderer sr;
\r
38 boolean showScores = false;
\r
39 boolean displaySearch = false;
\r
40 int[] searchResults = null;
\r
43 boolean fastPaint = false;
\r
48 public SeqCanvas(AlignViewport av)
\r
51 fr = new FeatureRenderer(av);
\r
52 sr = new SequenceRenderer(av);
\r
53 setLayout(new BorderLayout());
\r
54 PaintRefresher.Register(this, av.alignment);
\r
55 setBackground(Color.white);
\r
58 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
60 int scalestartx = startx - (startx % 10) + 10;
\r
62 g.setColor(Color.black);
\r
65 for (int i = scalestartx; i < endx; i += 10)
\r
67 String string = String.valueOf(i);
\r
68 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
69 ypos - (av.charHeight / 2));
\r
71 g.drawLine( ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2),
\r
72 (ypos + 2) - (av.charHeight / 2),
\r
73 ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
\r
78 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
80 FontMetrics fm = getFontMetrics(av.getFont());
\r
81 ypos += av.charHeight;
\r
84 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
86 SequenceI seq = av.alignment.getSequenceAt(i);
\r
90 while (index < endx)
\r
92 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
99 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
106 int x = LABEL_WEST - fm.stringWidth(value + "");
\r
107 g.drawString(value + "", x,
\r
108 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
113 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
115 ypos += av.charHeight;
\r
118 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
120 SequenceI seq = av.alignment.getSequenceAt(i);
\r
124 while (index > startx)
\r
126 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
133 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
140 g.drawString(value + "", 0,
\r
141 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
146 public void fastPaint(int horizontal, int vertical)
\r
148 if ( ( (horizontal == 0) && (vertical == 0)) || (gg == null))
\r
153 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
154 -vertical * av.charHeight);
\r
156 int sr = av.startRes;
\r
157 int er = av.endRes;
\r
158 int ss = av.startSeq;
\r
159 int es = av.endSeq;
\r
163 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
165 transX = (er - sr - horizontal) * av.charWidth;
\r
166 sr = er - horizontal;
\r
168 else if (horizontal < 0)
\r
170 er = sr - horizontal;
\r
172 else if (vertical > 0) // scroll down
\r
174 ss = es - vertical;
\r
176 if (ss < av.startSeq)
\r
177 { // ie scrolling too fast, more than a page at a time
\r
182 transY = imgHeight - (vertical * av.charHeight);
\r
185 else if (vertical < 0)
\r
187 es = ss - vertical;
\r
189 if (es > av.endSeq)
\r
195 gg.translate(transX, transY);
\r
197 gg.setColor(Color.white);
\r
198 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth,
\r
199 (es - ss) * av.charHeight);
\r
200 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
201 gg.translate( -transX, -transY);
\r
208 * Definitions of startx and endx (hopefully):
\r
209 * SMJS This is what I'm working towards!
\r
210 * startx is the first residue (starting at 0) to display.
\r
211 * endx is the last residue to display (starting at 0).
\r
212 * starty is the first sequence to display (starting at 0).
\r
213 * endy is the last sequence to display (starting at 0).
\r
214 * NOTE 1: The av limits are set in setFont in this class and
\r
215 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
219 // Set this to false to force a full panel paint
\r
220 public void paintComponent(Graphics g)
\r
223 && (fastPaint || getWidth() != g.getClipBounds().width
\r
224 || getHeight() != g.getClipBounds().height))
\r
226 g.drawImage(img, 0, 0, this);
\r
231 // this draws the whole of the alignment
\r
232 imgWidth = getWidth();
\r
233 imgHeight = getHeight();
\r
235 imgWidth -= (imgWidth % av.charWidth);
\r
236 imgHeight -= (imgHeight % av.charHeight);
\r
238 if ( (imgWidth < 1) || (imgHeight < 1))
\r
243 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
244 gg = (Graphics2D) img.getGraphics();
\r
245 gg.setFont(av.getFont());
\r
246 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
247 RenderingHints.VALUE_ANTIALIAS_ON);
\r
249 gg.setColor(Color.white);
\r
250 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
252 chunkWidth = getWrappedCanvasWidth(getWidth());
\r
253 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
255 av.setChunkHeight(chunkHeight);
\r
256 av.setChunkWidth(chunkWidth);
\r
258 if (av.getWrapAlignment())
\r
260 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
264 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
265 av.startRes, av.startSeq, 0);
\r
268 g.drawImage(img, 0, 0, this);
\r
271 public int getWrappedCanvasWidth(int cwidth)
\r
273 FontMetrics fm = getFontMetrics(av.getFont());
\r
278 if (av.scaleRightWrapped)
\r
280 LABEL_EAST = fm.stringWidth(av.alignment.getWidth() + "000");
\r
283 if (av.scaleLeftWrapped)
\r
285 LABEL_WEST = fm.stringWidth(av.alignment.getWidth() + "");
\r
288 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
291 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
294 AlignmentI al = av.getAlignment();
\r
296 FontMetrics fm = getFontMetrics(av.getFont());
\r
298 int LABEL_EAST = 0;
\r
300 if (av.scaleRightWrapped)
\r
302 LABEL_EAST = fm.stringWidth(al.getWidth() + "000");
\r
305 int LABEL_WEST = 0;
\r
307 if (av.scaleLeftWrapped)
\r
309 LABEL_WEST = fm.stringWidth(al.getWidth() + "0");
\r
312 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
313 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
315 av.endRes = av.startRes + cWidth;
\r
317 int endx = (startRes + cWidth) - 1;
\r
318 int ypos = 2 * av.charHeight;
\r
320 while ( (ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
322 g.setColor(Color.black);
\r
324 if (av.scaleLeftWrapped)
\r
326 drawWestScale(g, startRes, endx, ypos);
\r
329 if (av.scaleRightWrapped)
\r
331 g.translate(canvasWidth - LABEL_EAST + av.charWidth, 0);
\r
332 drawEastScale(g, startRes, endx, ypos);
\r
333 g.translate( - (canvasWidth - LABEL_EAST + av.charWidth), 0);
\r
336 g.translate(LABEL_WEST, 0);
\r
338 if (av.scaleAboveWrapped)
\r
340 drawNorthScale(g, startRes, endx, ypos);
\r
343 // When printing we have an extra clipped region,
\r
344 // the Printable page which we need to account for here
\r
345 Shape clip = g.getClip();
\r
349 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
353 g.setClip(0, (int) clip.getBounds().getY(),
\r
354 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
357 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
359 g.translate( -LABEL_WEST, 0);
\r
362 startRes += cWidth;
\r
363 endx = (startRes + cWidth) - 1;
\r
365 if (endx > al.getWidth())
\r
367 endx = al.getWidth();
\r
372 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
373 int y2, int startx, int starty, int offset)
\r
375 Graphics2D g = (Graphics2D) g1;
\r
376 g.setFont(av.getFont());
\r
377 sr.renderGaps(av.renderGaps);
\r
381 /// First draw the sequences
\r
382 /////////////////////////////
\r
383 for (int i = y1; i < y2; i++)
\r
385 nextSeq = av.alignment.getSequenceAt(i);
\r
387 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
388 x1, x2, (x1 - startx) * av.charWidth,
\r
390 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
391 av.charWidth, av.charHeight);
\r
393 if (av.showSequenceFeatures)
\r
395 fr.drawSequence(g, nextSeq,
\r
396 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
397 (x1 - startx) * av.charWidth,
\r
399 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
400 av.charWidth, av.charHeight);
\r
405 /////////////////////////////////////
\r
406 // Now outline any areas if necessary
\r
407 /////////////////////////////////////
\r
408 SequenceGroup group = av.getSelectionGroup();
\r
409 java.util.Vector groups = av.alignment.getGroups();
\r
414 int groupIndex = -1;
\r
416 if ( (group == null) && (groups.size() > 0))
\r
418 group = (SequenceGroup) groups.elementAt(0);
\r
428 boolean inGroup = false;
\r
432 for (i = y1; i < y2; i++)
\r
434 sx = (group.getStartRes() - startx) * av.charWidth;
\r
436 AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
437 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
\r
441 if ( (sx < getWidth()) && (ex > 0) &&
\r
442 group.sequences.contains(av.alignment.getSequenceAt(
\r
445 if ( (bottom == -1) &&
\r
446 !group.sequences.contains(
\r
447 av.alignment.getSequenceAt(i + 1)))
\r
449 bottom = sy + av.charHeight;
\r
454 if ( ( (top == -1) && (i == 0)) ||
\r
455 !group.sequences.contains(
\r
456 av.alignment.getSequenceAt(i - 1)))
\r
464 if (group == av.getSelectionGroup())
\r
466 g.setStroke(new BasicStroke(1,
\r
467 BasicStroke.CAP_BUTT,
\r
468 BasicStroke.JOIN_ROUND, 3f,
\r
471 g.setColor(Color.RED);
\r
475 g.setStroke(new BasicStroke());
\r
476 g.setColor(group.getOutlineColour());
\r
484 g.drawLine(sx, oldY, sx, sy);
\r
485 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
489 g.drawLine(sx, top, sx + ex, top);
\r
495 g.drawLine(sx, bottom, sx + ex, bottom);
\r
508 g.drawLine(sx, top, sx + ex, top);
\r
514 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
519 AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
520 g.drawLine(sx, oldY, sx, sy);
\r
521 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
527 if (groupIndex >= groups.size())
\r
532 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
534 while (groupIndex < groups.size());
\r
537 /// Highlight search Results once all sequences have been drawn
\r
538 //////////////////////////////////////////////////////////
\r
541 for (int r = 0; r < searchResults.length; r += 3)
\r
543 int searchSeq = searchResults[r];
\r
545 if ( (searchSeq >= y1) && (searchSeq < y2))
\r
547 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
549 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
550 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
552 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
554 if (searchStart < x1)
\r
559 if (searchEnd > x2)
\r
564 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
565 (searchStart - startx) * av.charWidth,
\r
567 AlignmentUtil.getPixelHeight(starty,
\r
569 av.charHeight), av.charWidth, av.charHeight);
\r
575 public void highlightSearchResults(int[] results)
\r
577 // results are in the order sequence, startRes, endRes
\r
578 if (results == null)
\r
580 displaySearch = false;
\r
584 displaySearch = true;
\r
587 searchResults = results;
\r