// printjob.java import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.text.*; class printjob extends Frame implements ActionListener { int curPointSize = 9; String filename; Vector lines = new Vector(); public int curpg; static int totpg=6; static Vector brknLns; Button printBtn = new Button("Print..."); Button ExitBtn = new Button("...EXIT..."); Button PreBtn = new Button("Previous"); Button NextBtn = new Button(" Next "); TextField pageNum = new TextField("1"); Preview preview; // constructor for printjob printjob(String filename) { super("PrintJob Example"); // Remember the filename. this.filename = filename; // Read the file into lines. try { String line; BufferedReader fis = new BufferedReader( new InputStreamReader(new FileInputStream(filename))); while ((line = fis.readLine()) != null) { lines.addElement(line); } fis.close(); } catch (Exception e) { e.printStackTrace(); } // Listen for events. pageNum.addActionListener(this); printBtn.addActionListener(this); ExitBtn.addActionListener(this); PreBtn.addActionListener(this); NextBtn.addActionListener(this); // Layout and show components. Panel p = new Panel(new FlowLayout()); p.add(PreBtn); p.add(NextBtn); p.add(new Label(" ", Label.RIGHT)); p.add(new Label("Page:", Label.RIGHT)); p.add(pageNum); p.add(new Label(" ", Label.RIGHT)); p.add(printBtn); p.add(ExitBtn); p.add(new Label(" ", Label.RIGHT)); add(p, BorderLayout.NORTH); PreBtn.setEnabled(false); // Create and prepare the preview component. preview = new Preview(filename, lines); add(preview, BorderLayout.CENTER); preview.setFont(new Font("Monospaced", Font.PLAIN, curPointSize)); pack(); show(); try{ //this delay is done to allow the paint() Thread.sleep(1000); // to update printjob.totpg with the latest value. }catch(Exception eee) {} if(curpg==printjob.totpg-1) { NextBtn.setEnabled(false); } } // end of constructor for printjob public void actionPerformed(ActionEvent evt) { curpg = Integer.parseInt(pageNum.getText()); if (evt.getSource() == pageNum) { // Set the page. preview.setPage(Integer.parseInt(pageNum.getText())-1); } else if (evt.getSource() == ExitBtn) { System.exit(0); } else if (evt.getSource() == NextBtn) { if(printjob.totpg == 1) { PreBtn.setEnabled(false); NextBtn.setEnabled(false); } else if(curpg >= printjob.totpg) { PreBtn.setEnabled(true); NextBtn.setEnabled(false); } else { PreBtn.setEnabled(true); if(curpg >= (printjob.totpg-1)) { NextBtn.setEnabled(false); } curpg += 1 ; } preview.setPage(curpg-1); pageNum.setText(new Integer(curpg).toString()); } else if(evt.getSource() == PreBtn) { if(curpg >= printjob.totpg) { PreBtn.setEnabled(true); NextBtn.setEnabled(false); curpg-=1; } else if(curpg == 2) { PreBtn.setEnabled(false); NextBtn.setEnabled(true); curpg = curpg -1; } else { NextBtn.setEnabled(true); if(curpg == 2) { PreBtn.setEnabled(false); } curpg = curpg-1; } preview.setPage(curpg-1); pageNum.setText(new Integer(curpg).toString()); } else if (evt.getSource() == printBtn) { // Print the file. Properties props = new Properties(); PrintJob pj = getToolkit().getPrintJob(this, filename, props); if (pj != null) { // Print out any properties. for (Enumeration e = props.propertyNames() ; e.hasMoreElements() ;) { System.out.println("enumeration"+e.nextElement()); } Graphics pjG = pj.getGraphics(); int pages = preview.getPageCount(pjG); // Always print even number of pages. if (pages % 2 == 1) { pages++; } for (int i=0; i"); } } } class Preview extends Component { int page; String filename; Font headerFont = new Font("Serif", Font.BOLD, 8); Font font; Vector lines; Preview(String filename, Vector lines) { this.filename = filename; this.lines = lines; repaint(); } // Sets the font and repaints the display. public void setFont(Font f) { font = f; repaint(); } // p is 0-based. void setPage(int p) { page = p; repaint(); } // Returns the page count relative to the metrics in g. int getPageCount(Graphics g) { prepareLayoutValues(g); return (brokenLines.size()-1) / linesPerPage + 1; } // Prints the page p and p+1. // p is 0-based. public void print(Graphics g, int p) { page = p; paint(g); } public void paint(Graphics g) { System.out.println("Paint Vector : "); prepareLayoutValues(g); g.setFont(headerFont); FontMetrics fm = g.getFontMetrics(); for (int p=0; p<2; p++) { if (page + p >= (brokenLines.size()-1) / linesPerPage + 1) { break; } // Draw page number. String s = "" + (page + p + 1); g.setFont(headerFont); fm = g.getFontMetrics(); printjob.totpg = ((brokenLines.size()-1) / (linesPerPage + 1))+1; System.out.println("step 2: "); g.drawString(s, boxRect.x + p*pageRect.width + boxRect.width - fm.stringWidth(s)-180, fm.getHeight()/2+fm.getAscent()+530); g.drawRect(boxRect.x + p*pageRect.width, boxRect.y, boxRect.width-1, boxRect.height-1); // g.setColor(Color.red); // Find the starting line on the current page. int l = Math.min(brokenLines.size(), (page+p) * linesPerPage); g.setFont(font); fm = g.getFontMetrics(); int x = listingRect.x + p*pageRect.width; int y = listingRect.y + fm.getAscent(); for (int i=0; i charsPerLine) { int charWidth = fm.charWidth('M'); s = s.substring(0, charsPerLine); // Draw the continuation symbol. g.drawArc( listingRect.x+charsPerLine*charWidth + p*pageRect.width, y-fm.getAscent()+fm.getHeight()/2, charWidth, fm.getHeight()/2, 45, -180); } g.drawString(s, x, y); // g.dispose(); // g.setColor(Color.red); y += fm.getHeight(); } System.out.println("Paint Vector : "+printjob.brknLns.size()); for(int j=0 ; j < printjob.brknLns.size();j++) { System.out.println(((String)printjob.brknLns.elementAt(j)).substring(0,4).trim()); try{ if((((String)printjob.brknLns.elementAt(j)).substring(0,4).trim()).equals("FROM")) { System.out.println(((String)printjob.brknLns.elementAt(j)).substring(0,4).trim()); g.setColor(Color.red); } else { g.setColor(Color.black); } }catch(Exception gg){} } //end of for } // end of paint() } // end of preview class // External margin in which the printer cannot print. int pageMargin; // Internal margin between boxes and text. int margin =15; // 10; Insets margins = new Insets(0,0,0,0); // Bounds which includes the header, box, and listing. Rectangle pageRect; // The bounds of the box. Rectangle boxRect; // The bounds of the listing. This rectangle strictly contains // the text and not the continuation character. Rectangle listingRect; // Number of characters per line. int charsPerLine; // Number of lines per page. int linesPerPage; // The lines after they have been broken so they fit within charsPerLine. Vector brokenLines; // This method calculates and sets up all the fields above // based on the metrics in g. void prepareLayoutValues(Graphics g) { Dimension pageSize; int res; if (g instanceof PrintGraphics) { PrintJob pj = ((PrintGraphics)g).getPrintJob(); res = pj.getPageResolution(); pageSize = pj.getPageDimension(); // getPageDimension() does not return the correct result // The following is a workaround. res = getToolkit().getScreenResolution(); pageSize = new Dimension((int)(6.3 * res), (int)9 * res); } else { res = getToolkit().getScreenResolution(); pageSize = new Dimension((int)(6.3 * res), (int)9 * res); } pageMargin = (int)(.75 * res); pageSize = new Dimension(pageSize.height-pageMargin, pageSize.width-pageMargin); // Reserve space for the header. FontMetrics fm = g.getFontMetrics(headerFont); pageRect = new Rectangle(0, 0, pageSize.width/2, pageSize.height); boxRect = inset(pageRect,margin,0,margin,0); listingRect = inset(pageRect,margin+5,1,margin,1); // Set up listing font. fm = g.getFontMetrics(font); int lpp = listingRect.height/fm.getHeight(); int cpp = listingRect.width/fm.charWidth('M'); // The metrics have changed so recompute the broken lines. if (lpp != linesPerPage || cpp != charsPerLine) { brokenLines = new Vector(); for (int i=0; i cpp) { brokenLines.addElement(s.substring(0, cpp)+"+"); s = s.substring(cpp); } brokenLines.addElement(s); printjob.brknLns = brokenLines; } linesPerPage = lpp; charsPerLine = cpp; } } Rectangle inset(Rectangle r, int left, int top, int right, int bottom) { Rectangle s = new Rectangle(r); s.x += left; s.y += top; s.width -= left+right; s.height -= top+bottom; return s; } }