what is wrong with this code ?

I get this printing code here and tried to contact the original writer, but couldn't.

I usually use this code inder JDK1.3.1, but for deployement reasons, i have to use

JDK1.2 in order to create an exe file under VisualCafe Expert 4.5.

The printing code work well under JDK1.3.1 but when i run it as an executable file,

i get the following error :

Exception occurred during event dispatching:

java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.

at java.awt.font.TextLayout.<init>(TextLayout.java:337)

at sun.java2d.PeekGraphics.drawString(PeekGraphics.java:845)

at MyPrinter$MyPrintable.print(MyPrinter.java:204)

at sun.java2d.RasterPrinterJob.printPage(RasterPrinterJob.java:485)

at sun.java2d.RasterPrinterJob.print(RasterPrinterJob.java:235)

at MyPrinter.run(MyPrinter.java:55)

at MyPrinter.printFile(MyPrinter.java:32)

here is the code i use for it :

import java.awt.*;

import java.awt.print.*;

import java.io.*;

import javax.swing.*;

publicclass MyPrinterimplements Runnable

{

protected PrinterJob job;

protected MyPrintable myPrint =new MyPrintable();

protected MyPageable myPage =new MyPageable();

//protected MyDialog dialog = new MyDialog();

protected RandomAccessFile raf;

protected String fileName;

protected File printFile;

protectedint numPages = 0;

protectedint[] lastFilePositions;

// private Thread myThread = null;

public MyPrinter(){}// end constructor

publicvoid printFile(String name)

{

// gives error message if file doesn't exist!

printFile =new File(name);

fileName = name;

if((printFile.exists()) && (printFile.canRead()))

{

this.run();

//myThread = new Thread(this);

//myThread.start();

}// end if

else

{

return;

}// end else

}// end printFile

publicvoid run()

{

numPages = findNumPages();

job = PrinterJob.getPrinterJob();

job.setPageable(myPage);// this is an instance of Pageable

if(job.printDialog())

{

// user didn't cancel the printing

try

{

job.print();

try

{

raf.close();

}// end try

catch(IOException e)

{

//dialog.showError("Fehler: "+e.getMessage(), "Problem");

//e.printStackTrace();

System.exit(1);

}// end catch

}// end try

catch (PrinterException pe)

{

//dialog.showError("Fehler: "+pe.getMessage(), "Problem");

//pe.printStackTrace();

System.exit(1);

}// end catch

}// end if

}// end run

privateint findNumPages()

{

try

{

numPages = 0;

raf =new RandomAccessFile(fileName,"r");

int fileLength = (int)printFile.length();

int lineNumber = 0;

while((int)raf.getFilePointer() < fileLength)

{

lineNumber = 0;

while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 43))

{

raf.readLine();

lineNumber++;

}// end while loop

numPages++;

}// end while loop

raf.seek(0);

lastFilePositions =newint[numPages];

intializeLastFilePositions();

}// end try

catch(IOException e)

{

//dialog.showError("Fehler: "+e.getMessage(), "Problem");

//e.printStackTrace();

System.exit(0);

}// end catch

return numPages;

}// end findNumPages

privatevoid intializeLastFilePositions()

{

for(int i = 0; i < lastFilePositions.length; i++)

{

lastFilePositions[i] = 0;

}// end for loop

}// end intializeLastFilePositions

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

publicclass MyPageableimplements Pageable

{

public Printable getPrintable(int pageIndex)

{

return myPrint;

}// end getPrintable

public PageFormat getPageFormat(int pageIndex)

{

PageFormat pf =new PageFormat();

pf.setOrientation(PageFormat.PORTRAIT);

return pf;

}// end getPageFormat

publicint getNumberOfPages()

{

return numPages;// test

//return ((int)printFile.length()/4000); // assume 4KB per page

}// end getNumberOfPages

}// end MyPageable class

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

publicclass MyPrintableimplements Printable

{

private Font fnt =new Font("Serif", Font.PLAIN, 10);

protectedint startAtLineNumber = 0;

publicint print(Graphics g, PageFormat pf,int pageIndex)throws PrinterException

{

// pageIndex # corresponds to page number # + 1

pf.setOrientation(PageFormat.PORTRAIT);

if(pageIndex > numPages)

return NO_SUCH_PAGE;

else

{

if(pageIndex == 0)

{

startAtLineNumber = 0;

}// end if

else

{

startAtLineNumber = lastFilePositions[pageIndex-1];

}// end else

g.setFont(fnt);

g.setColor(Color.black);

String oneLine;

int x = 75, y = 80, lineNum = 0;

try

{

raf.seek(startAtLineNumber);

int fileLength = (int)printFile.length();

while((((int)raf.getFilePointer()) < fileLength) && (lineNum < 43))

{

oneLine = raf.readLine();

if(oneLine.length() > 130)

{

// wrap lines if one line is too long

String secondLine = oneLine.substring(130, oneLine.length());

oneLine = oneLine.substring(0, 130);

g.drawString(oneLine, x, y);

y += 15;

g.drawString(secondLine, x, y);

y += 15;

}// end if

else

{

// line fit on one line

g.drawString(oneLine, x, y);

y += 15;

}// end else

lineNum++;

}// end while loop

lastFilePositions[pageIndex] = (int)raf.getFilePointer();

}// end try

catch(IOException e)

{

//dialog.showError("Fehler: "+e.getMessage(), "Problem");

//e.printStackTrace();

System.exit(0);

}// end catch

return Printable.PAGE_EXISTS;

}// end else

}// end print

}// end MyPrintable class

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}// end MyPrinter class

The printdialog is showing and saied me the number of pages to print (at least one)

but when i accept it, the exception is thrown.

Thanks for any help in correcting this code, I haven't succesfully understood the printing system

in Java when there is multiple pages.

PS: sorry i haven't no more duke dollards available.

[13591 byte] By [marc_huhardeaux] at [2007-9-26 6:43:16]
# 1
I can't tell what's line 204, and you have three calls to drawString(). It is possible for the third of those to call drawString() with a zero-length string as its first parameter. Try adding some code to avoid that.
DrClap at 2007-7-1 16:04:06 > top of Java-index,Archived Forums,Swing...
# 2
Well i found the problem : under JDK1.2.2, drawString() is unable to print an empty linewhile under jdk1.3.1, it can.So i have corrected what i was printing and i now it works. Anyway thanks for the help
marc_huhardeaux at 2007-7-1 16:04:06 > top of Java-index,Archived Forums,Swing...