Results 1 to 3 of 3
  1. #1
    myselfmayur is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default Spacing/Alignment problem with java 2D printing

    Hi,

    AWT framework i am using for printing.
    I am getting problem while printing with java. I have a string which looks like (Replace * is with a space character)

    "Retailer 001************************************MY INVOICE: MI1006151T10000"
    "PH: 1234567, 2134568***********************NAME: MAYUR MAHESHWARI"

    This is the string which i created dynamically through string operation. Now when i am going to print it with font 'Courier' or 'DialogInput' then it gets printied properly, but if use fonts other then the mentioned above then alignment is not proper something like below:

    "Retailer 001*****************************MY INVOICE: MI1006151T10000"
    "PH: 1234567, 2134568************************NAME: MAYUR MAHESHWARI"

    Please suggest me a solution. The code i am using is mentioned below:

    {code}
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException{
    if (pageIndex != 0) {
    return NO_SUCH_PAGE;
    }

    Graphics2D graphics2D = (Graphics2D)graphics;
    graphics2D.setFont(typeFont);

    int x = (int)pageFormat.getImageableX();
    int y = (int)pageFormat.getImageableY();

    graphics2D.translate(x, y);
    List<String> data = this.getDataList();

    FontMetrics fm = graphics2D.getFontMetrics();
    int stringHeight = 0;

    int maxWidth = 0;
    FontMetrics fm1 = graphics2D.getFontMetrics();
    for(String line : data){
    int width = fm1.stringWidth(line);
    if(maxWidth < width)
    maxWidth = width;
    }

    System.out.println("maxWidth:- " + maxWidth);

    for(String line : data){
    stringHeight += fm.getHeight();
    int ascent = fm.getAscent();
    int yCoordinate = stringHeight + ascent;
    String tempLine = line.trim();
    if(tempLine.startsWith("-") && tempLine.endsWith("-")){
    System.out.println("line:- " + tempLine);
    graphics2D.drawLine(x + 10, yCoordinate, maxWidth, yCoordinate);//(line, x, y);
    }else{
    AttributedString attString = new AttributedString(line);
    attString.addAttribute(TextAttribute.FONT, typeFont);
    //graphics2D.drawString(attString.getIterator(), x, yCoordinate);
    //drawString((Graphics2D)g, line, x, yCoordinate, PrintPreview.width);
    try{
    graphics2D.drawBytes(line.getBytes(),0,line.getByt es().length, x, yCoordinate);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }// End of for loop
    return PAGE_EXISTS;
    }
    {code}

  2. #2
    master is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Wink Problem Area

    The pixel size for every character is differ while rendering a String. For ex. Serif will took 7 pixel to render a character 'A' or took 3 pixel to render a space or took 4 pixel to render character 'i'. You have to set the same pixel size for every character. I think after that your problem will be solved.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    How are you positioning the non blank parts of the Strings you are printing.
    If you want column alignment you might need to print the non blank parts at the same x location on a line.

Similar Threads

  1. BoxLayout Alignment problem
    By whiteMath in forum AWT / Swing
    Replies: 12
    Last Post: 07-19-2010, 11:04 PM
  2. GridLayout alignment problem
    By q8inq8 in forum New To Java
    Replies: 9
    Last Post: 03-16-2010, 08:54 PM
  3. Problem with margins/spacing
    By joseche in forum AWT / Swing
    Replies: 2
    Last Post: 12-26-2008, 09:22 PM
  4. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 02:23 PM
  5. Help with spacing in java
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:03 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
  •