Page 1 of 2 12 LastLast
Results 1 to 20 of 32
  1. #1
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Printing formatted strings to laser printer

    I have managed to get formatted strings going in the output window, using System.out.printf("%4d", MyVariableInteger); for example

    I have also managed to print a string to the laser printer, using eg (part only of code!)

    Graphics@D g2d = (Graphics@D)g;
    g.drawString("Here", 50, 50); // the 50 50 is the X/Y

    I would like a hint on combining these so that I can print to the laser with formatted strings.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default Re: Printing formatted strings to laser printer

    Check out PrinterJob and PageFormat in the API.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

    Perhaps I would like the hint to be a little more specific!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Printing formatted strings to laser printer

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

    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)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Printing formatted strings to laser printer

    Quote Originally Posted by alan2 View Post
    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.

    db

    * http://docs.oracle.com/javase/7/docs...ng/String.html
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

    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)

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Printing formatted strings to laser printer

    OK here's a more specific link.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

    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!

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default 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?
    Last edited by alan2; 03-30-2013 at 09:01 PM.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default 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

  14. #14
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default 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!

    Java Code:
    package tryingstringformat;
    
    public class TryingStringFormat {
    
        public static void main(String[] args) {
            String aBoo = "34765";
            String aBoo2 ="123.45";
            String aBoo3="45.1";
            String aLineHeader = "Lemon";
            String aLineHeader2 ="Cherry";
            // for conversion to float do not need "f" appended
            float aVar1=Float.parseFloat(aBoo);
            float aVar2=Float.parseFloat(aBoo2);
            float aVar3=Float.parseFloat(aBoo3);
            float aVar4=1.1f; // or could have been direct note f
            // a suitable decimal aligned report line 
            String aDecLine= "%-10s %,10.2f %,10.2f %,10.2f %,10.2f %n";
             // no commas between format patterns
            // this sets out the required report columns
            // with linefeed at end
            String aTry = String.format(aDecLine,aLineHeader, aVar1,aVar2, aVar3,aVar4);
            String aTry2 = String.format(aDecLine,aLineHeader2,aVar2,aVar4, aVar1,aVar3);
            // as %n in string then no new line
            System.out.print(aTry); 
            System.out.println(aTry2);
            }
    }
    Last edited by alan2; 03-31-2013 at 08:06 AM.

  15. #15
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

    Hello all

    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.

    Java Code:
    package laserprintagain;
    
    import java.io.*;
    import java.awt.*;
    import java.net.*;
    import java.awt.image.*;
    import java.awt.print.*;
    import javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import java.util.*;
    import javax.swing.*;
    
    public class LaserPrintAgain implements Printable {
    
            public LaserPrintAgain () {
                   
                    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
                    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                    aset.add(OrientationRequested.PORTRAIT);
                    aset.add(new Copies(1));
                    aset.add(new JobName("My job", null));
                    PrintService [] services = 
                    PrintServiceLookup.lookupPrintServices(flavor,aset);
                    PrintService aBloggs = PrintServiceLookup.lookupDefaultPrintService();
                    // looks up default printer
                    if (services.length > 0 )  {
                        System.out.println(aBloggs); // for monitoring
                        DocPrintJob pj = aBloggs.createPrintJob(); // do job on default printer 
                           
                            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) {
                            Graphics2D g2d= (Graphics2D)g;
                            g2d.translate(pf.getImageableX(), pf.getImageableY()); 
                           // variables
                            String aLineHead1 = "Oranges";
                            String aLineHead2 = "Apples";
                            float aNo1 = 6123.45f;
                            float aNo2 = 900.97f;
                            float aNo3 = 11.11f;
                            float aNo4 = 3456f;
                         
                            String aDecCols="%-15s %, 15.2f %,15.2f "; // set up columns for 'report'
    
                            String aRepLine1 = String.format(aDecCols, aLineHead1, aNo1, aNo2); // can't reuse ReplLinex
                            g2d.drawString(aRepLine1, 50, 50); 
                            String aRepLine2 = String.format(aDecCols, aLineHead2, aNo3, aNo4); // can't reuse ReplLinex
                            g2d.drawString(aRepLine2, 50, 65); // must increment y 
                            
                            return Printable.PAGE_EXISTS;                                                   
                    } else {
                            return Printable.NO_SUCH_PAGE;
                    }
            }
    
            public static void main (String arg[]) {
                    LaserPrintAgain sp = new LaserPrintAgain();
            }
    }
    Last edited by alan2; 04-02-2013 at 09:38 PM.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Printing formatted strings to laser printer

    Maybe you need to use a monospaced font.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

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

  18. #18
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default 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...

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Printing formatted strings to laser printer

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    alan2 is offline Member
    Join Date
    Mar 2013
    Posts
    42
    Rep Power
    0

    Default Re: Printing formatted strings to laser printer

    Well, here's what I tried. Lines print OK but in Arial as printer default. In other words the line starting with Font font seems inoperative. Help!

    Java Code:
    Graphics2D g2d= (Graphics2D)g;
                       Font font = new Font(Font.SANS_SERIF, Font.BOLD, 40);
                        g2d.translate(pf.getImageableX(), pf.getImageableY()); 
                           String aLineHead1 = "Oranges";
                            String aLineHead2 = "Apples";
                            float aVar1 = 6123.45f;
                            float aVar2 = 900.97f;
                            float aVar3 = 11.11f;
                            float aVar4 = 3456f;
                         
                            String aDecCols="%-15s %, 15.2f %,15.2f "; // set up columns
    
                            String aRepLine1 = String.format(aDecCols, aLineHead1, aVar1, aVar2); // can't reuse ReplLinex
                            g2d.drawString(aRepLine1, 50, 50); 
                           String aRepLine2 = String.format(aDecCols, aLineHead2, aVar3, aVar4); // can't reuse ReplLinex
                            g2d.drawString(aRepLine2, 50, 65); // must increment y
    Last edited by alan2; 04-03-2013 at 06:33 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. printing to a laser printer
    By alan2 in forum New To Java
    Replies: 14
    Last Post: 03-20-2013, 07:47 AM
  2. printing to printer
    By ak88 in forum Advanced Java
    Replies: 3
    Last Post: 01-13-2013, 03:32 PM
  3. Printer loop the same printing
    By kiki2009 in forum Advanced Java
    Replies: 2
    Last Post: 04-29-2011, 05:43 PM
  4. Printing in a printer
    By UJJAL DHAR in forum Advanced Java
    Replies: 5
    Last Post: 01-29-2011, 12:03 PM
  5. Printing Text using printer
    By UJJAL DHAR in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •