Drucken drawString();

Hey Leute ich versuch gerade mit drawString einen String nach Rechts zu formatieren ?? ich habs so probiert wie wie es in dem Kommentar steht aber das programm erkennt "rightEdge" nicht ? Kann mir jemand sagen was ich falsch mache ?

Java:
package auftragsverwaltung;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.PrinterName;

/**
 *
 * @author xom
 */
public class Drucken implements Printable {
    
    Connection con;
    Statement st;
    ResultSet rst;

    public void print() throws PrinterException, SQLException {
            con = DriverManager.getConnection("jdbc:sqlite:meineDB.db");
            String sql = "SELECT * FROM Druckereinstellungen ";
            st = con.createStatement();
            rst = st.executeQuery(sql);


            String druckername = rst.getString(2);

            rst.close();
            con.close();
        
        
        
        PrintService[] printServices;
        PrintService printService;
        PageFormat pageFormat;

        

        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(new PrinterName(druckername, null));
        printServices = (PrintService[]) PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
        PrinterJob job = PrinterJob.getPrinterJob();  
        pageFormat = new PageFormat(); // If you want to adjust heigh and width etc. of your paper.
        pageFormat = job.defaultPage();

        PrinterJob printerjob = PrinterJob.getPrinterJob();

        printerjob.setPrintable(new Drucken(), pageFormat); // Server was my class's name, you use yours.

        try {
            printService = printServices[0];
            printerjob.setPrintService((javax.print.PrintService) printService); // Try setting the printer you want
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("Error: No printer named '" + druckername + "', using default printer.");
            pageFormat = printerjob.defaultPage(); // Set the default printer instead.
        }

        try {
            printerjob.print(); // Actual print command
        } catch (PrinterException exception) {
            System.err.println("Printing error: " + exception);
        }
        }

 
    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException 
    {
        try {
            if (page > 0) { /* We have only one page, and 'page' is zero-based */
                return NO_SUCH_PAGE;
            }
            
            con = DriverManager.getConnection("jdbc:sqlite:meineDB.db");
            String sql = "SELECT * FROM Druckereinstellungen ";
            st = con.createStatement();
            rst = st.executeQuery(sql);                   
            String schriftart = rst.getString(3);
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");
            LocalDateTime jetzt = LocalDateTime.now();
            String datum = jetzt.format(dtf);

            
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            FontMetrics fontMetrics = g.getFontMetrics();
            
            /* Now we perform our rendering */
            
            g.setFont(new Font(schriftart, 0, 14));
            
            String[] linien;
            int y = 10;
            while (rst.next()) {
                
                String linie = rst.getString(1);
                g.drawString(linie, 10, y);
                y += 20;
            
                
            }
            sql = "SELECT * FROM Druckerinformationen ";
            rst = st.executeQuery(sql);
            int nummer = rst.getInt(8);
            String n = Integer.toString(nummer);
            y +=20;
            g.drawString("Datum:" +datum, 10, y);
            y+=20;
            g.drawString("Waehrung: EURO", 10, y);
            y+=20;
            g.drawString("Beleg: "+n,10 , y);
            y+=20;
            g.drawString("---------------------------------------------------------------------------------------------------------", 10, y);
            y+=20;
            
            while (rst.next()) {
                String beschreibung = rst.getString(1);
                double brutto = rst.getDouble(5);               
                String b = Double.toString(brutto);
                int menge = rst.getInt(2);
                String m = Integer.toString(menge);
                double einzelpreis = rst.getDouble(3);
                String e = Double.toString(einzelpreis);
                g.drawString(beschreibung, 10, y);
                y+=20;
                g.drawString("Menge: "+m+"  Einzelpreis: "+e, 10, y);
                //g.drawString(b, rightEdge - fontMetrics.stringWidth(b), y);
                y+=20;
                g.drawString("---------------------------------------------------------------------------------------------------------", 10, y);
                y+=20;
                
            }
                 
            
            rst.close();
            con.close();
            } catch (SQLException ex) {
            Logger.getLogger(Drucken.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            
            return PAGE_EXISTS;
        
    }
}
 
Passende Stellenanzeigen aus deiner Region:

Neue Themen

Oben