Article View: borland.public.jbuilder.java.language
Article #17086Anyone out there willing to help?
From: Ross Dukeshier
Date: Sun, 20 Dec 1998 00:00
Date: Sun, 20 Dec 1998 00:00
206 lines
7764 bytes
7764 bytes
I'm at the end of my rope here folks. I've read all the messages in 5
different newsgroups, read the bug parade reports, read the frequently asked
questions, and looked at all the samples and the docs for the JDK 1.2 stuff.
Someone out there has to know the answers to my problem...
I'm trying to send text to a printer using the JDK 1.2 classes and all I get
is a blank page. I've tried using the BufferedImage class to get the
graphics object and I've tried getting the graphics object from a visible
JTextArea both producing the same results - blank page. When I run it, the
book shows as having the page (can't really tell if anything's on it,
though). There has to be something I'm missing between the interaction of
the PrinterJob, the Book, and my Printable object's print method. What is
the relationship between the print method and the book? If I've set the
pageable object of the PrinterJob to the book, what am I doing with the
print method? Just determining if the page exists or actually drawing the
specified page on the graphics object? From the docs, I gathered I was
building the graphics object, but even when I draw stuff straight on it in
the print method, I still get the ol' blank page. What am I missing???
Please help, this is driving me insane!!!
Here's the current view of my code...
package cm;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.*;
import java.util.*;
import java.awt.print.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.image.*;
public class PrintableBook extends Writer implements Printable {
protected String time;
protected Font font;
protected int fontsize;
protected JFrame testFrame = new JFrame();
protected JTextArea testArea = new JTextArea();
protected Font headerfont;
protected LineMetrics metrics;
protected LineMetrics headermetrics;
protected double x0;
protected double y0;
protected double headery;
protected int linesperpage;
protected int charnum = 0;
protected int linenum = 0;
protected int pagenum = 0;
protected PrinterJob job;
protected Book book;
protected BufferedImage bi;
protected Graphics2D page;
protected PageFormat pageFormat;
public PrintableBook(Font font, Vector text, PageFormat pageFormat) {
try {
testFrame.getContentPane().add(testArea);
testFrame.setVisible(true);
testArea.setText("This is the first line\n"+
"This is the second line\n"+
".\n.\n.\n.\n.\n.\n");
testFrame.setSize(500,500);
System.out.println("Creating book");
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
PrinterJob job = PrinterJob.getPrinterJob();
System.out.println("Getting time");
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.SHORT);
df.setTimeZone(TimeZone.getDefault());
time = df.format(new Date());
System.out.println("Setting page format");
this.pageFormat = pageFormat;
System.out.println("Determining lines per page");
fontsize = font.getSize();
System.out.println(font.getSize());
double ih = pageFormat.getImageableHeight();
System.out.println(ih);
metrics = font.getLineMetrics("Test", 0, 4, new
FontRenderContext(null,false,false));
linesperpage = (int)
(pageFormat.getImageableHeight()/metrics.getHeight());
System.out.println(linesperpage);
System.out.println("Getting graphics object");
bi = new BufferedImage(612, 792, BufferedImage.TYPE_BYTE_BINARY);
page = bi.createGraphics();
page.setColor(Color.black);
System.out.println("Creating book");
book = new Book();
System.out.println("Adding first page to book");
//******
book.append(this, pageFormat);
System.out.println("Writing text to book");
String line;
for (int row=0; row < text.size(); row++) {
line = (String) text.elementAt(row);
System.out.println(line);
write(line.toCharArray(),0,line.length());
}
System.out.println("Num Pages: "+book.getNumberOfPages());
book.setPage(0, this, pageFormat);
System.out.println("Num Pages: "+book.getNumberOfPages());
job.setPageable(book);
job.print();
}
catch (Exception e) {
System.out.println("Error occurred");
System.out.println(e.getMessage());
}
}
public void write(char[] buffer, int index, int len) {
System.out.println("In write method");
synchronized(this.lock) {
page.drawChars(buffer, index, len, (int) x0, (int) y0);
}
System.out.println("out of write method");
}
public void flush() {}
public void close() {
synchronized(this.lock) {
if (page != null) page.dispose();
}
}
public void pageBreak() { synchronized(this.lock) { newpage(); } }
public int getLinesPerPage() {return this.linesperpage; }
protected void newline() {
charnum = 0;
linenum++;
if (linenum >= linesperpage) {
book.setPage(pagenum, this, pageFormat);
page = null;
}
}
public int print(Graphics g, PageFormat pf, int pageIndex) {
//g = (Graphics) book.getPrintable(pageIndex);
System.out.println("Page number: "+pageIndex);
//g=page;
System.out.println(testArea.getText());
g=(Graphics) testArea.getGraphics();
return Printable.PAGE_EXISTS;
}
protected void newpage() {
page = bi.createGraphics();
book.append(this, pageFormat);
linenum = 0;
charnum = 0;
pagenum++;
page.setFont(headerfont);
page.drawString(job.getJobName(), (int) pageFormat.getImageableX(),
(int) pageFormat.getImageableY());
String s = "- "+pagenum+" -";
FontRenderContext frc = new FontRenderContext(null, false, false);
int w = (int) font.getStringBounds(s, frc).getWidth();
page.drawString(s, (int) (x0 + (pageFormat.getImageableWidth() - w)/2),
(int) headery);
w = (int) headerfont.getStringBounds(time, frc).getWidth();
page.drawString(time, (int) (x0+pageFormat.getImageableWidth() - w),
(int) headery);
int y = (int) (headery + headermetrics.getDescent() + 1);
page.drawLine((int) x0, (int) y, (int)
(x0+pageFormat.getImageableWidth()), (int) y);
page.setFont(font);
}
public static void main(String[] args) {
String s = "This is a test";
Font font = new Font("Monospaced", Font.PLAIN, 12);
FontRenderContext frc = new FontRenderContext(null,false,false);
LineMetrics lm = font.getLineMetrics(s,0,s.length(), frc);
System.out.println(lm.getNumChars());
Rectangle2D bounds = font.getStringBounds(s, frc);
System.out.println(bounds.getWidth());
Vector text = new Vector();
text.addElement("This is the first line");
text.addElement("This is the second line");
text.addElement(".");
text.addElement(".");
text.addElement(".");
text.addElement(".");
text.addElement(".");
text.addElement(".");
int fontsize = 12;
PrintableBook pb = new PrintableBook(font, text, new PageFormat());
}
}
Technical Details
Message-ID:
<75i7jh$5gi4@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,5cf86e902afbf4b,start X-Google-Attributes: gidf74b3,public From: "Ross Dukeshier" <dukeman@flash.net> Subject: Anyone out there willing to help? Date: 1998/12/20 Message-ID: <75i7jh$5gi4@forums.borland.com> X-Deja-AN: 424144530 Organization: Another Netscape Collabra Server User X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Newsgroups: borland.public.jbuilder.java.language