JAL-2600 Add in IdCanvas and checks for 0 size images
[jalview.git] / src / jalview / gui / AnnotationPanel.java
index 452f002..3dee5a8 100755 (executable)
@@ -451,7 +451,7 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
      * the selection list (read-only view) is in selection order, not
      * column order; make a copy so we can sort it
      */
-    List<Integer> selected = new ArrayList<Integer>(viscols.getSelected());
+    List<Integer> selected = new ArrayList<>(viscols.getSelected());
     Collections.sort(selected);
     for (int index : selected)
     {
@@ -971,7 +971,7 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
    * @param horizontal
    *          repaint with horizontal shift in alignment
    */
-  public void fastPaint(int horizontal)
+  public void fastPaint(int horizontal, boolean isresize)
   {
     if ((horizontal == 0) || gg == null
             || av.getAlignment().getAlignmentAnnotation() == null
@@ -981,22 +981,49 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
       repaint();
       return;
     }
-    long stime = System.currentTimeMillis();
-    gg.copyArea(0, 0, imgWidth, getHeight(),
-            -horizontal * av.getCharWidth(), 0);
-    long mtime = System.currentTimeMillis();
+
     int sr = av.getRanges().getStartRes();
     int er = av.getRanges().getEndRes() + 1;
     int transX = 0;
+    long stime;
+    long mtime;
 
-    if (horizontal > 0) // scrollbar pulled right, image to the left
+    if (isresize)
     {
-      transX = (er - sr - horizontal) * av.getCharWidth();
-      sr = er - horizontal;
+      imgWidth = (av.getRanges().getEndRes() - av.getRanges().getStartRes()
+              + 1) * av.getCharWidth();
+
+      if (imgWidth > 0)
+      {
+        BufferedImage newimage = new BufferedImage(imgWidth,
+                ap.getAnnotationPanel().getHeight(),
+                BufferedImage.TYPE_INT_ARGB);
+
+        gg = (Graphics2D) newimage.getGraphics();
+        gg.setFont(av.getFont());
+        gg.drawImage(image, null, 0, 0);
+        image = newimage;
+
+        transX = (er - horizontal - sr) * av.getCharWidth();
+        sr = er - horizontal - sr;
+      }
     }
-    else if (horizontal < 0)
+    else
     {
-      er = sr - horizontal;
+      stime = System.currentTimeMillis();
+      gg.copyArea(0, 0, imgWidth, getHeight(),
+              -horizontal * av.getCharWidth(), 0);
+      mtime = System.currentTimeMillis();
+
+      if (horizontal > 0) // scrollbar pulled right, image to the left
+      {
+        transX = (er - sr - horizontal) * av.getCharWidth();
+        sr = er - horizontal;
+      }
+      else if (horizontal < 0)
+      {
+        er = sr - horizontal;
+      }
     }
 
     gg.translate(transX, 0);
@@ -1168,10 +1195,14 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
   public void propertyChange(PropertyChangeEvent evt)
   {
     // Respond to viewport range changes (e.g. alignment panel was scrolled)
-    if (evt.getPropertyName().equals("startres")
-            || evt.getPropertyName().equals("endres"))
+    if (evt.getPropertyName().equals("startres"))
+    {
+      fastPaint((int) evt.getNewValue() - (int) evt.getOldValue(), false);
+    }
+    else if (evt.getPropertyName().equals("endres"))
     {
-      fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
+      // resize
+      fastPaint((int) evt.getNewValue() - (int) evt.getOldValue(), true);
     }
   }
 }