M
mhsampson
This is my first posting here.
I am trying to print 4 1/8 X 3/4" labels on an Okidata dot matrix
printer. Yes, I know that "nobody does this any more". Jobs are hard
to get.
The labels will probably have to include bar codes, so I am printing
them graphically, using the Graphics2D. The class listed below is a
modified form of some downloaded sample code.
When I execute this class from NetBeans, it prints on the Okidata. The
problem is that it clips off the left-most nine points.
Can any of you see what might be causing this?
Mike Sampson
public class Print2DGraphics implements Printable
{
private String lineOne = "";
private String lineTwo = "";
private String lineThree = "";
public Print2DGraphics(String l1, String l2, String l3 )
{
PrintService ps = null;
if ( l1 != null )
lineOne = l1;
if ( l2 != null )
lineTwo = l2;
if ( l3 != null )
lineThree = l3;
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();
aset.add(new JobName("My job", null));
PrintService[] services =
PrintServiceLookup.lookupPrintServices(flavor, aset);
for ( int i=0; i<services.length; ++i)
{
String psName = services.getName();
if ( psName.equalsIgnoreCase("Okidata ML 320 Turbo/D
(IBM)"))
{
ps = services;
}
}
if (ps != null)
{
System.out.println("selected printer " + ps.getName());
DocPrintJob pj = ps.createPrintJob();
try
{
Doc doc = new SimpleDoc(this, flavor, null);
pj.print(doc, aset);
}
catch (PrintException e)
{
System.err.println(e);
}
}
}
public int print(Graphics g,PageFormat pf,int pageIndex)
{
if (pageIndex == 0)
{
Paper p = pf.getPaper();
p.setSize(297, 54);
p.setImageableArea( 0, 0, 297, 54 );
pf.setPaper(p);
Graphics2D g2d= (Graphics2D)g;
g2d.setClip(0, 0, 297, 54 );
int fontPoints = 10;
int linePoints = 12;
Font f = new Font("Sans Serif", Font.PLAIN,
fontPoints);
g2d.setFont(f);
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(java.awt.Color.black);
g2d.drawString(" "+lineOne, 2, linePoints);
g2d.drawString(" "+lineTwo, 2, 2*linePoints);
g2d.drawString(" "+lineThree, 2, 3*linePoints);
g2d.drawRect(1, 1, 295, 52);
return Printable.PAGE_EXISTS;
}
else
{
return Printable.NO_SUCH_PAGE;
}
}
public static void main(String arg[])
{
Print2DGraphics sp =
new Print2DGraphics("Line one .......",
"Line two .......", "Line three .....");
}
}
I am trying to print 4 1/8 X 3/4" labels on an Okidata dot matrix
printer. Yes, I know that "nobody does this any more". Jobs are hard
to get.
The labels will probably have to include bar codes, so I am printing
them graphically, using the Graphics2D. The class listed below is a
modified form of some downloaded sample code.
When I execute this class from NetBeans, it prints on the Okidata. The
problem is that it clips off the left-most nine points.
Can any of you see what might be causing this?
Mike Sampson
public class Print2DGraphics implements Printable
{
private String lineOne = "";
private String lineTwo = "";
private String lineThree = "";
public Print2DGraphics(String l1, String l2, String l3 )
{
PrintService ps = null;
if ( l1 != null )
lineOne = l1;
if ( l2 != null )
lineTwo = l2;
if ( l3 != null )
lineThree = l3;
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();
aset.add(new JobName("My job", null));
PrintService[] services =
PrintServiceLookup.lookupPrintServices(flavor, aset);
for ( int i=0; i<services.length; ++i)
{
String psName = services.getName();
if ( psName.equalsIgnoreCase("Okidata ML 320 Turbo/D
(IBM)"))
{
ps = services;
}
}
if (ps != null)
{
System.out.println("selected printer " + ps.getName());
DocPrintJob pj = ps.createPrintJob();
try
{
Doc doc = new SimpleDoc(this, flavor, null);
pj.print(doc, aset);
}
catch (PrintException e)
{
System.err.println(e);
}
}
}
public int print(Graphics g,PageFormat pf,int pageIndex)
{
if (pageIndex == 0)
{
Paper p = pf.getPaper();
p.setSize(297, 54);
p.setImageableArea( 0, 0, 297, 54 );
pf.setPaper(p);
Graphics2D g2d= (Graphics2D)g;
g2d.setClip(0, 0, 297, 54 );
int fontPoints = 10;
int linePoints = 12;
Font f = new Font("Sans Serif", Font.PLAIN,
fontPoints);
g2d.setFont(f);
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(java.awt.Color.black);
g2d.drawString(" "+lineOne, 2, linePoints);
g2d.drawString(" "+lineTwo, 2, 2*linePoints);
g2d.drawString(" "+lineThree, 2, 3*linePoints);
g2d.drawRect(1, 1, 295, 52);
return Printable.PAGE_EXISTS;
}
else
{
return Printable.NO_SUCH_PAGE;
}
}
public static void main(String arg[])
{
Print2DGraphics sp =
new Print2DGraphics("Line one .......",
"Line two .......", "Line three .....");
}
}