📄 printalignmentimage.java
字号:
/***************************************************************** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.** @author: Copyright (C) Tim Carver****************************************************************/package org.emboss.jemboss.editor;import java.awt.*;import java.awt.print.Paper;import java.awt.print.PageFormat;import java.awt.print.PrinterJob;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.io.*;import javax.swing.border.*;import javax.imageio.*;import javax.imageio.stream.*;import org.emboss.jemboss.gui.ScrollPanel;/**** Print png/jpeg image and print preview.* Java 1.4 or higher is required for the imageio package* which is used here to create jpeg and png images of the* multiple alignment.**/public class PrintAlignmentImage extends ScrollPanel{ /** page format */ private PageFormat format = null; /** page number to print */ private int pageIndex = 0; /** alignment sequence panel */ private GraphicSequenceCollection gsc; /** status field for print preview */ private JTextField statusField = new JTextField(""); /** number of residues per line */ private int nResPerLine = 0; /** use anti aliasing (default is false) */ private boolean antiAlias = false; /** file format */ private String ftype; /** * * @param gsc sequence panel * @param page format * */ public PrintAlignmentImage(GraphicSequenceCollection gsc, PageFormat format) { this(gsc); this.format = format; } /** * * @param gsc sequence panel * */ public PrintAlignmentImage(GraphicSequenceCollection gsc) { super(); this.gsc = gsc; setBackground(Color.white); } /** * * Set the page format * @param format to use for the image * */ protected void setFormat(PageFormat format) { this.format = format; } /** * * Get the page format * @return format to use for the image * */ protected PageFormat getFormat() { return format; } /** * * Set the page number to create an image of * @param pageIndex page number * */ public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } /** * * Override this method to draw the sequences * @return Graphics g * */ public void paintComponent(Graphics g) {// let UI delegate paint first (incl. background filling) super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); if(nResPerLine == 0) gsc.drawSequences(g2d,format,pageIndex); else gsc.drawSequences(g2d,format,pageIndex,nResPerLine); } /** * * Print to a jpeg or png file * */ public void print() { if(format == null) getFormatDialog(); try { String fsave = showOptions(); if(fsave == null) return; int npages = gsc.getNumberPages(format,nResPerLine); for(int i=0;i<npages;i++) { RenderedImage rendImage = createAlignmentImage(i); writeImageToFile(rendImage, new File(fsave+i+"."+ftype), ftype); } } catch(NoClassDefFoundError ex) { JOptionPane.showMessageDialog(this, "This option requires Java 1.4 or higher."); } } /** * * Print to a jpeg or png file * */ public void print(int nResPerLine, String type, String filePrefix, boolean landscape, double leftMargin, double rightMargin, double topMargin, double btmMargin) { this.nResPerLine = nResPerLine; PrinterJob printerJob = PrinterJob.getPrinterJob(); format = new PageFormat(); if(landscape) format.setOrientation(PageFormat.LANDSCAPE); else format.setOrientation(PageFormat.PORTRAIT); if(leftMargin > 0.d) { Paper paper = format.getPaper(); double width = paper.getWidth()-(72*(leftMargin+rightMargin)); double hgt = paper.getHeight()-(72*(topMargin+btmMargin)); paper.setImageableArea(leftMargin*72,topMargin*72, width,hgt); format.setPaper(paper); } if(nResPerLine <= 0) this.nResPerLine = gsc.getResiduesPerLine(format); try { int npages = gsc.getNumberPages(format,this.nResPerLine); for(int i=0;i<npages;i++) { RenderedImage rendImage = createAlignmentImage(i); writeImageToFile(rendImage, new File(filePrefix+i+"."+type),type); } } catch(NoClassDefFoundError ex) { JOptionPane.showMessageDialog(this, "This option requires Java 1.4 or higher."); } } /** * * Print to one jpeg or png file * */ public void print(String filePrefix, double leftMargin, double rightMargin, double topMargin, double btmMargin) { this.print(nResPerLine,ftype,filePrefix, leftMargin,rightMargin,topMargin,btmMargin); } /** * * Print to one jpeg or png file * */ public void print(int nResPerLine, String type, String filePrefix, double leftMargin, double rightMargin, double topMargin, double btmMargin) { this.nResPerLine = nResPerLine; PrinterJob printerJob = PrinterJob.getPrinterJob(); format = new PageFormat(); Dimension d = gsc.getImageableSize(nResPerLine); double imageWidth = d.getWidth(); double imageHeight = d.getHeight(); Paper paper = format.getPaper(); if(leftMargin > 0.d) { leftMargin = leftMargin*72; topMargin = topMargin*72; rightMargin = rightMargin*72; btmMargin = btmMargin*72; paper.setSize(imageWidth+(leftMargin+rightMargin), imageHeight+(topMargin+btmMargin)); } else { paper.setSize(imageWidth,imageHeight); leftMargin = 0; topMargin = 0; } paper.setImageableArea(leftMargin,topMargin, imageWidth,imageHeight+imageHeight); format.setPaper(paper); try { RenderedImage rendImage = createAlignmentImage(0); writeImageToFile(rendImage, new File(filePrefix+"."+type),type); } catch(NoClassDefFoundError ex) { JOptionPane.showMessageDialog(this, "This option requires Java 1.4 or higher."); } } /** * * Provide some options for the image created * @param showFileOptions display file options * */ protected String showOptions() {// no. of residues per line Box YBox = Box.createVerticalBox(); YBox.add(Box.createVerticalGlue()); if(format == null) format = new PageFormat(); String mres = Integer.toString(gsc.getResiduesPerLine(format)); JLabel jres = new JLabel("Residues per line: [max:"+mres+"]"); if(nResPerLine != 0) mres = Integer.toString(nResPerLine); YBox.add(jres); JTextField maxResiduesField = new JTextField(mres,4); Dimension d = maxResiduesField.getPreferredSize(); maxResiduesField.setMaximumSize(d); Box bacross = Box.createHorizontalBox(); bacross.add(Box.createHorizontalGlue()); bacross.add(maxResiduesField); YBox.add(bacross);// file chooser String cwd = System.getProperty("user.dir"); JFileChooser fc = new JFileChooser(cwd); File fselect = new File(cwd+ System.getProperty("file.separator")+ "jae_image.jpeg"); fc.setSelectedFile(fselect);// file name prefix JLabel labFormat = new JLabel("Select Format:"); Font font = labFormat.getFont(); labFormat.setFont(font.deriveFont(Font.BOLD)); YBox.add(labFormat); bacross = Box.createHorizontalBox(); JComboBox formatSelect = new JComboBox(javax.imageio.ImageIO.getWriterFormatNames()); d = formatSelect.getPreferredSize(); formatSelect.setMaximumSize(d); bacross.add(Box.createHorizontalGlue()); bacross.add(formatSelect); YBox.add(bacross);// file prefix & format options fc.setAccessory(YBox); int n = fc.showSaveDialog(null); if(n == JFileChooser.CANCEL_OPTION) return null; nResPerLine = Integer.parseInt(maxResiduesField.getText()); ftype = (String)formatSelect.getSelectedItem();// remove file extension String fsave = fc.getSelectedFile().getAbsolutePath().toLowerCase(); if(fsave.endsWith(".png") || fsave.endsWith(".jpg") || fsave.endsWith(".jpeg") ) { int ind = fsave.lastIndexOf("."); fsave = fc.getSelectedFile().getAbsolutePath(); fsave = fsave.substring(0,ind); } else fsave = fc.getSelectedFile().getAbsolutePath();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -