From ee2fab02e829035b825cea4046266a6994978b96 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Fri, 18 Mar 2005 18:26:21 +0000 Subject: [PATCH] save eps and png --- src/jalview/gui/TreeCanvas.java | 1 - src/jalview/gui/TreePanel.java | 259 ++++++++++----------------------------- 2 files changed, 67 insertions(+), 193 deletions(-) diff --git a/src/jalview/gui/TreeCanvas.java b/src/jalview/gui/TreeCanvas.java index ed9677a..9435716 100755 --- a/src/jalview/gui/TreeCanvas.java +++ b/src/jalview/gui/TreeCanvas.java @@ -292,7 +292,6 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable, Print public int print(Graphics pg, PageFormat pf, int pi) throws PrinterException { - font = new Font("Verdana",Font.PLAIN,fontSize); pg.setFont(font); FontMetrics fm = pg.getFontMetrics(font); diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index 73ef46b..e58615e 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -11,6 +11,10 @@ import jalview.util.*; import javax.swing.*; import java.awt.print.*; import java.awt.*; +import java.awt.image.*; +import org.jibble.epsgraphics.*; +import javax.imageio.*; + @@ -25,15 +29,6 @@ public class TreePanel extends GTreePanel TreeCanvas treeCanvas; NJTree tree; - FileProperties fp; - PostscriptProperties pp; - - PrintWriter bw; - PrintStream ps; - boolean makeString = false; - StringBuffer out; - - public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e) { @@ -57,8 +52,6 @@ public class TreePanel extends GTreePanel tree = new NJTree(seq, type, pwtype, start, end); av.setCurrentTree(tree); - propertiesInit(); - treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); tree.reCount(tree.getTopNode()); @@ -89,7 +82,6 @@ public class TreePanel extends GTreePanel tree = new NJTree(seq, newtree); av.setCurrentTree(tree); - propertiesInit(); tree.reCount(tree.getTopNode()); tree.findHeight(tree.getTopNode()); @@ -100,191 +92,11 @@ public class TreePanel extends GTreePanel } -public void drawPostscript(PostscriptProperties pp) { - try { - int width = 0; - int height = 0; - - printout("%!\n"); - - printout("/" + pp.font + " findfont\n"); - printout(pp.fsize + " scalefont setfont\n"); - - int offx = pp.xoffset; - int offy = pp.yoffset; - - if (pp.orientation == PostscriptProperties.PORTRAIT) { - width = PostscriptProperties.SHORTSIDE; - height = PostscriptProperties.LONGSIDE; - } else { - height = PostscriptProperties.SHORTSIDE; - width = PostscriptProperties.LONGSIDE; - printout(height + " 0 translate\n90 rotate\n"); - } - float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight(); - SequenceNode top = tree.getTopNode(); - - if (top.count == 0) { - top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ; - } - - float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1); - - drawPostscriptNode(top,chunk,wscale,width,offx,offy); - - printout("showpage\n"); - } catch (java.io.IOException e) { - System.out.println("Exception " + e); - } -} - -public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException { - if (node == null) { - return; - } - - if (node.left() == null && node.right() == null) { - // Drawing leaf node - - float height = node.height; - float dist = node.dist; - - int xstart = (int)((height-dist)*scale) + offx; - int xend = (int)(height*scale) + offx; - - int ypos = (int)(node.ycount * chunk) + offy; - - // g.setColor(Color.black); - printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + - new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + - new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); - - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n"); - - if (treeCanvas.showDistances && node.dist > 0) { - // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); - printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n"); - } - //g.drawString((String)node.element(),xend+20,ypos); - printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n"); - } else { - drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy); - drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy); - - - float height = node.height; - float dist = node.dist; - - int xstart = (int)((height-dist)*scale) + offx; - int xend = (int)(height*scale) + offx; - int ypos = (int)(node.ycount * chunk) + offy; - - printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + - new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + - new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); - // g.setColor(Color.black); - // bw.append("\nblack setrgbcolor\n"); - // Draw horizontal line - // g.drawLine(xstart,ypos,xend,ypos); - printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n"); - int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy; - int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy; - - // g.drawLine((int)(height*scale) + offx, ystart, - // (int)(height*scale) + offx, yend); - printout - (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " + - yend + " lineto stroke\n"); - if (treeCanvas.showDistances && node.dist > 0) { - // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); - printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n"); - } - } -} - -public void printout(String s) throws IOException { - if (bw != null) { - bw.write(s); - } - if (ps != null) { - ps.print(s); - } - if (makeString == true) { - out.append(s); - } -} - - -public PostscriptProperties getPostscriptProperties() { - return pp; -} - -public FileProperties getFileProperties() { - return fp; -} - -public void setPostscriptProperties(PostscriptProperties pp) { - this.pp = pp; -} - -public void setFileProperties(FileProperties fp) { - this.fp = fp; -} public String getText(String format) { return null; } -public void getPostscript(PrintWriter bw) { - this.bw = bw; - drawPostscript(pp); - -} - -public void getPostscript(PrintStream bw) { - this.ps = bw; - drawPostscript(pp); - bw.flush(); -} - -public StringBuffer getPostscript() { - makeString = true; - out = new StringBuffer(); - drawPostscript(pp); - return out; -} - -public void propertiesInit() { - this.pp = new PostscriptProperties(); - this.fp = new FileProperties(); -} - - - public void saveButton_actionPerformed(ActionEvent e) - { - - try{ - JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY")); - chooser.setDialogTitle("Save Tree as postscript file"); - chooser.setToolTipText("Save"); - int value = chooser.showSaveDialog(this); - if (value == JalviewFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - - bw = new PrintWriter(new FileWriter(choice)); - getPostscript(bw); - bw.close(); - - } - - }catch(Exception ex){ex.printStackTrace();} - - } - protected void saveMenu_actionPerformed(ActionEvent e) { @@ -359,4 +171,67 @@ public void propertiesInit() { } + protected void epsTree_actionPerformed(ActionEvent e) + { + int width = treeCanvas.getWidth(), height=treeCanvas.getHeight(); + try + { + jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty( + "LAST_DIRECTORY"), new String[]{"eps"}, "Encapsulated Postscript"); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create EPS file from tree"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + return; + + jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getPath()); + FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); + EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height); + + + treeCanvas.draw(pg, width, height); + + pg.flush(); + pg.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + + protected void pngTree_actionPerformed(ActionEvent e) + { + int width = treeCanvas.getWidth(), height = treeCanvas.getHeight(); + try + { + jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty( + "LAST_DIRECTORY"), new String[]{"png"}, "Portable network graphics"); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle("Create PNG image from tree"); + chooser.setToolTipText("Save"); + + int value = chooser.showSaveDialog(this); + if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION) + return; + + jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getPath()); + FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); + + BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics png = bi.getGraphics(); + + treeCanvas.draw(png, width, height); + + ImageIO.write(bi, "png", out); + out.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + } -- 1.7.10.2