• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how to properly format and center all elements inside a receipt printed using graphics2d

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I restarted my work on printing receipt using graphics 2d and managed to change some of its properties but I'm still stuck with the dimensions and how to format them. Right now I am able to print the receipt in a crude and ugly way which is all I'm capable ATM, thus, I was wondering if anyone can give me some pointers on how to properly manage the format for receipts in graphics2d. What I really want here is that all the elements will be centered and that they will occupy the maximum width space like a normal receipt. Since my receipt is printed where the text is at "align right" format and the margin in the left and right are uneven, what's worse is that if the text is too long it is cut even though there is still some space left in the other side.

it is printed this way for visual presentation:



Here is my code:
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I currently have no immediate need to print anything in my projects, but what I have done in the past is render to a BufferedImage and scale appropriately for the printer resolution and page size. Gave me a very professional look which is exactly what were looking for as our documents often would have to be presented in court as evidence.
 
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usig a BufferedImage and pritng that is certainly a fine method.
However, that does not solve OP's problem of getting his lines
centered.

One possibility is a semi-automatic one.
Just print the contents to a plain text file, and next open that file
in any decent wordprocessor, with a preset style of 'Centered'.
In that way you can fully use the printing facilities of that
wordprocessor.

Another way is to make use of the FontMetrics class.
If you have a Graphics g, then you can get a FontMetrics object.
With a given font, you can ask for the length (and height) of
a string. Do that for all your strings, so that you know the dimension
of your largest string.

Having that, it is then easy to calculate where every string should be
printed, to get them all centered.
To check that it is working, you can use a JPanel for that, or you
can use Les' way: get a Graphics g from a BufferedImage and use
that. It certainly has the huge advantage of a very easy way to
save such a receipt. How big you should make that BufferedImage,
well, that is something to experiment. The disadvantage is the scaling.
If you have to scale your BI too much, you get very 'blocky' output.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

Another way is to make use of the FontMetrics class.
If you have a Graphics g, then you can get a FontMetrics object.
With a given font, you can ask for the length (and height) of
a string. Do that for all your strings, so that you know the dimension
of your largest string.

Having that, it is then easy to calculate where every string should be
printed, to get them all centered.
To check that it is working, you can use a JPanel for that, or you
can use Les' way: get a Graphics g from a BufferedImage and use
that. It certainly has the huge advantage of a very easy way to
save such a receipt. How big you should make that BufferedImage,
well, that is something to experiment. The disadvantage is the scaling.
If you have to scale your BI too much, you get very 'blocky' output.



I thought that there will a pretty straightforward way of doing this just like adding an align center code, anyway thank you for that information and I'll look it up, you mention that I can print every string in a set position if you've noticed in my above code the positioning is not set per line of string instead it is for the whole text to be printed, thus, I am wondering on how can you individually set the position for every line of your text? Thank you
 
Piet Souris
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rodolfo,

no indeed, you make a string out of a concatenation of a couple
of other strings.

Well, maybe there is an easy way to get all your strings centered,
but I do not know of such a method. Anyone?

The method to use for string placement is 'g.drawString(string, x, y)'
where x and y is, say, the left bottom point of the string.
Look at the API of a Graphics g and of a FontMetrics fm, for more information.
Especially the info on fm should tell you many things about string widths, heights,
leading et cetera.

I give you a demo of how to center some strings. Feel free to change it
and experiment as much as you like. I've made a Java 7 version, in case you
do not have Java 8.
 
Ranch Hand
Posts: 129
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've had pretty good luck using the TextLayout class. My technique for getting a centered text is to use the TextLayout class and then call getBounds() which returns a Rectangle2D object giving the bounds of the text. I then call getCenterX() to get the center of the bounds and subtract that from the position at which I wish to draw the text. Visually, it's the best solution I've been able to find.

Aesthetic considerations drive a lot of the design of the font-related classes in Java (I guess we can thank Steve Jobs and Donald Knuth for that). Normally, when you render text at coordinates (x,y), the y coordinate is treated as the baseline and the x coordinate as the left starting point. However, if you inspect the bounds returned from TextLayout in my example code below, you'll see that the x coordinate of the bounds varies from string to string. Java does this to ensure that there will be an attractive vertical alignment when rendering a block of several lines of text. For example, if you draw the the letters O and I on two lines of text, the left-most pixel of the I will actually be slightly to the right of that of the O. So using the getCenterX() from the bounds accounts for that adjustment factor and ensures good alignment.

You can use the same approach for left and right justified text.

Look at the paintComponent and drawString methods in the code example below

 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary W. Lucas wrote:



Tried this:

and got this error: Caused by: java.lang.RuntimeException: Uncompilable source code - withdraw.ATM is not abstract and does not override abstract method print(java.awt.Graphics,java.awt.print.PageFormat,int) in java.awt.print.Printable

 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:



Hi Piet, Seems like your the one that has been replying to me a lot, I really appreciate it. Could you guide me in using your code and applying it to mine, as I don't know which part is to remove,rename, and retain since I'm not using Jpanel to view the text. and this long code is really confusing me. Thank you
 
Piet Souris
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Gary,

thanks for this demo! I have never worked before with "TextLayout', so this
is a very interesting novelty for me.

If I turn on anti aliasing in my version, and include this vertical line as well,
then I see no differences, at first sight. But I find your way more elegant.

Is it easier to use as well? I wonder what people around here think.
It sure gives rodolfo a lot to experiment with.

Why he gets an error I don't know, I didn't get one.
 
Piet Souris
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rodolfo,

to get these lines centered, have a look at my method:

It returns a Dimension, having the width of the longest string, and the
heigth of a line of text.

In your case, I would add the parameters 'Graphics g' and 'List<String>'
You are in fact using a List as well (or, better, an array) but that is a bit
long-winded. First you make one string of all your lines, and then split
that string back into its string parts.
Having this dimensio with these values, just go ahead and print them,
like I did in my 'paitStrings(g) method.

If some line gets clipped, that is probably because you set your page width
too short? (I see: paparWidth = 3.25; line 5 of your opening listing)

But Gary's Textlayout class seems very elegant. Have a close look at how
he gets his lines centered. That leaves me to an important remark:

what will be your centerpoint for each line?

In my program I centered my lines with respect to the left margin, Gary
centers his lines with respect to the panel width (what in your case could
be the paperWidth). If you can't make up your mind here, I'd say:
try them out both, and see what you like best.

So it all boils down to adjusting your 'drawString' method.

Give it a try and keep us informed!
 
Piet Souris
Saloon Keeper
Posts: 5182
207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rodolfo wrote:Hi Piet, Seems like your the one that has been replying to me a lot, I really appreciate it


Thank you, and you're welcome.

It is always fascinating to see how topics get replied to. Some topics get many replies,
and many repliers, other topics go unreplied, and some topics only get one replier.

It would make a great subject of study for any sociology student, to find out the underlying
forces!

But one aspect is: is the subject interesting, do many people use it or not, et cetera.
In this case, besides helping you, I get the chance of inspecting my knowledge
on this subject, and to try out many new things before I put them in my answers.
So, in fact it is as much a learning exercise for me as it is for you.

And if we're lucky, one or more other people step in, showing some new ways in
which a problem can be solved.

That makes it all worthwile.

 
Those are the largest trousers in the world! Especially when next to this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic