JAL-3253-applet JAL-3383
[jalview.git] / src / jalview / gui / SeqCanvas.java
index 0d27354..2d83e9e 100755 (executable)
@@ -375,15 +375,15 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     int charHeight = av.getCharHeight();
     int charWidth = av.getCharWidth();
 
-    int width = getWidth();
-    int height = getHeight();
+    int availWidth = getWidth();
+    int availHeight = getHeight();
 
-    width -= (width % charWidth);
-    height -= (height % charHeight);
+    availWidth -= (availWidth % charWidth);
+    availHeight -= (availHeight % charHeight);
 
     // BH 2019 can't possibly fastPaint if either width or height is 0
 
-    if (width == 0 || height == 0)
+    if (availWidth == 0 || availHeight == 0)
     {
       return;
     }
@@ -433,10 +433,11 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       // img is a cached version of the last view we drew.
       // If we have no img or the size has changed, make a new one.
       //
-      if (img == null || width != img.getWidth()
-              || height != img.getHeight())
+      if (img == null || availWidth != img.getWidth()
+              || availHeight != img.getHeight())
       {
-        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        img = new BufferedImage(availWidth, availHeight,
+                BufferedImage.TYPE_INT_RGB);
       }
 
       Graphics2D gg = (Graphics2D) img.getGraphics();
@@ -449,11 +450,11 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       }
 
       gg.setColor(Color.white);
-      gg.fillRect(0, 0, img.getWidth(), img.getHeight());
+      gg.fillRect(0, 0, availWidth, availHeight);
 
       if (av.getWrapAlignment())
       {
-        drawWrappedPanel(gg, width, height, ranges.getStartRes());
+        drawWrappedPanel(gg, availWidth, availHeight, ranges.getStartRes());
       }
       else
       {
@@ -521,32 +522,31 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
   }
 
   /**
-   * Returns the visible width of the canvas in residues, after allowing for
-   * East or West scales (if shown)
+   * Using the current font, determine fields labelWidthEast and labelWidthWest,
+   * and return the number of residues that can fill the remaining width.
    * 
-   * @param canvasWidth
+   * @param width
    *          the width in pixels (possibly including scales)
    * 
-   * @return
+   * @return the visible width in residues, after allowing for East or West
+   *         scales (if shown)
+   * 
    */
-  public int getWrappedCanvasWidth(int canvasWidth)
+  public int getWrappedCanvasWidth(int width)
   {
     int charWidth = av.getCharWidth();
 
     FontMetrics fm = getFontMetrics(av.getFont());
 
-    int labelWidth = 0;
-    
-    if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
-    {
-      labelWidth = getLabelWidth(fm);
-    }
+    int labelWidth = (av.getScaleRightWrapped() || av.getScaleLeftWrapped()
+            ? getLabelWidth(fm)
+            : 0);
 
     labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
 
     labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
 
-    return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
+    return (width - labelWidthEast - labelWidthWest) / charWidth;
   }
 
   /**
@@ -572,6 +572,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
     }
 
+    // quick int log10
     int length = 0;
     for (int i = maxWidth; i > 0; i /= 10)
     {
@@ -586,18 +587,18 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
    * window
    * 
    * @param g
-   * @param canvasWidth
+   * @param availWidth
    *          available width in pixels
-   * @param canvasHeight
+   * @param availHeight
    *          available height in pixels
    * @param startColumn
    *          the first column (0...) of the alignment to draw
    */
-  public void drawWrappedPanel(Graphics g, int canvasWidth,
-          int canvasHeight, final int startColumn)
+  public void drawWrappedPanel(Graphics g, int availWidth, int availHeight,
+          final int startColumn)
   {
-    int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
-            canvasHeight);
+    int wrappedWidthInResidues = calculateWrappedGeometry(availWidth,
+            availHeight);
 
     av.setWrappedWidth(wrappedWidthInResidues);
 
@@ -607,7 +608,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     // we need to call this again to make sure the startColumn +
     // wrappedWidthInResidues values are used to calculate wrappedVisibleWidths
     // correctly.
-    calculateWrappedGeometry(canvasWidth, canvasHeight);
+    calculateWrappedGeometry(availWidth, availHeight);
 
     /*
      * draw one width at a time (excluding any scales shown),
@@ -622,7 +623,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     {
       int endColumn = Math
               .min(maxWidth, start + wrappedWidthInResidues - 1);
-      drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
+      drawWrappedWidth(g, ypos, start, endColumn, availHeight);
       ypos += wrappedRepeatHeightPx;
       start += wrappedWidthInResidues;
       currentWidth++;
@@ -641,11 +642,11 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
    * <li>whether scales are shown left, right or above the alignment</li>
    * </ul>
    * 
-   * @param canvasWidth
-   * @param canvasHeight
+   * @param availWidth
+   * @param availHeight
    * @return the number of residue columns in each width
    */
-  protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
+  protected int calculateWrappedGeometry(int availWidth, int availHeight)
   {
     int charHeight = av.getCharHeight();
 
@@ -679,8 +680,8 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
      * ensuring a part height includes at least one sequence
      */
     ViewportRanges ranges = av.getRanges();
-    wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
-    int remainder = canvasHeight % wrappedRepeatHeightPx;
+    wrappedVisibleWidths = availHeight / wrappedRepeatHeightPx;
+    int remainder = availHeight % wrappedRepeatHeightPx;
     if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
     {
       wrappedVisibleWidths++;
@@ -689,7 +690,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     /*
      * compute width in residues; this also sets East and West label widths
      */
-    int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
+    int wrappedWidthInResidues = getWrappedCanvasWidth(availWidth);
 
     /*
      *  limit visibleWidths to not exceed width of alignment