Search code examples
javaimplements

java getting error while using implements


Am new to java so please help me to solve my problem. Here is my code for printing the reciept bill

public  class BillPrintable implements Printable{

public int print(Graphics graphics,PageFormat pageformat,int pageIndex)throws PrinterException
    {
        int result = NO_SUCH_PAGE;
        if(pageIndex == 0)
        {
            Graphics2D g2d= (Graphics2D) graphics;
            double width = pageformat.getImageableWidth();
            g2d.translate((int) pageformat.getImageableX(), (int) pageformat.getImageableY());
            FontMetrics metrics=g2d.getFontMetrics(new Font("Arial",Font.BOLD,7));
            int idLength=metrics.stringWidth("000");
            int amtLength=metrics.stringWidth("000000");
            int qtyLength=metrics.stringWidth("000000");
            int priceLength=metrics.stringWidth("00000");
            int prodLength=(int)width-idLength-amtLength-priceLength-17;

            int productPosition = 0;
            int discountPosition=prodLength+0;
            int pricePosition =discountPosition+idLength + 10;
            int qtyPosition = pricePosition + priceLength + 4;
            int amtPosition = qtyPosition + qtyLength;
            try {
                int y=20;
                int yShift = 10;
                int headerRectHeight = 15;
                int headerRectHeighta = 40;
                g2d.setFont(new Font("Monospaced",Font.PLAIN,9));
                g2d.drawString("............................", 12, y);y+=yShift;
                g2d.drawString("  Restaurant Bill Reciept   ", 12, y);y+=yShift;
                g2d.drawString("----------------------------", 12, y);y+=headerRectHeighta;


                g2d.drawString("......................................", 10, y);y+=yShift;


                g2d.drawString("......................................", 10, y);;y+=yShift;

                g2d.drawString("          Free Home Delivery          ", 10, y);;y+=yShift;
                g2d.drawString("             9895776277               ", 10, y);;y+=yShift;
                g2d.drawString("......................................", 10, y);;y+=yShift;
                g2d.drawString("      Thanks To Visit Our Restaurant  ", 10, y);;y+=yShift;
                g2d.drawString("......................................", 10, y);;y+=yShift;

            }
            catch(Exception r)
            {
                r.printStackTrace();
            }
            result = PAGE_EXISTS;
        }
        return result;
    }


}

Here the error i got is like this The type bill.BillPrintable must implement the inherited abstract method Printable.print(Graphics, PageFormat, int).Dont know how the problem arises and how to solve the problem.please help me to solve thanks in advance This is my imported statements.please have a look

import java.awt.Font;
import java.awt.Graphics2D;
import javafx.print.Paper;
import com.sun.prism.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.*;
import java.awt.print.*;
import java.awt.print.PageFormat.*;
import java.awt.print.Paper.*;
import javax.swing.UIManager;
import java.awt.FontMetrics;

Solution

  • You import class Graphics from a wrong package:

    import com.sun.prism.Graphics
    

    must be

    import java.awt.Graphics
    

    Also the following imports are unessasary:

    import java.text.*;
    import java.awt.print.*;
    import java.awt.print.PageFormat.*;
    import java.awt.print.Paper.*;
    

    Pay attention: You should not use com.sun classes directly