4 import javax.swing.*;
\r
5 import jalview.datamodel.*;
\r
6 import jalview.analysis.*;
\r
9 public class SeqCanvas extends JPanel
\r
19 boolean showScores = false;
\r
20 boolean displaySearch = false;
\r
21 int [] searchResults = null;
\r
26 boolean fastPaint = false;
\r
29 public SeqCanvas(AlignViewport av)
\r
32 fr = new FeatureRenderer(av);
\r
33 setLayout(new BorderLayout());
\r
34 PaintRefresher.Register(this);
\r
38 void drawNorthScale(Graphics g, int startx, int endx,int ypos) {
\r
39 int scalestartx = startx - startx % 10 + 10;
\r
41 g.setColor(Color.black);
\r
44 for (int i = scalestartx; i < endx; i += 10)
\r
46 String string = String.valueOf(i);
\r
47 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
48 ypos - av.charHeight / 2);
\r
50 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
51 ypos + 2 - av.charHeight / 2,
\r
52 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
57 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
59 FontMetrics fm = getFontMetrics(av.getFont());
\r
60 ypos+= av.charHeight;
\r
62 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
64 SequenceI seq = av.alignment.getSequenceAt(i);
\r
67 while (index < endx)
\r
69 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
75 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
80 int x = fm.stringWidth("000") - fm.stringWidth(value+"");
\r
81 g.drawString(value + "", x, ypos + i*av.charHeight - av.charHeight/5);
\r
86 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
88 ypos+= av.charHeight;
\r
90 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
92 SequenceI seq = av.alignment.getSequenceAt(i);
\r
95 while (index > startx)
\r
97 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
103 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
107 g.drawString(value + "", 0, ypos + i*av.charHeight - av.charHeight/5);
\r
116 public void fastPaint(int horizontal, int vertical)
\r
118 if (horizontal == 0 && vertical == 0)
\r
121 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
122 -vertical * av.charHeight);
\r
124 int sr = av.startRes, er = av.endRes + 1, ss = av.startSeq, es = av.endSeq,
\r
125 transX = 0, transY = 0;
\r
126 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
128 transX = (er - sr - horizontal) * av.charWidth;
\r
129 sr = er - horizontal;
\r
131 else if (horizontal < 0)
\r
132 er = sr - horizontal;
\r
134 else if (vertical > 0) // scroll down
\r
136 transY = imgHeight - vertical * av.charHeight;
\r
137 ss = es - vertical;
\r
139 else if (vertical < 0)
\r
140 es = ss - vertical;
\r
142 gg.translate(transX, transY);
\r
144 gg.setColor(Color.white);
\r
145 gg.fillRect(0,0, (er-sr)*av.charWidth, (es-ss)*av.charHeight);
\r
146 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
147 gg.translate( -transX, -transY);
\r
155 * Definitions of startx and endx (hopefully):
\r
156 * SMJS This is what I'm working towards!
\r
157 * startx is the first residue (starting at 0) to display.
\r
158 * endx is the last residue to display (starting at 0).
\r
159 * starty is the first sequence to display (starting at 0).
\r
160 * endy is the last sequence to display (starting at 0).
\r
161 * NOTE 1: The av limits are set in setFont in this class and
\r
162 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
165 public void paintComponent(Graphics g)
\r
167 g.setColor(Color.white);
\r
168 g.fillRect(0, 0, getWidth(), getHeight());
\r
172 g.drawImage(img, 0, 0, this);
\r
177 // this draws the whole of the alignment
\r
178 imgWidth = getWidth();
\r
179 imgHeight = getHeight();
\r
180 if(imgWidth==0 || imgHeight==0)
\r
183 imgWidth -= imgWidth%av.charWidth;
\r
184 imgHeight-= imgHeight%av.charHeight;
\r
186 img = createImage(imgWidth,imgHeight);
\r
187 gg = (Graphics2D)img.getGraphics();
\r
188 gg.setFont(av.getFont());
\r
189 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
\r
191 gg.setColor(Color.white);
\r
192 gg.fillRect(0,0,imgWidth,imgHeight);
\r
194 chunkWidth = getWrappedCanvasWidth( getWidth() );
\r
195 chunkHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;
\r
197 av.setChunkHeight(chunkHeight);
\r
198 av.setChunkWidth(chunkWidth);
\r
201 if (av.getWrapAlignment())
\r
202 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
204 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);
\r
206 g.drawImage(img, 0, 0, this);
\r
210 public int getWrappedCanvasWidth(int cwidth)
\r
212 FontMetrics fm = getFontMetrics(av.getFont());
\r
214 int LABEL_EAST = 0;
\r
215 if(av.scaleRightWrapped)
\r
216 LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );
\r
217 int LABEL_WEST = 0;
\r
218 if(av.scaleLeftWrapped)
\r
219 LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"0" );
\r
221 return (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;
\r
224 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)
\r
226 AlignmentI al = av.getAlignment();
\r
228 FontMetrics fm = getFontMetrics(av.getFont());
\r
230 int LABEL_EAST = 0;
\r
231 if(av.scaleRightWrapped)
\r
232 LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );
\r
233 int LABEL_WEST = 0;
\r
234 if(av.scaleLeftWrapped)
\r
235 LABEL_WEST = fm.stringWidth( al.getWidth()+"0" );
\r
237 int cWidth = (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;
\r
238 int cHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;
\r
240 int endx = startRes+cWidth-1;
\r
241 int ypos = 2*av.charHeight;
\r
244 while (ypos <= canvasHeight && endx <al.getWidth())
\r
246 g.setColor(Color.black);
\r
248 if(av.scaleLeftWrapped)
\r
249 drawWestScale(g, startRes, endx, ypos);
\r
251 if(av.scaleRightWrapped)
\r
253 g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);
\r
254 drawEastScale(g, startRes, endx, ypos);
\r
255 g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);
\r
259 g.translate(LABEL_WEST,0);
\r
260 if(av.scaleAboveWrapped)
\r
261 drawNorthScale(g, startRes, endx, ypos);
\r
263 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
264 g.translate(-LABEL_WEST,0);
\r
267 startRes += cWidth;
\r
268 endx = startRes + cWidth - 1;
\r
270 if (endx > al.getWidth())
\r
271 endx = al.getWidth();
\r
277 public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {
\r
279 Graphics2D g = (Graphics2D)g1;
\r
280 g.setFont(av.getFont());
\r
281 RendererI sr = av.getRenderer();
\r
283 /*Vector pid = av.getConsensus(false);
\r
284 Vector tmpseq = new Vector();
\r
285 for (int i = 0; i < av.getAlignment().getHeight(); i++)
\r
286 if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))
\r
287 tmpseq.addElement(av.getAlignment().getSequenceAt(i));
\r
289 if (sr instanceof SequenceRenderer)
\r
290 pid = AAFrequency.calculate(tmpseq,x1,x2);
\r
292 else if (sr instanceof GraphRenderer)
\r
293 pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),
\r
294 av.getAlignment().getSequences(),
\r
295 av.getPIDWindow(),x1,x2);
\r
299 /* if (y2 > starty && y1 < av.getEndSeq())
\r
303 (x1-startx)*charWidth,
\r
304 offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),
\r
305 (x2-x1+1)*charWidth,
\r
306 offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));
\r
311 /// First draw the sequences
\r
312 /////////////////////////////
\r
313 for (int i = y1 ; i < y2 ;i++)
\r
315 nextSeq = av.alignment.getSequenceAt(i);
\r
317 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,
\r
318 (x1 - startx) * av.charWidth,
\r
319 offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
320 av.charWidth,av.charHeight,null, i);
\r
322 if(av.showSequenceFeatures)
\r
324 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,
\r
325 (x1 - startx) * av.charWidth,
\r
327 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
328 av.charWidth, av.charHeight, null, i);
\r
332 /////////////////////////////////////
\r
334 // Now outline any areas if necessary
\r
335 /////////////////////////////////////
\r
336 SequenceGroup group = av.getSelectionGroup();
\r
337 java.util.Vector groups = av.alignment.getGroups();
\r
339 int sx = -1, sy = -1, ex = -1;
\r
340 int groupIndex = -1;
\r
341 if (group == null && groups.size() > 0)
\r
343 group = (SequenceGroup) groups.elementAt(0);
\r
352 boolean inGroup = false;
\r
353 int top=-1, bottom =-1;
\r
354 for (i = y1; i < y2; i++)
\r
356 sx = (group.getStartRes() - startx) * av.charWidth;
\r
357 sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
358 ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth;
\r
360 if (av.getWrapAlignment())
\r
365 if (ex > getWidth())
\r
367 if (av.getWrapAlignment())
\r
368 ex = getWrappedCanvasWidth(getWidth()) * av.charWidth;
\r
372 else if(ex>getWidth())
\r
373 ex -= av.charWidth;
\r
376 if (sx < getWidth()
\r
378 && group.sequences.contains(av.alignment.getSequenceAt(i)))
\r
380 if (bottom == -1 &&
\r
381 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))
\r
382 bottom = sy + av.charHeight ;
\r
386 if (top == -1 && i==0 ||
\r
387 !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))
\r
393 if (group == av.getSelectionGroup())
\r
395 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));
\r
396 g.setColor(Color.RED);
\r
400 g.setStroke(new BasicStroke());
\r
401 g.setColor(group.getOutlineColour());
\r
409 g.drawLine(sx, oldY, sx, sy );
\r
410 g.drawLine(sx+ex, oldY, sx+ex, sy );
\r
414 g.drawLine(sx, top, sx + ex, top);
\r
419 g.drawLine(sx, bottom, sx + ex, bottom);
\r
434 g.drawLine(sx, top, sx + ex, top);
\r
439 g.drawLine(sx, bottom-1, sx + ex, bottom-1);
\r
443 sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
444 g.drawLine(sx, oldY, sx, sy );
\r
445 g.drawLine(sx+ex, oldY, sx+ex, sy );
\r
449 if (groupIndex >= groups.size())
\r
452 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
455 while (groupIndex < groups.size());
\r
458 /// Highlight search Results once all sequences have been drawn
\r
459 //////////////////////////////////////////////////////////
\r
462 for(int r=0; r<searchResults.length; r+=3)
\r
464 int searchSeq = searchResults[r];
\r
465 int searchStart = searchResults[r+1];
\r
466 int searchEnd = searchResults[r+2];
\r
468 if (searchSeq >= y1 && searchSeq < y2)
\r
470 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
475 ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),
\r
478 (searchStart - startx) * av.charWidth,
\r
480 AlignmentUtil.getPixelHeight(starty, searchSeq,
\r
492 public void highlightSearchResults(int [] results)
\r
494 // results are in the order sequence, startRes, endRes
\r
496 displaySearch = false;
\r
498 displaySearch = true;
\r
500 searchResults = results;
\r