Merge remote-tracking branch 'origin/bug/JAL-2722' into portforward/JAL-2675_2102b1to2103
[jalview.git] / src / jalview / gui / SeqCanvas.java
index 04ffc09..191a7d8 100755 (executable)
@@ -426,15 +426,66 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    * @param endSeq
    *          end sequence of print area
    */
-  public void drawPanelForPrint(Graphics g1, int startRes, int endRes,
+  public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
           int startSeq, int endSeq)
   {
+    drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
+
     BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
             startSeq, endSeq);
-    drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
-    ((Graphics2D) g1).setComposite(
-            AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
-    g1.drawImage(selectImage, 0, 0, this);
+    if (selectImage != null)
+    {
+      ((Graphics2D) g1).setComposite(AlphaComposite
+              .getInstance(AlphaComposite.SRC_OVER));
+      g1.drawImage(selectImage, 0, 0, this);
+    }
+  }
+
+  /**
+   * Draw a wrapped alignment panel for printing
+   * 
+   * @param g
+   *          Graphics object to draw with
+   * @param canvasWidth
+   *          width of drawing area
+   * @param canvasHeight
+   *          height of drawing area
+   * @param startRes
+   *          start residue of print area
+   */
+  public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
+          int canvasHeight, int startRes)
+  {
+    SequenceGroup group = av.getSelectionGroup();
+
+    drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
+
+    if (group != null)
+    {
+      BufferedImage selectImage = null;
+      try
+      {
+        selectImage = new BufferedImage(canvasWidth, canvasHeight,
+                BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
+      } catch (OutOfMemoryError er)
+      {
+        System.gc();
+        System.err.println("Print image OutOfMemory Error.\n" + er);
+        new OOMWarning("Creating wrapped alignment image for printing", er);
+      }
+      if (selectImage != null)
+      {
+        Graphics2D g2 = selectImage.createGraphics();
+        setupSelectionGroup(g2, selectImage);
+        drawWrappedSelection(g2, group, canvasWidth, canvasHeight,
+                startRes);
+
+        g2.setComposite(
+                AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
+        g.drawImage(selectImage, 0, 0, this);
+        g2.dispose();
+      }
+    }
   }
 
   /*
@@ -568,7 +619,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    * @param startRes
    *          DOCUMENT ME!
    */
-  public void drawWrappedPanel(Graphics g, int canvasWidth,
+  private void drawWrappedPanel(Graphics g, int canvasWidth,
           int canvasHeight, int startRes)
   {
     updateViewport();
@@ -776,7 +827,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     return annotations.adjustPanelHeight();
   }
 
-
   /**
    * Draws the visible region of the alignment on the graphics context. If there
    * are hidden column markers in the visible region, then each sub-region
@@ -796,9 +846,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    * @param yOffset
    *          vertical offset at which to draw (for wrapped alignments)
    */
-  private void drawPanel(Graphics g1, int startRes, int endRes,
-          int startSeq, int endSeq, int yOffset)
-
+  public void drawPanel(Graphics g1, final int startRes, final int endRes,
+          final int startSeq, final int endSeq, final int yOffset)
   {
     updateViewport();
     if (!av.hasHiddenColumns())
@@ -872,7 +921,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
   }
 
-
   /**
    * Draws a region of the visible alignment
    * 
@@ -979,7 +1027,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       {
         drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
                 endSeq, offset);
-        
+
         groupIndex++;
 
         g.setStroke(new BasicStroke());
@@ -1022,16 +1070,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     // set up drawing colour
     Graphics2D g = (Graphics2D) selectionImage.getGraphics();
 
-    // set background to transparent
-    g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
-    g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
-
-    // set up foreground to draw red dashed line
-    g.setComposite(AlphaComposite.Src);
-    g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
-            BasicStroke.JOIN_ROUND, 3f, new float[]
-    { 5f, 3f }, 0f));
-    g.setColor(Color.RED);
+    setupSelectionGroup(g, selectionImage);
 
     if (!av.getWrapAlignment())
     {
@@ -1049,6 +1088,24 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
   }
 
   /*
+   * Set up graphics for selection group
+   */
+  private void setupSelectionGroup(Graphics2D g,
+          BufferedImage selectionImage)
+  {
+    // set background to transparent
+    g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
+    g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
+
+    // set up foreground to draw red dashed line
+    g.setComposite(AlphaComposite.Src);
+    g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
+            BasicStroke.JOIN_ROUND, 3f, new float[]
+    { 5f, 3f }, 0f));
+    g.setColor(Color.RED);
+  }
+
+  /*
    * Draw a selection group over an unwrapped alignment
    * @param g graphics object to draw with
    * @param group selection group