OK I've gone right through that, and I must confess that I found it hard work. And I did not see how to combine the format method
eg System.out.printf("%10.2f", MyVariable) with the g.drawString(Something, 50,50)

03-29-2013, 11:22 AM

DarrylBurke

Re: Printing formatted strings to laser printer

Quote:

Originally Posted by alan2

And I did not see how to combine the format method
eg System.out.printf("%10.2f", MyVariable) with the g.drawString(Something, 50,50)

You don't combine that. Go through the API for the String class* and you might find a method of interest.

I have looked at this long and largely incomprehensible (at my stage) document. Nothing stands out. I can only think that format on page 37 was what you had in mind, but I cannot begin to see the coding I need to send a formatted string through the g.drawString. I realise that you are probably trying to educate me but this is pitched at too high a level for me.

This is what my current state of thought process produces

Public static Bloggs format(Bloggs "%10.2f" MyVariable) // My variable eg 345.67
// and then to pass the string to the report
g.drawString(Bloggs)

Sorry, I'm no wiser! This seems to be an extract of what you already pointed me at. I really need to see an example of the actual syntax to send a formatted integer, float or string to the lineprinter.

I'm comfortable with the System.out.printf possibilities (but of course that is to the Output window) and also with the g.drawString("Someword",50,50) code (to get something onto a laser printer, and I can place a String anywhere on the page. But I can't yet see how to put a formatted number on the page. Once I have that, I'm pretty sure I'll be off and running with financial reports. Please could you make your answer less abstract and (gasp) more of spoonfeed for this one!

03-30-2013, 04:44 AM

jim829

Re: Printing formatted strings to laser printer

Format the value and store it in a string field using the Format method. The API tells you how to do it. Then print that String out in the paintComponent method using g.drawString().

Also, it is critical to read the JDK API and then write some test snippets to try it out. For example, forget about using drawString for the moment. Just try formatting a String using the Format method and then print the string. Then compare using the System.out.printf() method with the same format syntax.

Regards,
Jim

03-30-2013, 09:59 PM

alan2

Re: Printing formatted strings to laser printer

Thanks! I'll get stuck into this today. I assume that string *field* rather than just string variable is critical?

03-30-2013, 10:31 PM

jim829

Re: Printing formatted strings to laser printer

When I say field I mean instance variable. The terms are synonymous. They are also called class member variables. However, you can't access a local variable outside of its enclosing method or block so the paintComponent method can only paint those instance variables (or fields) it has access to.

Regards,
Jim

03-30-2013, 10:43 PM

alan2

Re: Printing formatted strings to laser printer

Jim, I have made great progress! The key for me was knowing that I could use String s=String.format(...and I know about the various format options). The String.format was the break through! Just experimenting with that before I move back to 2D and g

03-30-2013, 11:42 PM

alan2

Re: Printing formatted strings to laser printer

Thanks again Jim. Here is my experiment, which works! Now to try to combine this with printing to a laser printer!

This is what I have cobbbled togther now and it prints formatted lines of data on the default laser printer. However, I cannot get the decimal points to line up. These are affected by the lengths and compositions of both the string and numeric data. Any ideas (suitably expressed for a beginner) welcomed.

Can you assist with how I set that up. Presumably with another aset.add line?

04-03-2013, 03:39 AM

alan2

Re: Printing formatted strings to laser printer

Would really appreciate the syntax and position of a line that specifies monospace font. It seems to me that it must be possible but Googling extensively and experimenting with many syntax structures has failed...