Skip to content
Snippets Groups Projects
Commit f6848a2d authored by tjc's avatar tjc
Browse files

add print to PostScript

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@8686 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent 9fa5d44e
No related branches found
No related tags found
No related merge requests found
...@@ -22,18 +22,33 @@ ...@@ -22,18 +22,33 @@
package uk.ac.sanger.artemis.components; package uk.ac.sanger.artemis.components;
import java.awt.*; import java.awt.*;
import java.awt.print.Paper; import java.awt.event.ActionEvent;
import java.awt.print.PageFormat; import java.awt.event.ActionListener;
import java.awt.print.PrinterJob; import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
import java.awt.event.*; import java.awt.print.PageFormat;
import javax.swing.*; import java.awt.print.Printable;
import java.util.*; import java.awt.print.PrinterException;
import java.io.*; import java.awt.print.PrinterJob;
import javax.swing.border.*; import java.io.File;
import javax.imageio.*; import java.io.IOException;
import javax.imageio.stream.*;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import uk.ac.sanger.artemis.editor.ScrollPanel; import uk.ac.sanger.artemis.editor.ScrollPanel;
...@@ -42,9 +57,9 @@ import uk.ac.sanger.artemis.editor.ScrollPanel; ...@@ -42,9 +57,9 @@ import uk.ac.sanger.artemis.editor.ScrollPanel;
* Use to print images from ACT * Use to print images from ACT
* *
*/ */
public class PrintACT extends ScrollPanel public class PrintACT extends ScrollPanel implements Printable
{ {
private static final long serialVersionUID = 1L;
/** act display to create image from */ /** act display to create image from */
private MultiComparator mc; private MultiComparator mc;
private JCheckBox drawLabels = new JCheckBox("Show labels on alignment"); private JCheckBox drawLabels = new JCheckBox("Show labels on alignment");
...@@ -173,7 +188,7 @@ public class PrintACT extends ScrollPanel ...@@ -173,7 +188,7 @@ public class PrintACT extends ScrollPanel
menuBar.add(filemenu); menuBar.add(filemenu);
// print png/jpeg // print png/jpeg
JMenuItem printImage = new JMenuItem("Print Image Files (png/jpeg)..."); JMenuItem printImage = new JMenuItem("Save As Image Files (png/jpeg)...");
printImage.addActionListener(new ActionListener() printImage.addActionListener(new ActionListener()
{ {
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
...@@ -182,6 +197,17 @@ public class PrintACT extends ScrollPanel ...@@ -182,6 +197,17 @@ public class PrintACT extends ScrollPanel
} }
}); });
filemenu.add(printImage); filemenu.add(printImage);
// print PostScript
JMenuItem printPS = new JMenuItem("Print...");
printPS.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doPrintActions();
}
});
filemenu.add(printPS);
// close // close
filemenu.add(new JSeparator()); filemenu.add(new JSeparator());
...@@ -298,6 +324,25 @@ public class PrintACT extends ScrollPanel ...@@ -298,6 +324,25 @@ public class PrintACT extends ScrollPanel
* *
*/ */
private RenderedImage createImage() private RenderedImage createImage()
{
Dimension d = getImageSize();
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(
d.width,d.height,
BufferedImage.TYPE_INT_RGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
paintComponent(g2d);
return bufferedImage;
}
/**
* Get the size of the image
* @return
*/
private Dimension getImageSize()
{ {
int width = 999999; int width = 999999;
int height = 0; int height = 0;
...@@ -342,19 +387,9 @@ public class PrintACT extends ScrollPanel ...@@ -342,19 +387,9 @@ public class PrintACT extends ScrollPanel
width = mc.getAlignmentViewerArray()[i].getWidth(); width = mc.getAlignmentViewerArray()[i].getWidth();
} }
} }
return new Dimension(width, height);
// Create a buffered image in which to draw
BufferedImage bufferedImage = new BufferedImage(
width,height,
BufferedImage.TYPE_INT_RGB);
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
paintComponent(g2d);
return bufferedImage;
} }
/** /**
* *
* Write out the image * Write out the image
...@@ -376,5 +411,50 @@ public class PrintACT extends ScrollPanel ...@@ -376,5 +411,50 @@ public class PrintACT extends ScrollPanel
e.printStackTrace(); e.printStackTrace();
} }
} }
protected void doPrintActions()
{
final PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(PrintACT.this);
pj.printDialog();
try
{
pj.print();
}
catch (Exception PrintException) {}
}
/**
*
* The method @print@ must be implemented for @Printable@ interface.
* Parameters are supplied by system.
*
*/
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
Graphics2D g2 = (Graphics2D) g;
// RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = getImageSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pf.getImageableHeight(); //height of printer page
double pageWidth = pf.getImageableWidth(); //width of printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
// Make sure not print empty pages
if(pageIndex >= totalNumPages)
return Printable.NO_SUCH_PAGE;
// Shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageableX(), pf.getImageableY());
// Shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex*pageHeight);
// Scale the page so the width fits...
g2.scale(scale, scale);
paintComponent(g2);
return Printable.PAGE_EXISTS;
}
} }
...@@ -24,11 +24,13 @@ package uk.ac.sanger.artemis.components; ...@@ -24,11 +24,13 @@ package uk.ac.sanger.artemis.components;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage; import java.awt.image.RenderedImage;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.awt.event.*; import java.awt.event.*;
import javax.swing.*; import javax.swing.*;
import java.io.*; import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import uk.ac.sanger.artemis.editor.ScrollPanel; import uk.ac.sanger.artemis.editor.ScrollPanel;
...@@ -37,8 +39,9 @@ import uk.ac.sanger.artemis.editor.ScrollPanel; ...@@ -37,8 +39,9 @@ import uk.ac.sanger.artemis.editor.ScrollPanel;
* Use to print images from Artemis * Use to print images from Artemis
* *
*/ */
public class PrintArtemis extends ScrollPanel public class PrintArtemis extends ScrollPanel implements Printable
{ {
private static final long serialVersionUID = 1L;
/** entry to create image from */ /** entry to create image from */
private EntryEdit entry; private EntryEdit entry;
...@@ -183,7 +186,7 @@ public class PrintArtemis extends ScrollPanel ...@@ -183,7 +186,7 @@ public class PrintArtemis extends ScrollPanel
menuBar.add(filemenu); menuBar.add(filemenu);
// print png/jpeg // print png/jpeg
JMenuItem printImage = new JMenuItem("Print Image Files (png/jpeg)..."); JMenuItem printImage = new JMenuItem("Save As Image Files (png/jpeg)...");
printImage.addActionListener(new ActionListener() printImage.addActionListener(new ActionListener()
{ {
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
...@@ -192,6 +195,17 @@ public class PrintArtemis extends ScrollPanel ...@@ -192,6 +195,17 @@ public class PrintArtemis extends ScrollPanel
} }
}); });
filemenu.add(printImage); filemenu.add(printImage);
// print PostScript
JMenuItem printPS = new JMenuItem("Print...");
printPS.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doPrintActions();
}
});
filemenu.add(printPS);
// close // close
filemenu.add(new JSeparator()); filemenu.add(new JSeparator());
...@@ -421,13 +435,23 @@ public class PrintArtemis extends ScrollPanel ...@@ -421,13 +435,23 @@ public class PrintArtemis extends ScrollPanel
"This option requires Java 1.4 or higher."); "This option requires Java 1.4 or higher.");
} }
} }
protected void doPrintActions()
{
final PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(PrintArtemis.this);
pj.printDialog();
try
{
pj.print();
}
catch (Exception PrintException) {}
}
/** /**
*
* Returns a generated image * Returns a generated image
* @param pageIndex page number * @param pageIndex page number
* @return image * @return image
*
*/ */
private RenderedImage createImage() private RenderedImage createImage()
{ {
...@@ -445,12 +469,10 @@ public class PrintArtemis extends ScrollPanel ...@@ -445,12 +469,10 @@ public class PrintArtemis extends ScrollPanel
/** /**
*
* Write out the image * Write out the image
* @param image image * @param image image
* @param file file to write image to * @param file file to write image to
* @param type type of image * @param type type of image
*
*/ */
private void writeImageToFile(RenderedImage image, private void writeImageToFile(RenderedImage image,
File file, String type) File file, String type)
...@@ -466,4 +488,43 @@ public class PrintArtemis extends ScrollPanel ...@@ -466,4 +488,43 @@ public class PrintArtemis extends ScrollPanel
} }
} }
/**
*
* The method @print@ must be implemented for @Printable@ interface.
* Parameters are supplied by system.
*
*/
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
setImageSize();
Graphics2D g2 = (Graphics2D) g;
// RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
if(panelWidth == 0)
{
d = this.getPreferredSize();
panelWidth = d.width;
panelHeight = d.height;
}
double pageHeight = pf.getImageableHeight(); //height of printer page
double pageWidth = pf.getImageableWidth(); //width of printer page
double scale = pageWidth/panelWidth;
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
// Make sure not print empty pages
if(pageIndex >= totalNumPages)
return Printable.NO_SUCH_PAGE;
// Shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageableX(), pf.getImageableY());
// Shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex*pageHeight);
// Scale the page so the width fits...
g2.scale(scale, scale);
paintComponent(g2);
return Printable.PAGE_EXISTS;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment