package jalview.gui;\r
\r
import java.awt.*;\r
+import java.awt.image.*;\r
import javax.swing.*;\r
-import java.awt.event.*;\r
import jalview.datamodel.*;\r
-import jalview.schemes.*;\r
import jalview.analysis.*;\r
\r
\r
-public class SeqCanvas extends JPanel\r
+public class SeqCanvas extends JComponent\r
{\r
- Image img;\r
- Graphics gg;\r
+ FeatureRenderer fr;\r
+ SequenceRenderer sr;\r
+ BufferedImage img;\r
+ Graphics2D gg;\r
int imgWidth;\r
int imgHeight;\r
\r
AlignViewport av;\r
\r
- public boolean paintFlag = false;\r
-\r
boolean showScores = false;\r
boolean displaySearch = false;\r
int [] searchResults = null;\r
int chunkHeight;\r
int chunkWidth;\r
\r
+ boolean fastPaint = false;\r
+\r
\r
public SeqCanvas(AlignViewport av)\r
{\r
this.av = av;\r
+ fr = new FeatureRenderer(av);\r
+ sr = new SequenceRenderer(av);\r
setLayout(new BorderLayout());\r
PaintRefresher.Register(this);\r
\r
}\r
\r
- public void drawScale(Graphics g, int startx, int endx,int ypos) {\r
- int scalestartx = startx - startx%10 + 10;\r
+ void drawNorthScale(Graphics g, int startx, int endx,int ypos) {\r
+ int scalestartx = startx - startx % 10 + 10;\r
\r
- g.setColor(Color.black);\r
+ g.setColor(Color.black);\r
+\r
+ // NORTH SCALE\r
+ for (int i = scalestartx; i < endx; i += 10)\r
+ {\r
+ String string = String.valueOf(i);\r
+ g.drawString(string, (i - startx - 1) * av.charWidth,\r
+ ypos - av.charHeight / 2);\r
\r
- for (int i=scalestartx;i < endx;i+= 10)\r
+ g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,\r
+ ypos + 2 - av.charHeight / 2,\r
+ (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);\r
+\r
+ }\r
+ }\r
+\r
+ void drawWestScale(Graphics g, int startx, int endx, int ypos)\r
+ {\r
+ FontMetrics fm = getFontMetrics(av.getFont());\r
+ ypos+= av.charHeight;\r
+ // EAST SCALE\r
+ for (int i = 0; i < av.alignment.getHeight(); i++)\r
+ {\r
+ SequenceI seq = av.alignment.getSequenceAt(i);\r
+ int index = startx;\r
+ int value = -1;\r
+ while (index < endx)\r
{\r
- String string = String.valueOf(i);\r
- g.drawString(string,(i-startx-1)*av.charWidth,ypos - av.charHeight/2);\r
+ if (jalview.util.Comparison.isGap(seq.getCharAt(index)))\r
+ {\r
+ index++;\r
+ continue;\r
+ }\r
\r
- g.drawLine( (i-startx-1)*av.charWidth +av.charWidth/2, ypos+2 - av.charHeight/2,\r
- (i-startx-1)*av.charWidth +av.charWidth/2, ypos-2 );\r
+ value = av.alignment.getSequenceAt(i).findPosition(index);\r
+ break;\r
+ }\r
+ if(value!=-1)\r
+ {\r
+ int x = LABEL_WEST - fm.stringWidth(value+"");\r
+ g.drawString(value + "", x, ypos + i*av.charHeight - av.charHeight/5);\r
+ }\r
+ }\r
+ }\r
\r
+ void drawEastScale(Graphics g, int startx, int endx, int ypos)\r
+{\r
+ ypos+= av.charHeight;\r
+ // EAST SCALE\r
+ for (int i = 0; i < av.alignment.getHeight(); i++)\r
+ {\r
+ SequenceI seq = av.alignment.getSequenceAt(i);\r
+ int index = endx;\r
+ int value = -1;\r
+ while (index > startx)\r
+ {\r
+ if (jalview.util.Comparison.isGap(seq.getCharAt(index)))\r
+ {\r
+ index--;\r
+ continue;\r
}\r
\r
+ value = av.alignment.getSequenceAt(i).findPosition(index);\r
+ break;\r
+ }\r
+ if(value!=-1)\r
+ g.drawString(value + "", 0, ypos + i*av.charHeight - av.charHeight/5);\r
}\r
\r
+}\r
+\r
+\r
+\r
+\r
+\r
+public void fastPaint(int horizontal, int vertical)\r
+{\r
+ if (horizontal == 0 && vertical == 0 || gg==null)\r
+ return;\r
+\r
+ gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
+ -vertical * av.charHeight);\r
+\r
+ int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,\r
+ transX = 0, transY = 0;\r
+ if (horizontal > 0) // scrollbar pulled right, image to the left\r
+ {\r
+ transX = (er - sr - horizontal) * av.charWidth;\r
+ sr = er - horizontal;\r
+ }\r
+ else if (horizontal < 0)\r
+ er = sr - horizontal;\r
+\r
+ else if (vertical > 0) // scroll down\r
+ {\r
+ ss = es - vertical;\r
+ if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
+ ss = av.startSeq;\r
+ else\r
+ transY = imgHeight - vertical * av.charHeight;\r
+ }\r
+ else if (vertical < 0)\r
+ {\r
+ es = ss - vertical;\r
+ if(es > av.endSeq)\r
+ es = av.endSeq;\r
+ }\r
+\r
+\r
+ gg.translate(transX, transY);\r
+\r
+ gg.setColor(Color.white);\r
+ gg.fillRect(0,0, (er-sr+1)*av.charWidth, (es-ss)*av.charHeight);\r
+ drawPanel(gg, sr, er, ss, es, sr, ss, 0);\r
+ gg.translate( -transX, -transY);\r
+\r
+ fastPaint = true;\r
+ repaint();\r
+\r
+}\r
\r
/**\r
* Definitions of startx and endx (hopefully):\r
\r
public void paintComponent(Graphics g)\r
{\r
+ g.setColor(Color.white);\r
+ g.fillRect(0, 0, getWidth(), getHeight());\r
\r
- if (img==null || imgWidth!=getWidth() || imgHeight!=getHeight() || paintFlag)\r
+ if (fastPaint)\r
{\r
+ g.drawImage(img, 0, 0, this);\r
+ fastPaint = false;\r
+ return;\r
+ }\r
+\r
+ // this draws the whole of the alignment\r
imgWidth = getWidth();\r
imgHeight = getHeight();\r
- img = createImage(imgWidth,imgHeight);\r
- gg = img.getGraphics();\r
+\r
+ imgWidth -= imgWidth%av.charWidth;\r
+ imgHeight-= imgHeight%av.charHeight;\r
+\r
+ if(imgWidth<1 || imgHeight<1)\r
+ return;\r
+\r
+ img = new BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);\r
+ gg = (Graphics2D)img.getGraphics();\r
gg.setFont(av.getFont());\r
- paintFlag = false;\r
- }\r
+ gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
\r
+ gg.setColor(Color.white);\r
+ gg.fillRect(0,0,imgWidth,imgHeight);\r
\r
- chunkWidth = getWidth()/av.charWidth;\r
+ chunkWidth = getWrappedCanvasWidth( getWidth() );\r
chunkHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;\r
\r
av.setChunkHeight(chunkHeight);\r
av.setChunkWidth(chunkWidth);\r
\r
- fillBackground(gg,Color.WHITE,0,0,imgWidth,imgHeight);\r
\r
if (av.getWrapAlignment())\r
drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
else\r
drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
\r
+ g.drawImage(img, 0, 0, this);\r
+\r
+ }\r
+\r
+ int LABEL_WEST, LABEL_EAST;\r
+ public int getWrappedCanvasWidth(int cwidth)\r
+ {\r
+ FontMetrics fm = getFontMetrics(av.getFont());\r
+\r
+ LABEL_EAST = 0;\r
+ LABEL_WEST = 0;\r
+\r
+ if(av.scaleRightWrapped)\r
+ LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );\r
\r
- g.drawImage(img,0,0,this);\r
+ if(av.scaleLeftWrapped)\r
+ LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"" );\r
\r
+ return (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
}\r
\r
public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
{\r
- AlignmentI da = av.getAlignment();\r
+ AlignmentI al = av.getAlignment();\r
\r
- int cWidth = canvasWidth/av.charWidth;\r
+ FontMetrics fm = getFontMetrics(av.getFont());\r
+\r
+ int LABEL_EAST = 0;\r
+ if(av.scaleRightWrapped)\r
+ LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );\r
+ int LABEL_WEST = 0;\r
+ if(av.scaleLeftWrapped)\r
+ LABEL_WEST = fm.stringWidth(al.getWidth()+"0");\r
+\r
+\r
+ int cWidth = (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
int cHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;\r
\r
+ av.endRes = av.startRes + cWidth;\r
+\r
int endx = startRes+cWidth-1;\r
- int ypos = 2*av.charHeight;\r
+ int ypos = 2*av.charHeight;\r
\r
- while (ypos <= canvasHeight)\r
+ while (ypos <= canvasHeight && startRes<av.alignment.getWidth() )\r
{\r
- drawScale(g, startRes, endx, ypos);\r
- drawPanel(g, startRes, endx, 0, da.getHeight(), startRes, 0, ypos);\r
+ g.setColor(Color.black);\r
+\r
+ if(av.scaleLeftWrapped)\r
+ drawWestScale(g, startRes, endx, ypos);\r
+\r
+ if(av.scaleRightWrapped)\r
+ {\r
+ g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);\r
+ drawEastScale(g, startRes, endx, ypos);\r
+ g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
+ }\r
+\r
+ g.translate(LABEL_WEST,0);\r
+ if(av.scaleAboveWrapped)\r
+ drawNorthScale(g, startRes, endx, ypos);\r
+\r
+\r
+ // When printing we have an extra clipped region,\r
+ // the Printable page which we need to account for here\r
+ Shape clip = g.getClip();\r
+ if(clip==null)\r
+ g.setClip(0, 0, cWidth*av.charWidth, canvasHeight);\r
+ else\r
+ g.setClip(0,\r
+ (int)clip.getBounds().getY(),\r
+ cWidth*av.charWidth,\r
+ (int)clip.getBounds().getHeight()\r
+ );\r
+\r
+ drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);\r
+ g.setClip(clip);\r
+ g.translate(-LABEL_WEST,0);\r
\r
ypos += cHeight;\r
startRes += cWidth;\r
endx = startRes + cWidth - 1;\r
\r
- if (endx > da.getWidth())\r
- endx = da.getWidth();\r
+ if (endx > al.getWidth())\r
+ endx = al.getWidth();\r
}\r
\r
}\r
\r
\r
- public void drawPanel(Graphics g,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
-\r
+ synchronized public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
\r
+ Graphics2D g = (Graphics2D)g1;\r
g.setFont(av.getFont());\r
- int charWidth = av.getCharWidth();\r
- int charHeight = av.getCharHeight();\r
- RendererI sr = av.getRenderer();\r
-\r
- /*Vector pid = av.getConsensus(false);\r
- Vector tmpseq = new Vector();\r
- for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
- if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
- tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
-\r
- if (sr instanceof SequenceRenderer)\r
- pid = AAFrequency.calculate(tmpseq,x1,x2);\r
+ sr.renderGaps(av.renderGaps);\r
\r
- else if (sr instanceof GraphRenderer)\r
- pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
- av.getAlignment().getSequences(),\r
- av.getPIDWindow(),x1,x2);\r
-\r
-*/\r
-\r
- /* if (y2 > starty && y1 < av.getEndSeq())\r
- {\r
- fillBackground(g,\r
- Color.red,\r
- (x1-startx)*charWidth,\r
- offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
- (x2-x1+1)*charWidth,\r
- offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
- }*/\r
\r
SequenceI nextSeq;\r
- SequenceGroup group=null;\r
- SequenceGroup [] groups=null;\r
- boolean inGroup=false; // are we in a defined group?\r
- boolean inRB=false; //Are we in a rubberband group?\r
- int sx=-1, sy=-1, ex=-1, ey=-1;\r
- int oldStartX=0,oldEndX=0,oldY=0;\r
- int oldRStartX=0,oldREndX=0,oldRY=0;\r
+\r
+ /// First draw the sequences\r
+ /////////////////////////////\r
for (int i = y1 ; i < y2 ;i++)\r
{\r
- nextSeq = av.getAlignment().getSequenceAt(i);\r
-\r
- group = av.alignment.findGroup( nextSeq );\r
- groups = av.alignment.findAllGroups( nextSeq );\r
+ nextSeq = av.alignment.getSequenceAt(i);\r
\r
- sr.drawSequence(g, nextSeq, groups,x1,x2,\r
- (x1 - startx) * charWidth,\r
- offset + AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight()),\r
- charWidth,charHeight,null, i);\r
+ sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
+ (x1 - startx) * av.charWidth,\r
+ offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
+ av.charWidth,av.charHeight);\r
\r
-\r
- if( group!=null )\r
+ if(av.showSequenceFeatures)\r
{\r
+ fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
+ (x1 - startx) * av.charWidth,\r
+ offset +\r
+ AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
+ av.charWidth, av.charHeight);\r
+ }\r
+ }\r
+ //\r
+ /////////////////////////////////////\r
\r
- g.setColor(Color.lightGray);\r
-\r
- sx = (group.getStartRes()-startx)*charWidth;\r
- sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight());\r
- ex = (group.getEndRes()+1-startx)*charWidth;\r
- ey = offset + AlignmentUtil.getPixelHeight(starty, i+1, av.getCharHeight());\r
+ // Now outline any areas if necessary\r
+ /////////////////////////////////////\r
+ SequenceGroup group = av.getSelectionGroup();\r
+ java.util.Vector groups = av.alignment.getGroups();\r
\r
+ int sx = -1, sy = -1, ex = -1;\r
+ int groupIndex = -1;\r
+ if (group == null && groups.size() > 0)\r
+ {\r
+ group = (SequenceGroup) groups.elementAt(0);\r
+ groupIndex = 0;\r
+ }\r
\r
- if (!inGroup)\r
+ if (group != null)\r
+ do\r
+ {\r
+ int oldY = -1;\r
+ int i = 0;\r
+ boolean inGroup = false;\r
+ int top=-1, bottom =-1;\r
+ for (i = y1; i < y2; i++)\r
{\r
- g.drawLine(sx, sy, ex, sy); // Horizontal, top of new box\r
- inGroup=true;\r
+ sx = (group.getStartRes() - startx) * av.charWidth;\r
+ sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
+ ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth -1;\r
+\r
+ if (sx < getWidth()\r
+ && ex > 0\r
+ && group.sequences.contains(av.alignment.getSequenceAt(i)))\r
+ {\r
+ if (bottom == -1 &&\r
+ !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))\r
+ bottom = sy + av.charHeight ;\r
+\r
+ if (!inGroup)\r
+ {\r
+ if (top == -1 && i==0 ||\r
+ !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))\r
+ top = sy;\r
+\r
+\r
+ oldY = sy;\r
+ inGroup = true;\r
+ if (group == av.getSelectionGroup())\r
+ {\r
+ g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));\r
+ g.setColor(Color.RED);\r
+ }\r
+ else\r
+ {\r
+ g.setStroke(new BasicStroke());\r
+ g.setColor(group.getOutlineColour());\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (inGroup)\r
+ {\r
+ g.drawLine(sx, oldY, sx, sy );\r
+ g.drawLine(sx+ex, oldY, sx+ex, sy );\r
+\r
+ if (top != -1)\r
+ {\r
+ g.drawLine(sx, top, sx + ex, top);\r
+ top =-1;\r
+ }\r
+ if (bottom != -1)\r
+ {\r
+ g.drawLine(sx, bottom, sx + ex, bottom);\r
+ bottom = -1;\r
+ }\r
+\r
+\r
+ inGroup = false;\r
+ }\r
+ }\r
}\r
\r
- g.drawLine(sx, sy, sx, ey ); // vertical line, left hand side\r
- g.drawLine( ex,sy,ex,ey); // vertical line, right hand side\r
- if (i == y2 - 1)// last line of alignment, seal the box\r
- g.drawLine(sx, ey, ex, ey); // Horizontal, bottom of old box\r
+ if (inGroup)\r
+ {\r
\r
- oldStartX = sx;\r
- oldEndX = ex;\r
- oldY = ey;\r
+ if(top!=-1)\r
+ {\r
+ g.drawLine(sx, top, sx + ex, top);\r
+ top =-1;\r
+ }\r
+ if(bottom!=-1)\r
+ {\r
+ g.drawLine(sx, bottom-1, sx + ex, bottom-1);\r
+ bottom = -1;\r
+\r
+ }\r
+ sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
+ g.drawLine(sx, oldY, sx, sy );\r
+ g.drawLine(sx+ex, oldY, sx+ex, sy );\r
+ inGroup = false;\r
+ }\r
+ groupIndex++;\r
+ if (groupIndex >= groups.size())\r
+ break;\r
\r
- }\r
- else if(inGroup)\r
- {\r
- g.setColor(Color.lightGray);\r
- g.drawLine(oldStartX, oldY, oldEndX, oldY); // Horizontal, bottom of old box\r
- }\r
- else\r
- inGroup = false;\r
+ group = (SequenceGroup) groups.elementAt(groupIndex);\r
\r
- group = av.getRubberbandGroup();\r
- if( group != null && group.sequences.contains(nextSeq) )\r
- {\r
- g.setColor(Color.RED.brighter());\r
- sx = (group.getStartRes() - startx) * charWidth;\r
- sy = offset +\r
- AlignmentUtil.getPixelHeight(starty, i, av.getCharHeight());\r
- ex = (group.getEndRes() + 1 - startx) * charWidth;\r
- ey = offset +\r
- AlignmentUtil.getPixelHeight(starty, i + 1, av.getCharHeight());\r
-\r
- if (!inRB)\r
- {\r
-\r
- g.drawLine(sx, sy, ex, sy); // Horizontal, top of new box\r
- inRB = true;\r
- }\r
-\r
- g.drawLine(sx, sy, sx, ey); // vertical line, left hand side\r
- g.drawLine(ex, sy, ex, ey); // vertical line, right hand side\r
- if (i == y2 - 1) // last line of alignment, seal the box\r
- g.drawLine(sx, ey, ex, ey); // Horizontal, bottom of old box\r
-\r
- oldRStartX = sx;\r
- oldREndX = ex;\r
- oldRY = ey;\r
- }\r
- else if(inRB)\r
- {\r
- g.setColor(Color.RED.brighter());\r
- g.drawLine(oldRStartX, oldRY, oldREndX, oldRY); // Horizontal, bottom of old box\r
- inRB = false;\r
- }\r
- else\r
- inRB = false;\r
+ }\r
+ while (groupIndex < groups.size());\r
\r
- }\r
\r
/// Highlight search Results once all sequences have been drawn\r
+ //////////////////////////////////////////////////////////\r
if(displaySearch)\r
{\r
for(int r=0; r<searchResults.length; r+=3)\r
{\r
int searchSeq = searchResults[r];\r
- int searchStart = searchResults[r+1];\r
- int searchEnd = searchResults[r+2];\r
\r
if (searchSeq >= y1 && searchSeq < y2)\r
{\r
+ SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);\r
+\r
+ int searchStart = seq.findIndex( searchResults[r+1] )-1;\r
+ int searchEnd = seq.findIndex( searchResults[r+2] )-1;\r
+\r
SequenceRenderer ssr = (SequenceRenderer) sr;\r
- ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
+ if(searchStart<x1)\r
+ searchStart = x1;\r
+ if(searchEnd > x2)\r
+ searchEnd = x2;\r
+\r
+ ssr.drawHighlightedText(seq,\r
searchStart,\r
searchEnd,\r
- (searchStart - startx) * charWidth,\r
+ (searchStart - startx) * av.charWidth,\r
offset +\r
AlignmentUtil.getPixelHeight(starty, searchSeq,\r
- charHeight),\r
- charWidth,\r
- charHeight);\r
+ av.charHeight),\r
+ av.charWidth,\r
+ av.charHeight);\r
}\r
}\r
}\r
\r
}\r
\r
- public void fillBackground(Graphics g,Color c, int x1,int y1,int width,int height) {\r
- g.setColor(c);\r
- g.fillRect(x1,y1,width,height);\r
- }\r
\r
- public int getChunkWidth() {\r
- return chunkWidth;\r
- }\r
\r
public void highlightSearchResults(int [] results)\r
{\r
\r
searchResults = results;\r
\r
- paintFlag = true;\r
repaint();\r
}\r
\r