carookee - group communication for you
Home / JavaForum / Java allgemein
Infos   |   Features   |   Gold-Edition   |   Kundenservice   
java
  Übersicht
  Forum
Beginner
Java allgemein
JDBC
JNI
Networking
Online-Ressourcen
Swing + AWT
XML
Meckerecke
  Mitglieder
LOGIN





· Passwort vergessen
· Kostenlos anmelden
  Information
  Demo
  Features
  Im Vergleich
  Anmeldung
SUCHE
Beiträge, Foren oder Verfasser finden:
  Kundenservice
  Impressum
  Datenschutz
  AGB
Status
5.234 User online
0 User eingeloggt
 

Beiträge
   Antworten     Neuer Beitrag    

Beitrag 849 von 2212 (38%) |<   <   >   >|

Autor Gast
Datum 30.11.01, 22:04
Betreff Probleme beim Drucken ...


Ich möchte Inhalt der TextArea ausdrucken.
1. die Ersten zwei Zeichen(von Links) werden gar nicht gedruckt,
2. ist der Inhalt größer als 1 Seite - da gehen beim mir, ganze Zwei Zeilen von
Seite 2 verloren.


Kann jemand helfen.


Hier ist die Klasse:
public class Print {
    
    Toolkit toolKit = Toolkit.getDefaultToolkit();
    PrintJob printJob = toolKit.getPrintJob(new JFrame(), "", null);
    
    Font font = new Font("Monospaced",0,12);
    String s = myTextArea.getText();
    
    int pageNum = 1;
    int linesForPage = 0;
    int linesForJob = 0;
    
    StringReader sr = new StringReader (s);
    LineNumberReader lnr = new LineNumberReader (sr);
    String nextLine;
    int pageHeight = printJob.getPageDimension().height;
    
    public Print() {
        if (printJob != null) {
            Graphics g = printJob.getGraphics();
            if(g != null) {
                printInhalt(printJob, g, s);
                g.dispose();
            }
            printJob.end();
        }
    }
    
    
    //Print string to graphics via printjob
    void printInhalt(PrintJob printJob, Graphics g, String s) {
        //Note: String is immutable so won't change while printing.
        if(!(g instanceof PrintGraphics)) {
            throw new IllegalArgumentException("Graphics context not PrintGraphics");
        }
        
        //have to set the font to get any output
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);
        int fontHeight = fm.getHeight();
        int fontDescent = fm.getDescent();
        int curHeight = 0;
        
        try {
            do {
                nextLine = lnr.readLine();
                if(nextLine != null) {
                    if((curHeight + fontHeight) > pageHeight) {
                        // New Page
                        pageNum++;
                        linesForPage = 0;
                        g.dispose();
                        g = printJob.getGraphics();
                        if(g != null) {
                            g.setFont(font);
                        }
                        curHeight = 0;
                    }
                    curHeight += fontHeight;
                    if(g != null) {
                        g.drawString(nextLine, 0, curHeight - fontDescent);
                        linesForPage++;
                        linesForJob++;
                    }
                }
            }
            while(nextLine != null);
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}



 Auf diesen Beitrag antworten
 Neuen Beitrag verfassen


|<   <   >   >|

                                                                                                                                                                                                                           

Impressum  · Datenschutz  · AGB  · Infos  · Presse
Ein modernes Forum: teamturn.com