Results 1 to 3 of 3
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default How to make new-line when p rinting strings?

    So i've got my printing java file in place, and working. Now i know that %n in a string doesn't cause the program to create a new-line when the strings are printed.

    Obviously there are various methods for sending output to a printer, also depending on the printer type. So here is the method i'm using right now:

    //PrintPage.java
    The output width is specified with an array of strings to be printed.
    This information is accepted in the constructor before being processed to form paragraphs broken after the specified width.
    The problem here is that "%n" in the input strings are ineffective with printing - so how do I make a new line when I need one?

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package ozzypos;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.awt.print.*;
    import java.text.*;
    import javax.swing.*;
     
    /**
     *
     * @author Ozzy
     */
     
    public class PrintPage implements Printable, ActionListener {
        public static float breakWidth = 0;
        private static float padLeft = 4;
        private static float padRight = 4;
        public static String msg = "";
        private static JDialog f;
        AttributedString as = null;
        AttributedCharacterIterator paragraph = null;
        int paragraphStart, paragraphEnd;
        FontRenderContext frc = null;
        LineBreakMeasurer lineMeasurer = null;
        float drawPosX, drawPosY;
     
        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
            if (page > 0) return NO_SUCH_PAGE;
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX()+mmToPixel(padLeft), pf.getImageableY());
            as = new AttributedString(msg);
            paragraph = as.getIterator();
            paragraphStart = paragraph.getBeginIndex();
            paragraphEnd = paragraph.getEndIndex();
            frc = g2d.getFontRenderContext();
            lineMeasurer = new LineBreakMeasurer(paragraph, frc);
            drawPosY = 0;
     
            lineMeasurer.setPosition(paragraphStart);
            while (lineMeasurer.getPosition() < paragraphEnd) {
                TextLayout layout = lineMeasurer.nextLayout(breakWidth);
                drawPosX = layout.isLeftToRight()? 0 : breakWidth - layout.getAdvance();
                drawPosY += layout.getAscent();
                layout.draw(g2d, drawPosX, drawPosY);
                drawPosY += layout.getDescent() + layout.getLeading();
            }
     
            return PAGE_EXISTS;
        }
     
        public static void main(String args[]) {
            String printMsg = "";
            for (String s:args) printMsg += (s + " ");
            msg = printMsg;
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            new PrintPage();
        }
     
        private void dialogWindowOpened(WindowEvent e) {
             PrinterJob job = PrinterJob.getPrinterJob();
             job.setPrintable(this);
             //boolean ok = job.printDialog();
             boolean ok = true;
             if (ok) {
                 try {
                      job.print();
                 } catch (PrinterException ex) {
                  /* The job did not successfully complete */
                 }
             }
             f.setVisible(false);
        }
     
        PrintPage() {
            initComponents();
        }
     
        PrintPage(String[] args, float mmWidth) {
            breakWidth = mmToPixel(mmWidth-padRight);
            main(args);
        }
     
        private void initComponents() {
            f = new JDialog();
            f.addWindowListener(new WindowAdapter() {
               public void windowOpened(WindowEvent e) {dialogWindowOpened(e);}
            });
            JLabel printLabel = new JLabel("Printing...");
            f.setTitle("Printing...");
            f.add("Center", printLabel);
            f.setModal(true);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
     
        private float mmToPixel(float mm) {
            float pixels = ((mm/10f)/2.54f)*72f;
            return pixels;
        }
    }
    Last edited by ozzyman; 05-03-2011 at 02:35 PM.

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

    Default

    how do I make a new line when I need one?
    You compute appropriate values for drawPosX and drawPosY as used in
    Java Code:
    layout.draw(g2d, drawPosX, drawPosY);
    db

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Ohhh I was thinking i'd do that as back-up in case I couldn't get the new-line char - I assumed there was a new-line char. Alright, thanks :).

Similar Threads

  1. Can't make regex ignore line terminator
    By mobilityguy in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 04:27 PM
  2. How do I make ImageIcon strings shorter.
    By Singing Boyo in forum New To Java
    Replies: 7
    Last Post: 03-10-2009, 09:29 AM
  3. How to make Scanner read the same line
    By mcollins in forum New To Java
    Replies: 2
    Last Post: 03-03-2009, 06:41 AM
  4. Make a text in Jlabel down to next Line
    By hungleon88 in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2008, 11:10 PM
  5. Replies: 0
    Last Post: 08-06-2007, 06:54 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
  •