add 1 to fastPaint, in case seqs of different length
[jalview.git] / src / jalview / gui / SeqCanvas.java
index 6dc2acd..f3e7532 100755 (executable)
@@ -1,15 +1,17 @@
 package jalview.gui;\r
 \r
 import java.awt.*;\r
+import java.awt.image.*;\r
 import javax.swing.*;\r
 import jalview.datamodel.*;\r
 import jalview.analysis.*;\r
 \r
 \r
-public class SeqCanvas extends JPanel\r
+public class SeqCanvas extends JComponent\r
 {\r
      FeatureRenderer fr;\r
-    Image             img;\r
+     SequenceRenderer sr;\r
+    BufferedImage             img;\r
     Graphics2D          gg;\r
     int               imgWidth;\r
     int               imgHeight;\r
@@ -30,6 +32,7 @@ public class SeqCanvas extends JPanel
     {\r
        this.av         = av;\r
        fr = new FeatureRenderer(av);\r
+       sr = new SequenceRenderer(av);\r
        setLayout(new BorderLayout());\r
        PaintRefresher.Register(this);\r
 \r
@@ -77,7 +80,7 @@ public class SeqCanvas extends JPanel
       }\r
       if(value!=-1)\r
       {\r
-        int x = fm.stringWidth("000") - fm.stringWidth(value+"");\r
+        int x = LABEL_WEST - fm.stringWidth(value+"");\r
         g.drawString(value + "", x,  ypos +  i*av.charHeight - av.charHeight/5);\r
       }\r
     }\r
@@ -121,7 +124,7 @@ public void fastPaint(int horizontal, int vertical)
     gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,\r
                 -vertical * av.charHeight);\r
 \r
-    int sr = av.startRes, er = av.endRes + 1, ss = av.startSeq, es = av.endSeq,\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
@@ -133,16 +136,24 @@ public void fastPaint(int horizontal, int vertical)
 \r
     else if (vertical > 0) // scroll down\r
     {\r
-      transY = imgHeight - vertical * av.charHeight;\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)*av.charWidth, (es-ss)*av.charHeight);\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
@@ -162,7 +173,7 @@ public void fastPaint(int horizontal, int vertical)
  * in the adjustment listener in SeqPanel when the scrollbars move.\r
  */\r
 \r
-  public void paintComponent(Graphics g)\r
+  public void paint(Graphics g)\r
   {\r
     g.setColor(Color.white);\r
     g.fillRect(0, 0, getWidth(), getHeight());\r
@@ -183,7 +194,7 @@ public void fastPaint(int horizontal, int vertical)
       imgWidth -= imgWidth%av.charWidth;\r
       imgHeight-= imgHeight%av.charHeight;\r
 \r
-      img = createImage(imgWidth,imgHeight);\r
+      img = new BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);\r
       gg  = (Graphics2D)img.getGraphics();\r
       gg.setFont(av.getFont());\r
       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
@@ -207,16 +218,19 @@ public void fastPaint(int horizontal, int vertical)
 \r
   }\r
 \r
+  int LABEL_WEST, LABEL_EAST;\r
   public int getWrappedCanvasWidth(int cwidth)\r
   {\r
     FontMetrics fm = getFontMetrics(av.getFont());\r
 \r
-    int LABEL_EAST = 0;\r
+    LABEL_EAST = 0;\r
+    LABEL_WEST = 0;\r
+\r
     if(av.scaleRightWrapped)\r
       LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );\r
-    int LABEL_WEST = 0;\r
+\r
     if(av.scaleLeftWrapped)\r
-      LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"0" );\r
+      LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"" );\r
 \r
     return  (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;\r
   }\r
@@ -232,7 +246,8 @@ public void fastPaint(int horizontal, int vertical)
         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
+        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
@@ -240,8 +255,7 @@ public void fastPaint(int horizontal, int vertical)
       int  endx   = startRes+cWidth-1;\r
       int  ypos  = 2*av.charHeight;\r
 \r
-\r
-      while (ypos <= canvasHeight && endx <al.getWidth())\r
+      while (ypos <= canvasHeight && startRes<av.alignment.getWidth() )\r
       {\r
         g.setColor(Color.black);\r
 \r
@@ -255,12 +269,25 @@ public void fastPaint(int horizontal, int vertical)
           g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);\r
         }\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
@@ -274,37 +301,12 @@ public void fastPaint(int horizontal, int vertical)
   }\r
 \r
 \r
-  public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\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
-    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
 \r
@@ -317,7 +319,7 @@ public void fastPaint(int horizontal, int vertical)
      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,null, i);\r
+                 av.charWidth,av.charHeight);\r
 \r
      if(av.showSequenceFeatures)\r
      {\r
@@ -325,7 +327,7 @@ public void fastPaint(int horizontal, int vertical)
                        (x1 - startx) * av.charWidth,\r
                        offset +\r
                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
-                       av.charWidth, av.charHeight, null, i);\r
+                       av.charWidth, av.charHeight);\r
      }\r
     }\r
     //\r
@@ -355,23 +357,7 @@ public void fastPaint(int horizontal, int vertical)
         {\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;\r
-\r
-          if (av.getWrapAlignment())\r
-          {\r
-            if (sx < 0)\r
-              sx = 0;\r
-\r
-            if (ex > getWidth())\r
-            {\r
-              if (av.getWrapAlignment())\r
-                ex = getWrappedCanvasWidth(getWidth()) * av.charWidth;\r
-\r
-            }\r
-          }\r
-          else if(ex>getWidth())\r
-            ex -= av.charWidth;\r
-\r
+          ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth -1;\r
 \r
           if (sx < getWidth()\r
               && ex > 0\r