简体   繁体   中英

Issues with passing arguments to the print function in Java

public int print(Graphics g, PageFormat pf, int page, String customer_name) 
    throws PrinterException {

    System.out.println("The value of customer name:"+customer_name);

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    String x = layout.get("");
    System.out.println("The value of x is\n"+x);
    /* Now we perform our rendering */
    g.drawString("Customer Name: "+customer_names, 100, 100);


    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

I want to call this method from another class, while passing an additional argument to it, customer_name . I call this method from another class as follow:

Printer print = new Printer();  //making an object to access that class Printer.java
PageFormat page = job.defaultPage();
print.print(<I have no idea what to put here for graphics>, page, 5, customer_name_field.getText());

When I call the method print.print , I gives the message that it requires Graphics, PageFormat, int, String . But what should I put for Graphics , I have no idea?

It's not working because it looks like you're going about it wrong:

  • Your print method has an extra String parameter tacked to the end which prevents it from being a true method override for a class that implements Printable.
  • You're trying to call your print method directly scrounging around for a Graphics context when you shouldn't consider doing this.

If you just want to print some text then you need to follow the first sections of the Printing Tutorial . Your print method above does not conform with a Printable's print(...) method override. Please do yourself a favor and follow the tutorial. I've given you the link.

Consider creating a class that implements Printable, passing your String as a single parameter to the class's constructor, and use this to set an instance field. The print(...) method should match that found in the tutorial, should have an @Override annotation, and most important will never be called directly by you . Your PrinterJob instance will do the printing behind the scenes.

Note, that if your goal is to print a Swing GUI, then the steps are different, since Swing GUI's carry much of the innate machinery for printing within them.

Depends on the class you're in, there's a high chance that the UI framework element you are currently using already provides you with the getGraphics() function.

You might even want to override the paint() function of the class you're using, and call this method you have written from that. It depends on your use-case.

This is my github project path where i did the code for same. https://github.com/knikam/Mobile_shop_management/blob/master/src/mobile_shop_mangment/Sell_mobile.java

public int print(Graphics g, PageFormat pf, int page) 
    throws PrinterException {



    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    String x = layout.get("");
    System.out.println("The value of x is\n"+x);
    /* Now we perform our rendering */
    String customer_namescustomer_names.getText();**This is work for me**
    g.drawString("Customer Name: "+customer_names, 100, 100);


    /* tell the caller that this page is part of the printed document */
    return PAGE_EXISTS;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM