Printing (again)

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 .....");
}

}
 
O

Oliver Wong

mhsampson said:
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 don't know what you mean by "the left-most nine points", but did you
try setting the imageable/clipping area to be larger? Did you try scaling
your picture so that it is smaller?

- Oliver
 
M

mhsampson

Oliver,

Thanks for the reply.
I don't know what you mean by "the left-most nine points", but did you
try setting the imageable/clipping area to be larger? Did you try scaling
your picture so that it is smaller?

A "point" is a graphical unit = 1/72". I am not starting with an
image, but with data. So I use the drawString() and drawRect() methods
of class Graphics2D to build an image of the size I need to display (to
fit on the label forms). The size is: width=4 1/8" = 297 points;
height=3/4" = 54 points. That is the size I set the PageFormat.Paper
and the Graphics.Clip. What shows up on the printer is everything but
the left-most 1/8" (9 points).

Is there a better way to do this?

Mike Sampson
 
O

Oliver Wong

mhsampson said:
A "point" is a graphical unit = 1/72". I am not starting with an
image, but with data. So I use the drawString() and drawRect() methods
of class Graphics2D to build an image of the size I need to display (to
fit on the label forms). The size is: width=4 1/8" = 297 points;
height=3/4" = 54 points. That is the size I set the PageFormat.Paper
and the Graphics.Clip. What shows up on the printer is everything but
the left-most 1/8" (9 points).

Is there a better way to do this?

I'm trying to diagnose the problem, and one of the cases I'm trying to
isolate or eliminate is whether there's a limitation in the printer that
restricts the area it can print on, which is why I recommend you try scaling
your picture smaller. I notice you use drawRect, for example, to draw a
border around your text. Try setting the width of the rect smaller (e.g. by
10 points), and see if the whole rectangle then gets printed.

If this works, then maybe the printer can't print in the region of the
rightmost 9 points (at least under Java), and you'll have to limit your
printing to the printable area.

- Oliver
 
R

Roedy Green

When I execute this class from NetBeans, it prints on the Okidata. The
problem is that it clips off the left-most nine points.

Try dumping the codes you are going to feed to the Oki to a file, then
examine with a hex editor to make sure they are correct. Try feeding
them to the Oki, and see if it prints ok.

Whether it does or does not, you have cut the possible sources of your
problem in half.

See http://mindprod.com/printing.html for how to get that file
delivered to the oki.
 
R

Roedy Green

When I execute this class from NetBeans, it prints on the Okidata. The
problem is that it clips off the left-most nine points.

The other way to do this, which will be a lot faster, and probably
only marginally more difficult is to generate the Oki printer codes
directly, and print your test using the Oki's native fonts.

This will work even on tiny systems without printer driver support on
or OS's that don't have OS support for the Oki or that have buggy
printer driver support.
 
R

Roedy Green

When I execute this class from NetBeans, it prints on the Okidata. The
problem is that it clips off the left-most nine points.

Try printing on some other printer so you can tell if the problem is
your generic code or something peculiar to the Oki.
 
R

Roedy Green

A "point" is a graphical unit = 1/72".

You may be running into problem with metrics based on the size of the
paper vs the size of the printable region, and Java or the OS not
being completely clear on what those margins are for the Oki.
 
M

mhsampson

Oliver, Roedy,

Thanks again for all the replies.

I did as Oliver suggested, widening the Paper width, and increasing my
ImageableArea's X position until nothing was being clipped from the
left of the image. Turns out that number is 17.

Since this works, and I am jsut prototyping, I am going to hold Roedy's
excellent suggestions for when I need them.

Thanks to all. I'm learning a lot here.

Mike Sampson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top