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
222 if (fastPaint || getWidth() != g.getClipBounds().width
\r
223 || getHeight() != g.getClipBounds().height)
\r
225 g.drawImage(img, 0, 0, this);
\r
230 // this draws the whole of the alignment
\r
231 imgWidth = getWidth();
\r
232 imgHeight = getHeight();
\r
234 imgWidth -= (imgWidth % av.charWidth);
\r
235 imgHeight -= (imgHeight % av.charHeight);
\r
237 if ( (imgWidth < 1) || (imgHeight < 1))
\r
242 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
243 gg = (Graphics2D) img.getGraphics();
\r
244 gg.setFont(av.getFont());
\r
245 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
246 RenderingHints.VALUE_ANTIALIAS_ON);
\r
248 gg.setColor(Color.white);
\r
249 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
251 chunkWidth = getWrappedCanvasWidth(getWidth());
\r
252 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
254 av.setChunkHeight(chunkHeight);
\r
255 av.setChunkWidth(chunkWidth);
\r
257 if (av.getWrapAlignment())
\r
259 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
263 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
264 av.startRes, av.startSeq, 0);
\r
267 g.drawImage(img, 0, 0, this);
\r
270 public int getWrappedCanvasWidth(int cwidth)
\r
272 FontMetrics fm = getFontMetrics(av.getFont());
\r
277 if (av.scaleRightWrapped)
\r
279 LABEL_EAST = fm.stringWidth(av.alignment.getWidth() + "000");
\r
282 if (av.scaleLeftWrapped)
\r
284 LABEL_WEST = fm.stringWidth(av.alignment.getWidth() + "");
\r
287 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
290 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
293 AlignmentI al = av.getAlignment();
\r
295 FontMetrics fm = getFontMetrics(av.getFont());
\r
297 int LABEL_EAST = 0;
\r
299 if (av.scaleRightWrapped)
\r
301 LABEL_EAST = fm.stringWidth(al.getWidth() + "000");
\r
304 int LABEL_WEST = 0;
\r
306 if (av.scaleLeftWrapped)
\r
308 LABEL_WEST = fm.stringWidth(al.getWidth() + "0");
\r
311 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
312 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
314 av.endRes = av.startRes + cWidth;
\r
316 int endx = (startRes + cWidth) - 1;
\r
317 int ypos = 2 * av.charHeight;
\r
319 while ( (ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
321 g.setColor(Color.black);
\r
323 if (av.scaleLeftWrapped)
\r
325 drawWestScale(g, startRes, endx, ypos);
\r
328 if (av.scaleRightWrapped)
\r
330 g.translate(canvasWidth - LABEL_EAST + av.charWidth, 0);
\r
331 drawEastScale(g, startRes, endx, ypos);
\r
332 g.translate( - (canvasWidth - LABEL_EAST + av.charWidth), 0);
\r
335 g.translate(LABEL_WEST, 0);
\r
337 if (av.scaleAboveWrapped)
\r
339 drawNorthScale(g, startRes, endx, ypos);
\r
342 // When printing we have an extra clipped region,
\r
343 // the Printable page which we need to account for here
\r
344 Shape clip = g.getClip();
\r
348 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
352 g.setClip(0, (int) clip.getBounds().getY(),
\r
353 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
356 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
358 g.translate( -LABEL_WEST, 0);
\r
361 startRes += cWidth;
\r
362 endx = (startRes + cWidth) - 1;
\r
364 if (endx > al.getWidth())
\r
366 endx = al.getWidth();
\r
371 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
372 int y2, int startx, int starty, int offset)
\r
374 Graphics2D g = (Graphics2D) g1;
\r
375 g.setFont(av.getFont());
\r
376 sr.renderGaps(av.renderGaps);
\r
380 /// First draw the sequences
\r
381 /////////////////////////////
\r
382 for (int i = y1; i < y2; i++)
\r
384 nextSeq = av.alignment.getSequenceAt(i);
\r
386 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
387 x1, x2, (x1 - startx) * av.charWidth,
\r
389 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
390 av.charWidth, av.charHeight);
\r
392 if (av.showSequenceFeatures)
\r
394 fr.drawSequence(g, nextSeq,
\r
395 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
396 (x1 - startx) * av.charWidth,
\r
398 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
399 av.charWidth, av.charHeight);
\r
404 /////////////////////////////////////
\r
405 // Now outline any areas if necessary
\r
406 /////////////////////////////////////
\r
407 SequenceGroup group = av.getSelectionGroup();
\r
408 java.util.Vector groups = av.alignment.getGroups();
\r
413 int groupIndex = -1;
\r
415 if ( (group == null) && (groups.size() > 0))
\r
417 group = (SequenceGroup) groups.elementAt(0);
\r
427 boolean inGroup = false;
\r
431 for (i = y1; i < y2; i++)
\r
433 sx = (group.getStartRes() - startx) * av.charWidth;
\r
435 AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
436 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
\r
440 if ( (sx < getWidth()) && (ex > 0) &&
\r
441 group.sequences.contains(av.alignment.getSequenceAt(
\r
444 if ( (bottom == -1) &&
\r
445 !group.sequences.contains(
\r
446 av.alignment.getSequenceAt(i + 1)))
\r
448 bottom = sy + av.charHeight;
\r
453 if ( ( (top == -1) && (i == 0)) ||
\r
454 !group.sequences.contains(
\r
455 av.alignment.getSequenceAt(i - 1)))
\r
463 if (group == av.getSelectionGroup())
\r
465 g.setStroke(new BasicStroke(1,
\r
466 BasicStroke.CAP_BUTT,
\r
467 BasicStroke.JOIN_ROUND, 3f,
\r
470 g.setColor(Color.RED);
\r
474 g.setStroke(new BasicStroke());
\r
475 g.setColor(group.getOutlineColour());
\r
483 g.drawLine(sx, oldY, sx, sy);
\r
484 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
488 g.drawLine(sx, top, sx + ex, top);
\r
494 g.drawLine(sx, bottom, sx + ex, bottom);
\r
507 g.drawLine(sx, top, sx + ex, top);
\r
513 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
518 AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
519 g.drawLine(sx, oldY, sx, sy);
\r
520 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
526 if (groupIndex >= groups.size())
\r
531 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
533 while (groupIndex < groups.size());
\r
536 /// Highlight search Results once all sequences have been drawn
\r
537 //////////////////////////////////////////////////////////
\r
540 for (int r = 0; r < searchResults.length; r += 3)
\r
542 int searchSeq = searchResults[r];
\r
544 if ( (searchSeq >= y1) && (searchSeq < y2))
\r
546 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
548 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
549 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
551 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
553 if (searchStart < x1)
\r
558 if (searchEnd > x2)
\r
563 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
564 (searchStart - startx) * av.charWidth,
\r
566 AlignmentUtil.getPixelHeight(starty,
\r
568 av.charHeight), av.charWidth, av.charHeight);
\r
574 public void highlightSearchResults(int[] results)
\r
576 // results are in the order sequence, startRes, endRes
\r
577 if (results == null)
\r
579 displaySearch = false;
\r
583 displaySearch = true;
\r
586 searchResults = results;
\r