Save as jpg added
authoramwaterhouse <Andrew Waterhouse>
Wed, 16 Feb 2005 17:56:33 +0000 (17:56 +0000)
committeramwaterhouse <Andrew Waterhouse>
Wed, 16 Feb 2005 17:56:33 +0000 (17:56 +0000)
src/jalview/gui/AlignFrame.java
src/jalview/gui/AlignmentPanel.java
src/jalview/jbgui/GAlignFrame.java

index 701c209..a20a3fe 100755 (executable)
@@ -92,6 +92,23 @@ public class AlignFrame extends GAlignFrame
     htmlOutput = null;\r
   }\r
 \r
+  protected void createJPG_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+    int height = (viewport.alignment.getWidth() / viewport.getChunkWidth() +1) * viewport.chunkHeight;\r
+    int width = alignPanel.seqPanel.getWidth() + alignPanel.idPanel.getWidth();\r
+\r
+\r
+    if(!viewport.getWrapAlignment())\r
+    {\r
+      height = viewport.alignment.getHeight() * viewport.charHeight;\r
+      width = alignPanel.idPanel.getWidth() + viewport.alignment.getWidth() * viewport.charWidth;\r
+    }\r
+\r
+    alignPanel.makeJPG( width, height);\r
+  }\r
+\r
+\r
   public void printMenuItem_actionPerformed(ActionEvent e)\r
   {\r
     //Putting in a thread avoids Swing painting problems\r
index 3ce9fa1..96f4d4a 100755 (executable)
@@ -8,6 +8,11 @@ import java.awt.*;
 import java.awt.event.*;\r
 import java.awt.print.*;\r
 \r
+import com.sun.image.codec.jpeg.*;\r
+import java.io.*;\r
+import java.awt.image.*;\r
+\r
+\r
 public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListener, Printable\r
 {\r
 \r
@@ -269,19 +274,22 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene
     repaint();\r
   }\r
 \r
-\r
-\r
   public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException\r
   {\r
-    if(av.getWrapAlignment())\r
-    {\r
-     return PrintWrappedAlignment(pg, pf, pi);\r
-    }\r
-\r
     pg.translate((int)pf.getImageableX(), (int)pf.getImageableY());\r
 \r
-    int pwidth = (int)pf.getImageableWidth();\r
-    int pheight = (int)pf.getImageableHeight();\r
+    int pwidth = (int) pf.getImageableWidth();\r
+    int pheight = (int) pf.getImageableHeight();\r
+\r
+    if (av.getWrapAlignment())\r
+      return printWrappedAlignment(pg, pwidth,pheight, pi);\r
+    else\r
+      return printUnwrapped(pg,pwidth, pheight,pi);\r
+  }\r
+\r
+  public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi) throws PrinterException\r
+  {\r
+\r
     int idWidth = (int)idPanel.idCanvas.getLabelWidth().getWidth();\r
 \r
 \r
@@ -356,33 +364,16 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene
     return Printable.PAGE_EXISTS;\r
   }\r
 \r
-  class VPanel extends javax.swing.JPanel\r
-  {\r
-    Image image;\r
-    public VPanel(Image i)\r
-    {\r
-      image = i;\r
-      repaint();\r
-    }\r
-    public void paintComponent(Graphics g)\r
-    {\r
-      if(image!=null)\r
-        g.drawImage(image,0,0,this);\r
-    }\r
 \r
-  }\r
-\r
-  public int PrintWrappedAlignment(Graphics pg, PageFormat pf, int pi)\r
+  public int printWrappedAlignment(Graphics pg, int pwidth, int pheight, int pi) throws PrinterException\r
   {\r
-    pg.translate((int)pf.getImageableX(), (int)pf.getImageableY());\r
 \r
-    int pwidth = (int)pf.getImageableWidth();\r
-    int pheight = (int)pf.getImageableHeight();\r
     int idWidth = (int)idPanel.idCanvas.getLabelWidth().getWidth();\r
 \r
     if( seqPanel.seqCanvas.getWidth() < pwidth-idWidth)\r
       pwidth = seqPanel.seqCanvas.getWidth() + idWidth;\r
 \r
+\r
     pg.setColor(Color.white);\r
     pg.fillRect(0,0,pwidth, pheight);\r
     pg.setFont( av.getFont() );\r
@@ -395,7 +386,7 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene
     int chunkWidth  =   (pwidth-idWidth)/av.charWidth;\r
 \r
     int noChunksOnPage = pheight / chunkHeight;\r
-    int totalChunks = endy / chunkHeight;\r
+    int totalChunks = da.getWidth() / chunkWidth;\r
 \r
     if ( pi*noChunksOnPage > totalChunks )\r
      return Printable.NO_SUCH_PAGE;\r
@@ -424,9 +415,54 @@ public class AlignmentPanel extends GAlignmentPanel implements AdjustmentListene
     seqPanel.seqCanvas.drawWrappedPanel(pg, pwidth-idWidth, pheight, pi*noChunksOnPage*chunkWidth);\r
 \r
 \r
-    return Printable.NO_SUCH_PAGE;//.PAGE_EXISTS;\r
+    return Printable.PAGE_EXISTS;\r
 \r
   }\r
+\r
+  public void makeJPG(int width, int height)\r
+  {\r
+    try\r
+    {\r
+\r
+      BufferedImage bi = new BufferedImage(width, height,\r
+                                           BufferedImage.TYPE_INT_RGB);\r
+      Graphics pg = bi.getGraphics();\r
+\r
+      try\r
+      {\r
+        if (av.getWrapAlignment())\r
+          printWrappedAlignment(pg, width, height, 0);\r
+        else\r
+          printUnwrapped(pg, width, height, 0);\r
+      }\r
+      catch (Exception ex)\r
+      {}\r
+\r
+      javax.swing.JFileChooser chooser = new javax.swing.JFileChooser(jalview.bin.Cache.getProperty(\r
+          "LAST_DIRECTORY"));\r
+      chooser.setDialogTitle("Create JPG image from alignment");\r
+      chooser.setToolTipText("Save");\r
+\r
+      int value = chooser.showSaveDialog(this);\r
+      if (value == javax.swing.JFileChooser.APPROVE_OPTION)\r
+      {\r
+\r
+        FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
+        JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(out);\r
+        JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(bi);\r
+        param.setQuality(1.0f, true);\r
+\r
+        jie.encode(bi, param);\r
+        out.close();\r
+      }\r
+\r
+    }\r
+    catch (IOException ex)\r
+    {\r
+      System.out.println("ex Writing image ");\r
+    }\r
+  }\r
+\r
 }\r
 \r
 \r
index 093c233..39492af 100755 (executable)
@@ -90,6 +90,7 @@ public class GAlignFrame extends JInternalFrame
   JMenuItem pasteNew = new JMenuItem();\r
   JMenuItem pasteThis = new JMenuItem();\r
   protected JCheckBoxMenuItem applyToAllGroups = new JCheckBoxMenuItem();\r
+  JMenuItem createJPG = new JMenuItem();\r
   public GAlignFrame()\r
   {\r
 \r
@@ -166,6 +167,7 @@ public class GAlignFrame extends JInternalFrame
     colourMenu.setText("Colour");\r
     calculateMenu.setText("Calculate");\r
     alignMenu.setText("Web Service");\r
+    groupsMenuItem.setEnabled(false);\r
     groupsMenuItem.setText("Groups");\r
     groupsMenuItem.addActionListener(new java.awt.event.ActionListener()\r
     {\r
@@ -680,6 +682,14 @@ public class GAlignFrame extends JInternalFrame
         applyToAllGroups_actionPerformed(e);\r
       }\r
     });\r
+    createJPG.addActionListener(new java.awt.event.ActionListener()\r
+    {\r
+      public void actionPerformed(ActionEvent e)\r
+      {\r
+        createJPG_actionPerformed(e);\r
+      }\r
+    });\r
+    createJPG.setText("Save as JPG Image");\r
     alignFrameMenuBar.add(fileMenu);\r
     alignFrameMenuBar.add(editMenu);\r
     alignFrameMenuBar.add(searchMenu);\r
@@ -691,6 +701,7 @@ public class GAlignFrame extends JInternalFrame
     fileMenu.add(saveAlignmentMenu);\r
     fileMenu.add(outputTextboxMenu);\r
     fileMenu.add(htmlMenuItem);\r
+    fileMenu.add(createJPG);\r
     fileMenu.add(printMenuItem);\r
     fileMenu.addSeparator();\r
     fileMenu.add(closeMenuItem);\r
@@ -1107,6 +1118,11 @@ public class GAlignFrame extends JInternalFrame
 \r
   }\r
 \r
+  protected void createJPG_actionPerformed(ActionEvent e)\r
+  {\r
+\r
+  }\r
+\r
 \r
 \r
 }\r