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 || gg==null)
\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 ss = es - vertical;
\r
137 if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time
\r
140 transY = imgHeight - vertical * av.charHeight;
\r
142 else if (vertical < 0)
\r
144 es = ss - vertical;
\r
150 gg.translate(transX, transY);
\r
152 gg.setColor(Color.white);
\r
153 gg.fillRect(0,0, (er-sr)*av.charWidth, (es-ss)*av.charHeight);
\r
154 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
155 gg.translate( -transX, -transY);
\r
163 * Definitions of startx and endx (hopefully):
\r
164 * SMJS This is what I'm working towards!
\r
165 * startx is the first residue (starting at 0) to display.
\r
166 * endx is the last residue to display (starting at 0).
\r
167 * starty is the first sequence to display (starting at 0).
\r
168 * endy is the last sequence to display (starting at 0).
\r
169 * NOTE 1: The av limits are set in setFont in this class and
\r
170 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
173 public void paintComponent(Graphics g)
\r
175 g.setColor(Color.white);
\r
176 g.fillRect(0, 0, getWidth(), getHeight());
\r
180 g.drawImage(img, 0, 0, this);
\r
185 // this draws the whole of the alignment
\r
186 imgWidth = getWidth();
\r
187 imgHeight = getHeight();
\r
188 if(imgWidth==0 || imgHeight==0)
\r
191 imgWidth -= imgWidth%av.charWidth;
\r
192 imgHeight-= imgHeight%av.charHeight;
\r
194 img = createImage(imgWidth,imgHeight);
\r
195 gg = (Graphics2D)img.getGraphics();
\r
196 gg.setFont(av.getFont());
\r
197 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
\r
199 gg.setColor(Color.white);
\r
200 gg.fillRect(0,0,imgWidth,imgHeight);
\r
202 chunkWidth = getWrappedCanvasWidth( getWidth() );
\r
203 chunkHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;
\r
205 av.setChunkHeight(chunkHeight);
\r
206 av.setChunkWidth(chunkWidth);
\r
209 if (av.getWrapAlignment())
\r
210 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
212 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);
\r
214 g.drawImage(img, 0, 0, this);
\r
218 public int getWrappedCanvasWidth(int cwidth)
\r
220 FontMetrics fm = getFontMetrics(av.getFont());
\r
222 int LABEL_EAST = 0;
\r
223 if(av.scaleRightWrapped)
\r
224 LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );
\r
225 int LABEL_WEST = 0;
\r
226 if(av.scaleLeftWrapped)
\r
227 LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"0" );
\r
229 return (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;
\r
232 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)
\r
234 AlignmentI al = av.getAlignment();
\r
236 FontMetrics fm = getFontMetrics(av.getFont());
\r
238 int LABEL_EAST = 0;
\r
239 if(av.scaleRightWrapped)
\r
240 LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );
\r
241 int LABEL_WEST = 0;
\r
242 if(av.scaleLeftWrapped)
\r
243 LABEL_WEST = fm.stringWidth( al.getWidth()+"0" );
\r
245 int cWidth = (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;
\r
246 int cHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;
\r
248 int endx = startRes+cWidth-1;
\r
249 int ypos = 2*av.charHeight;
\r
252 while (ypos <= canvasHeight && endx <al.getWidth())
\r
254 g.setColor(Color.black);
\r
256 if(av.scaleLeftWrapped)
\r
257 drawWestScale(g, startRes, endx, ypos);
\r
259 if(av.scaleRightWrapped)
\r
261 g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);
\r
262 drawEastScale(g, startRes, endx, ypos);
\r
263 g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);
\r
267 g.translate(LABEL_WEST,0);
\r
268 if(av.scaleAboveWrapped)
\r
269 drawNorthScale(g, startRes, endx, ypos);
\r
271 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
272 g.translate(-LABEL_WEST,0);
\r
275 startRes += cWidth;
\r
276 endx = startRes + cWidth - 1;
\r
278 if (endx > al.getWidth())
\r
279 endx = al.getWidth();
\r
285 public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {
\r
287 Graphics2D g = (Graphics2D)g1;
\r
288 g.setFont(av.getFont());
\r
289 RendererI sr = av.getRenderer();
\r
291 /*Vector pid = av.getConsensus(false);
\r
292 Vector tmpseq = new Vector();
\r
293 for (int i = 0; i < av.getAlignment().getHeight(); i++)
\r
294 if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))
\r
295 tmpseq.addElement(av.getAlignment().getSequenceAt(i));
\r
297 if (sr instanceof SequenceRenderer)
\r
298 pid = AAFrequency.calculate(tmpseq,x1,x2);
\r
300 else if (sr instanceof GraphRenderer)
\r
301 pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),
\r
302 av.getAlignment().getSequences(),
\r
303 av.getPIDWindow(),x1,x2);
\r
307 /* if (y2 > starty && y1 < av.getEndSeq())
\r
311 (x1-startx)*charWidth,
\r
312 offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),
\r
313 (x2-x1+1)*charWidth,
\r
314 offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));
\r
319 /// First draw the sequences
\r
320 /////////////////////////////
\r
321 for (int i = y1 ; i < y2 ;i++)
\r
323 nextSeq = av.alignment.getSequenceAt(i);
\r
325 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,
\r
326 (x1 - startx) * av.charWidth,
\r
327 offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
328 av.charWidth,av.charHeight,null, i);
\r
330 if(av.showSequenceFeatures)
\r
332 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,
\r
333 (x1 - startx) * av.charWidth,
\r
335 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
336 av.charWidth, av.charHeight, null, i);
\r
340 /////////////////////////////////////
\r
342 // Now outline any areas if necessary
\r
343 /////////////////////////////////////
\r
344 SequenceGroup group = av.getSelectionGroup();
\r
345 java.util.Vector groups = av.alignment.getGroups();
\r
347 int sx = -1, sy = -1, ex = -1;
\r
348 int groupIndex = -1;
\r
349 if (group == null && groups.size() > 0)
\r
351 group = (SequenceGroup) groups.elementAt(0);
\r
360 boolean inGroup = false;
\r
361 int top=-1, bottom =-1;
\r
362 for (i = y1; i < y2; i++)
\r
364 sx = (group.getStartRes() - startx) * av.charWidth;
\r
365 sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
366 ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth;
\r
368 if (av.getWrapAlignment())
\r
373 if (ex > getWidth())
\r
375 if (av.getWrapAlignment())
\r
376 ex = getWrappedCanvasWidth(getWidth()) * av.charWidth;
\r
380 else if(ex>getWidth())
\r
381 ex -= av.charWidth;
\r
384 if (sx < getWidth()
\r
386 && group.sequences.contains(av.alignment.getSequenceAt(i)))
\r
388 if (bottom == -1 &&
\r
389 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))
\r
390 bottom = sy + av.charHeight ;
\r
394 if (top == -1 && i==0 ||
\r
395 !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))
\r
401 if (group == av.getSelectionGroup())
\r
403 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));
\r
404 g.setColor(Color.RED);
\r
408 g.setStroke(new BasicStroke());
\r
409 g.setColor(group.getOutlineColour());
\r
417 g.drawLine(sx, oldY, sx, sy );
\r
418 g.drawLine(sx+ex, oldY, sx+ex, sy );
\r
422 g.drawLine(sx, top, sx + ex, top);
\r
427 g.drawLine(sx, bottom, sx + ex, bottom);
\r
442 g.drawLine(sx, top, sx + ex, top);
\r
447 g.drawLine(sx, bottom-1, sx + ex, bottom-1);
\r
451 sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
452 g.drawLine(sx, oldY, sx, sy );
\r
453 g.drawLine(sx+ex, oldY, sx+ex, sy );
\r
457 if (groupIndex >= groups.size())
\r
460 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
463 while (groupIndex < groups.size());
\r
466 /// Highlight search Results once all sequences have been drawn
\r
467 //////////////////////////////////////////////////////////
\r
470 for(int r=0; r<searchResults.length; r+=3)
\r
472 int searchSeq = searchResults[r];
\r
473 int searchStart = searchResults[r+1];
\r
474 int searchEnd = searchResults[r+2];
\r
476 if (searchSeq >= y1 && searchSeq < y2)
\r
478 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
483 ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),
\r
486 (searchStart - startx) * av.charWidth,
\r
488 AlignmentUtil.getPixelHeight(starty, searchSeq,
\r
500 public void highlightSearchResults(int [] results)
\r
502 // results are in the order sequence, startRes, endRes
\r
504 displaySearch = false;
\r
506 displaySearch = true;
\r
508 searchResults = results;
\r