/** PrintTester.java This is an example of how to print from a text area. Essentially you need to create an image of what you want to print with the drawing area for the image being the printer. The code shown here is by no means the only (or necessarily best) way of printing a text area, but it does work and should be suitable (with slight modification) for anyone having trouble with printint in their assignment. The code you will be most interested in is the class "Print". This was written by Jarrod for a large java-based assignment last year and was used in that software. The remainder of the code was slapped together by Alan and is really just setting up the frame and text area. @author Jarrod Trevathan and Alan McCabe */ // Import Java libraries import java.awt.*; import java.awt.datatransfer.*; import java.io.*; import java.util.*; import java.awt.event.*; import javax.swing.*; /** Class Print - prints a text area */ class Print { // Variable for storing print properties Properties p = new Properties(); // constructor public Print(Frame parent, final TextArea textArea) { // get tool kit Toolkit toolkit = parent.getToolkit(); // Set print properties PrintJob pjob = toolkit.getPrintJob(parent, "Message", p); if (pjob != null) { // Does a print job exist? Graphics pg = pjob.getGraphics(); // Get graphics environment if (pg != null) { // Print next page String s = textArea.getText(); printLongString (pjob, pg, s); pg.dispose(); // Dispose of current page } pjob.end(); // End of print job } } // Function printLongString // Print string to graphics via printjob // Does not deal with word wrap or tabs private void printLongString (PrintJob pjob, Graphics pg, String s) { int pageNum = 1; // Page number int linesForThisPage = 0; // Number lines for current page int linesForThisJob = 0; // Number lines for current job // Note: String is immutable so won't change while printing. if (!(pg instanceof PrintGraphics)) { throw new IllegalArgumentException ("Graphics context not PrintGraphics"); } // create StringReader and LineNumberReader objects StringReader sr = new StringReader (s); LineNumberReader lnr = new LineNumberReader (sr); String nextLine; // get the page height int pageHeight = pjob.getPageDimension().height; // set the font to print in Font helv = new Font("Helvetica", Font.PLAIN, 12); //have to set the font to get any output pg.setFont (helv); // get the dimensions of the font FontMetrics fm = pg.getFontMetrics(helv); int fontHeight = fm.getHeight(); int fontDescent = fm.getDescent(); int curHeight = 0; try { do { // get the next line from the text area nextLine = lnr.readLine(); if (nextLine != null) { // if we are over the page, create a new one if ((curHeight + fontHeight) > pageHeight) { // New Page pageNum++; linesForThisPage = 0; pg.dispose(); pg = pjob.getGraphics(); if (pg != null) { pg.setFont (helv); } curHeight = 0; } curHeight += fontHeight; if (pg != null) { // draw the line to the printer pg.setColor(Color.black); pg.drawString (nextLine, 20, curHeight - fontDescent+20); linesForThisPage++; linesForThisJob++; } else { System.out.println ("pg null"); } } } while (nextLine != null); } catch (EOFException eof) { // Fine, ignore } catch (Exception t) { // Anything else t.printStackTrace(); } } } // TextFrame to display a text box class TextFrame extends JFrame { // constants private final static int DEFAULT_FRAME_WIDTH = 500; private final static int DEFAULT_FRAME_HEIGHT = 500; // text area, button and panel private TextArea textArea; private JButton theButton; private JPanel titlePanel; // constructor public TextFrame() { // set size of frame setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT); // get content pane of Frame Container contentPane = getContentPane(); // set LayoutManager for this Frame contentPane.setLayout(new BorderLayout()); // create TextArea (note that it will be stretched to fill "center") textArea = new TextArea(7, 40); // user must not be able to write into TextArea textArea.setEditable(true); // create title panel titlePanel = new JPanel(); titlePanel.setLayout(new BorderLayout()); // set a nice titled border around titlePanel and add a text box titlePanel.setBorder( BorderFactory.createTitledBorder("Print the Text Area")); // add the text area to the title panel and the title panel to the // content pane titlePanel.add(textArea, "Center"); contentPane.add(titlePanel, "Center"); // create a buttonPanel and add it to the south area JPanel buttonPanel = new JPanel(); // add "Print" button with listener to buttonPanel theButton = new JButton("Print"); theButton.addActionListener(new ButtonListener(this)); buttonPanel.add(theButton); // add the button panel contentPane.add(buttonPanel, "South"); // add window listener addWindowListener(new WindowCloser()); } // This inner class handles Window events. private class WindowCloser extends WindowAdapter { // we only re-define the windowClosing member function public void windowClosing(WindowEvent e) { System.exit(0); } } // This inner class handles button events. private class ButtonListener implements ActionListener { JFrame myParent; // constructor (need to pass in parent for printing code) public ButtonListener(JFrame parent) { myParent = parent; } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == theButton) { try { Print printer = new Print(myParent, textArea); } catch (Exception ex) { System.out.println("Had problems: " + ex); } } } } } // wrapper class public class PrintTester { // main method to be invoked from JVM public static void main(String[] args) { // construct a new TextFrame TextFrame frame = new TextFrame(); frame.setTitle("Printing Text"); frame.show(); } }