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