import javax.swing.*;\r
import java.awt.print.*;\r
import java.awt.*;\r
+import java.awt.image.*;\r
+import org.jibble.epsgraphics.*;\r
+import javax.imageio.*;\r
+\r
\r
\r
\r
TreeCanvas treeCanvas;\r
NJTree tree;\r
\r
- FileProperties fp;\r
- PostscriptProperties pp;\r
-\r
- PrintWriter bw;\r
- PrintStream ps;\r
- boolean makeString = false;\r
- StringBuffer out;\r
-\r
-\r
\r
public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e)\r
{\r
tree = new NJTree(seq, type, pwtype, start, end);\r
av.setCurrentTree(tree);\r
\r
- propertiesInit();\r
-\r
treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName);\r
\r
tree.reCount(tree.getTopNode());\r
tree = new NJTree(seq, newtree);\r
av.setCurrentTree(tree);\r
\r
- propertiesInit();\r
\r
tree.reCount(tree.getTopNode());\r
tree.findHeight(tree.getTopNode());\r
}\r
\r
\r
-public void drawPostscript(PostscriptProperties pp) {\r
- try {\r
- int width = 0;\r
- int height = 0;\r
-\r
- printout("%!\n");\r
-\r
- printout("/" + pp.font + " findfont\n");\r
- printout(pp.fsize + " scalefont setfont\n");\r
-\r
- int offx = pp.xoffset;\r
- int offy = pp.yoffset;\r
-\r
- if (pp.orientation == PostscriptProperties.PORTRAIT) {\r
- width = PostscriptProperties.SHORTSIDE;\r
- height = PostscriptProperties.LONGSIDE;\r
- } else {\r
- height = PostscriptProperties.SHORTSIDE;\r
- width = PostscriptProperties.LONGSIDE;\r
- printout(height + " 0 translate\n90 rotate\n");\r
- }\r
- float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight();\r
- SequenceNode top = tree.getTopNode();\r
-\r
- if (top.count == 0) {\r
- top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ;\r
- }\r
-\r
- float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1);\r
-\r
- drawPostscriptNode(top,chunk,wscale,width,offx,offy);\r
-\r
- printout("showpage\n");\r
- } catch (java.io.IOException e) {\r
- System.out.println("Exception " + e);\r
- }\r
-}\r
-\r
-public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException {\r
- if (node == null) {\r
- return;\r
- }\r
-\r
- if (node.left() == null && node.right() == null) {\r
- // Drawing leaf node\r
-\r
- float height = node.height;\r
- float dist = node.dist;\r
-\r
- int xstart = (int)((height-dist)*scale) + offx;\r
- int xend = (int)(height*scale) + offx;\r
-\r
- int ypos = (int)(node.ycount * chunk) + offy;\r
-\r
- // g.setColor(Color.black);\r
- printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");\r
-\r
- // Draw horizontal line\r
- // g.drawLine(xstart,ypos,xend,ypos);\r
- printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n");\r
-\r
- if (treeCanvas.showDistances && node.dist > 0) {\r
- // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);\r
- printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n");\r
- }\r
- //g.drawString((String)node.element(),xend+20,ypos);\r
- printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n");\r
- } else {\r
- drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy);\r
- drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy);\r
-\r
-\r
- float height = node.height;\r
- float dist = node.dist;\r
-\r
- int xstart = (int)((height-dist)*scale) + offx;\r
- int xend = (int)(height*scale) + offx;\r
- int ypos = (int)(node.ycount * chunk) + offy;\r
-\r
- printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getGreen()/255) + " " +\r
- new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n");\r
- // g.setColor(Color.black);\r
- // bw.append("\nblack setrgbcolor\n");\r
- // Draw horizontal line\r
- // g.drawLine(xstart,ypos,xend,ypos);\r
- printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n");\r
- int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy;\r
- int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy;\r
-\r
- // g.drawLine((int)(height*scale) + offx, ystart,\r
- // (int)(height*scale) + offx, yend);\r
- printout\r
- (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " +\r
- yend + " lineto stroke\n");\r
- if (treeCanvas.showDistances && node.dist > 0) {\r
- // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5);\r
- printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n");\r
- }\r
- }\r
-}\r
-\r
-public void printout(String s) throws IOException {\r
- if (bw != null) {\r
- bw.write(s);\r
- }\r
- if (ps != null) {\r
- ps.print(s);\r
- }\r
- if (makeString == true) {\r
- out.append(s);\r
- }\r
-}\r
-\r
-\r
-public PostscriptProperties getPostscriptProperties() {\r
- return pp;\r
-}\r
-\r
-public FileProperties getFileProperties() {\r
- return fp;\r
-}\r
-\r
-public void setPostscriptProperties(PostscriptProperties pp) {\r
- this.pp = pp;\r
-}\r
-\r
-public void setFileProperties(FileProperties fp) {\r
- this.fp = fp;\r
-}\r
\r
public String getText(String format) {\r
return null;\r
}\r
\r
-public void getPostscript(PrintWriter bw) {\r
- this.bw = bw;\r
- drawPostscript(pp);\r
-\r
-}\r
-\r
-public void getPostscript(PrintStream bw) {\r
- this.ps = bw;\r
- drawPostscript(pp);\r
- bw.flush();\r
-}\r
-\r
-public StringBuffer getPostscript() {\r
- makeString = true;\r
- out = new StringBuffer();\r
- drawPostscript(pp);\r
- return out;\r
-}\r
-\r
-public void propertiesInit() {\r
- this.pp = new PostscriptProperties();\r
- this.fp = new FileProperties();\r
-}\r
-\r
-\r
- public void saveButton_actionPerformed(ActionEvent e)\r
- {\r
-\r
- try{\r
- JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty("LAST_DIRECTORY"));\r
- chooser.setDialogTitle("Save Tree as postscript file");\r
- chooser.setToolTipText("Save");\r
- int value = chooser.showSaveDialog(this);\r
- if (value == JalviewFileChooser.APPROVE_OPTION)\r
- {\r
- String choice = chooser.getSelectedFile().getPath();\r
- jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);\r
-\r
- bw = new PrintWriter(new FileWriter(choice));\r
- getPostscript(bw);\r
- bw.close();\r
-\r
- }\r
-\r
- }catch(Exception ex){ex.printStackTrace();}\r
-\r
- }\r
-\r
\r
protected void saveMenu_actionPerformed(ActionEvent e)\r
{\r
}\r
\r
\r
+ protected void epsTree_actionPerformed(ActionEvent e)\r
+ {\r
+ int width = treeCanvas.getWidth(), height=treeCanvas.getHeight();\r
+ try\r
+ {\r
+ jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+ "LAST_DIRECTORY"), new String[]{"eps"}, "Encapsulated Postscript");\r
+ chooser.setFileView(new jalview.io.JalviewFileView());\r
+ chooser.setDialogTitle("Create EPS file from tree");\r
+ chooser.setToolTipText("Save");\r
+\r
+ int value = chooser.showSaveDialog(this);\r
+ if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
+ return;\r
+\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getPath());\r
+ FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
+ EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height);\r
+\r
+\r
+ treeCanvas.draw(pg, width, height);\r
+\r
+ pg.flush();\r
+ pg.close();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+ }\r
+\r
+ protected void pngTree_actionPerformed(ActionEvent e)\r
+ {\r
+ int width = treeCanvas.getWidth(), height = treeCanvas.getHeight();\r
+ try\r
+ {\r
+ jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+ "LAST_DIRECTORY"), new String[]{"png"}, "Portable network graphics");\r
+ chooser.setFileView(new jalview.io.JalviewFileView());\r
+ chooser.setDialogTitle("Create PNG image from tree");\r
+ chooser.setToolTipText("Save");\r
+\r
+ int value = chooser.showSaveDialog(this);\r
+ if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
+ return;\r
+\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY",chooser.getSelectedFile().getPath());\r
+ FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
+\r
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);\r
+ Graphics png = bi.getGraphics();\r
+\r
+ treeCanvas.draw(png, width, height);\r
+\r
+ ImageIO.write(bi, "png", out);\r
+ out.close();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+ }\r
+\r
}\r