Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [JAVA] Errore versione file

    Ciao, sto provando ad utilizzare la seguente public class

    [CODE]
    /**
    * This class provides some facilities to menage the print job
    *
    * @author Mario Fusco
    */

    import java.awt.*;
    import java.util.*;
    import java.awt.print.*;

    public class Printer {

    PrinterJob pj;
    PageFormat pf;

    // The font used to print the current item
    Font font;
    FontMetrics fm;

    // The Graphics where the items to print will be painted
    Graphics graphics;

    // Holds the sequence of items to print
    Vector itemsToPrint = new Vector();

    /**
    * The constructor
    */
    public Printer() {
    // create a printjob
    pj = PrinterJob.getPrinterJob();
    // get the default page
    pf = pj.defaultPage();

    // Print an empty book
    // this trick is used to communicate with the printer and obtain the Graphics object
    // where the items to print will be painted
    Book dummyBook = new Book();
    dummyBook.append(new DummyPage(), new PageFormat());
    pj.setPageable(dummyBook);
    try {
    pj.print();
    } catch (PrinterException pe) {
    pe.printStackTrace();
    }
    }

    /**
    * Send to the printer the current graphics
    */
    public void print() {
    try {
    pj.setPageable(getPages());
    if (pj.printDialog()) pj.print();
    } catch (PrinterException pe) {
    pe.printStackTrace();
    }
    }

    /**
    * Set the font in which the next text appended to the job will be printed
    *
    * @param font The new font
    */
    public void setFont(Font font) {
    this.font = font;
    }

    /**
    * Clear the current graphics and add a new String of text
    *
    * @param text The text to be printed
    */
    public void setText(String text) {
    itemsToPrint = new Vector();
    itemsToPrint.add(new TextToPrint(text, font));
    }

    /**
    * Add a new String of text to the current job
    *
    * @param text The text to be printed
    */
    public void appendText(String text) {
    itemsToPrint.add(new TextToPrint(text, font));
    }

    /**
    * Append a new table to the current job
    *
    * @param header The header for the column in the table
    * @param data The data of the table: each item of the Vector represent a row in the table
    * and must be a String[] of length equals to the number of columns.
    */
    public void appendTable(String[] header, Vector data) {
    itemsToPrint.add(new TableToPrint(font, header, data, null, null));
    }

    /**
    * Append a new table to the current job
    *
    * @param header The header for the column in the table
    * @param data The data of the table: each item of the Vector represent a row in the table
    * and must be a String[] of length equals to the number of columns.
    * @columnWidth Each item of this array represent the relavite width of the corresponding column
    */
    public void appendTable(String[] header, Vector data, int[] columnWidth) {
    itemsToPrint.add(new TableToPrint(font, header, data, columnWidth, null));
    }

    /**
    * Append a new table to the current job
    *
    * @param header The header for the column in the table
    * @param data The data of the table: each item of the Vector represent a row in the table
    * and must be a String[] of length equals to the number of columns.
    * @rightAlligned Each item of this array indicates if the cell of the corresponding
    * column must be right alligned
    */
    public void appendTable(String[] header, Vector data, boolean[] rightAlligned) {
    itemsToPrint.add(new TableToPrint(font, header, data, null, rightAlligned));
    }

    /**
    * Append a new table to the current job
    *
    * @param header The header for the column in the table
    * @param data The data of the table: each item of the Vector represent a row in the table
    * and must be a String[] of length equals to the number of columns.
    * @columnWidth Each item of this array represent the relavite width of the corresponding column
    * @rightAlligned Each item of this array indicates if the cell of the corresponding
    * column must be right alligned
    */
    public void appendTable(String[] header, Vector data, int[] columnWidth, boolean[] rightAlligned) {
    itemsToPrint.add(new TableToPrint(font, header, data, columnWidth, rightAlligned));
    }

    /**
    * Return the book to be printed
    */
    private Book getPages() {
    Book book = new Book();
    Vector currentPage = new Vector();

    // Get the bounds of a page
    int yLimit = (int)pf.getImageableHeight();
    int xLimit = (int)pf.getImageableWidth();

    // This point holds the bottom left corner angle position of the row of text to print
    int currX = 0;
    int currY = 0;

    // Put all the the items to print in the page
    for (int i = 0; i < itemsToPrint.size(); i++) {
    if (itemsToPrint.elementAt(i) instanceof TableToPrint) {
    TableToPrint table = (TableToPrint)itemsToPrint.elementAt(i);
    currY = printTable(table, book, currentPage, currY, xLimit, yLimit);
    } else {
    TextToPrint text = (TextToPrint)itemsToPrint.elementAt(i);
    currY = printText(text, book, currentPage, currY, xLimit, yLimit);
    }
    }

    if (currentPage.size() > 0)
    book.append(new Page(currentPage), pf);
    return book;
    }

    /**
    * Put a block of text in the book to be printed
    *
    * @return the y position where the next block of text or table should begin
    */
    private int printText(TextToPrint text, Book book, Vector currentPage, int currY, int xLimit, int yLimit) {
    // set the font used to print this text
    graphics.setFont(text.font);

    // Get the FontMetrics for the current Font
    fm = graphics.getFontMetrics();
    int rowHeight = fm.getHeight();

    // If a new page is beginning shift the cursor in the bottom position of the first row
    if (currY == 0) currY = rowHeight;

    // Tokenize the test to be printed in row
    StringTokenizer st = new StringTokenizer(text.text, "\n", true);

    // Remember is the last row printed is empty or not
    boolean emptyRow = true;

    while (st.hasMoreTokens()) {
    String currentRow = st.nextToken();

    // If the row is empty move down the point of a distance equal to the row height
    if (currentRow.equals("\n")) {
    if (emptyRow) {
    currY += rowHeight;

    // If the page is finished save it in the book and begin a new one
    if (currY >= yLimit) {
    book.append(new Page((Vector)currentPage.clone()), pf);
    currentPage.removeAllElements();
    currY = rowHeight;
    }
    } else emptyRow = true;
    } else {
    // the row isn't empty
    emptyRow = false;

    // split the current block of text in more row to fit it in the page
    Enumeration rows = splitString(currentRow, xLimit).elements();

    // Print the text a row at time
    while (rows.hasMoreElements()) {
    currentPage.add(new StringToPrint((String)rows.nextElement(), text.font, (int)pf.getImageableX(), (int)pf.getImageableY() + currY));
    currY += rowHeight;

    // If the page is finished save it in the book and begin a new one
    if (currY >= yLimit) {
    book.append(new Page((Vector)currentPage.clone()), pf);
    currentPage.removeAllElements();
    currY = rowHeight;
    }
    }
    }
    }
    return currY;
    }

    /**
    * Put a block of text in the book to be printed
    */
    private int printTable(TableToPrint table, Book book, Vector currentPage, int currY, int xLimit, int yLimit) {

    // print the header of the table in bold
    Font boldFont = new Font(table.font.getName(), Font.BOLD, table.font.getSize());
    graphics.setFont(boldFont);

    // Get the FontMetrics for the current Font
    fm = graphics.getFontMetrics();
    int rowHeight = fm.getHeight();

    // If a new page is beginning shift the cursor in the bottom position of the first row
    if (currY == 0) currY = rowHeight;

    // Get the width of each column
    int[] columnWidth = table.columnWidth;
    for (int i = 0; i < table.columnNr; i++)
    columnWidth[i] = columnWidth[i] * xLimit / 1000;

    // Get the beginning position of each column
    int[] columnPos = new int[table.columnNr];
    for (int i = 0; i < table.columnNr; i++) {
    columnPos[i] = 1;
    for (int j = 0; j < i; j++)
    columnPos[i] += columnWidth[j];
    }

    // take out two pixel from each column to get the space for the lines
    for (int i = 0; i < table.columnNr; i++)
    columnWidth[i] -= 2;

    // Each Vector in this array holds the sequence of row to be printed in a cell
    Vector[] cell = new Vector[table.columnNr];

    // hold the max cell height of the cells in this row
    int maxCellHeight = 0;

    for (int i = 0; i < table.columnNr; i++) {
    // split the text to fit it in the cell
    cell[i] = splitString(table.header[i], columnWidth[i]);
    maxCellHeight = maxCellHeight > cell[i].size() ? maxCellHeight : cell[i].size();
    }

    // add the first horizontal line of the table
    currentPage.add(new LineToPrint((int)pf.getImageableX(), (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY - rowHeight + 3));

    // Write the header
    for (int i = 0; i < maxCellHeight; i++) {
    for (int j = 0; j < table.columnNr; j++) {
    if (cell[j].size() > i)
    currentPage.add(new StringToPrint((String)cell[j].elementAt(i), boldFont, (int)pf.getImageableX() + columnPos[j], (int)pf.getImageableY() + currY));
    // add the left margin of the cell
    currentPage.add(new LineToPrint((int)pf.getImageableX() + columnPos[j] - 1, (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + columnPos[j] - 1, (int)pf.getImageableY() + currY + 3));
    }

  2. #2
    // add the right margin of the last cell
    currentPage.add(new LineToPrint((int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY + 3));

    currY += rowHeight;

    // If the page is finished save it in the book and begin a new one
    if (currY >= yLimit) {
    book.append(new Page((Vector)currentPage.clone()), pf);
    currentPage.removeAllElements();
    currY = rowHeight;
    }
    }

    graphics.setFont(table.font);

    // Get the FontMetrics for the current Font
    fm = graphics.getFontMetrics();
    rowHeight = fm.getHeight();

    // Write the data in the table
    for (int i = 0; i < table.data.size(); i++) {

    // add the upper bound line of this row
    currentPage.add(new LineToPrint((int)pf.getImageableX(), (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY - rowHeight + 3));

    cell = new Vector[table.columnNr];

    // hold the max cell height of the cells in this row
    maxCellHeight = 0;
    String[] row = (String[])table.data.elementAt(i);
    for (int j = 0; j < table.columnNr; j++) {
    cell[j] = splitString(row[j], columnWidth[j]);
    maxCellHeight = maxCellHeight > cell[j].size() ? maxCellHeight : cell[j].size();
    }

    for (int j = 0; j < maxCellHeight; j++) {
    for (int k = 0; k < table.columnNr; k++) {
    if (cell[k].size() > j)
    if (table.rightAlligned[k])
    currentPage.add(new StringToPrint((String)cell[k].elementAt(j), table.font, (int)pf.getImageableX() + columnPos[k] + columnWidth[k] - fm.stringWidth((String)cell[k].elementAt(j)), (int)pf.getImageableY() + currY));
    else
    currentPage.add(new StringToPrint((String)cell[k].elementAt(j), table.font, (int)pf.getImageableX() + columnPos[k], (int)pf.getImageableY() + currY));
    currentPage.add(new LineToPrint((int)pf.getImageableX() + columnPos[k] - 1, (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + columnPos[k] - 1, (int)pf.getImageableY() + currY + 3));
    }
    currentPage.add(new LineToPrint((int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY + 3));
    currY += rowHeight;

    // If the page is finished save it in the book and begin a new one
    if (currY >= yLimit) {
    book.append(new Page((Vector)currentPage.clone()), pf);
    currentPage.removeAllElements();
    currY = rowHeight;
    }
    }
    }
    // add the lower bound of the table
    currentPage.add(new LineToPrint((int)pf.getImageableX(), (int)pf.getImageableY() + currY - rowHeight + 3, (int)pf.getImageableX() + xLimit, (int)pf.getImageableY() + currY - rowHeight + 3));

    return currY;
    }

    /**
    * Split a String in rows no longer than a given width
    *
    * @param stringToSplit The text to split
    * @param maxWidth The max width of a row
    *
    * @retrun a Vector of Strings, one for each row in which the original text is splitted
    */
    private Vector splitString(String stringToSplit, int maxWidth) {
    Vector result = new Vector();

    // if the width of the whole String is shorter that maxWidth return a Vector with only one item
    if (maxWidth >= fm.stringWidth(stringToSplit)) {
    result.add(stringToSplit);
    return result;
    }

    // divide the String in words
    StringTokenizer st = new StringTokenizer(stringToSplit, " ");
    String row = "";
    while (st.hasMoreTokens()) {
    String word = st.nextToken();
    // try to add the current word to the row
    if (maxWidth > fm.stringWidth(row + word)) {
    // the row is shorter that mawWidth
    row += word + " ";
    } else if (!row.equals("")) {
    // else add the row to Vector
    result.add(row);
    // and begin a new row
    row = word;
    }

    // if single word is larger than maxWidth
    if (maxWidth < fm.stringWidth(word)) {
    // split the word in its single charachters
    char[] chars = word.toCharArray();
    row = "";
    for (int i = 0; i < chars.length; i++) {
    if (maxWidth > fm.stringWidth(row + chars[i])) {
    row += chars[i];
    } else {
    result.add(row);
    row = "" + chars[i];
    }
    }
    }
    }

    // if the last row isn't empty add it to the Vector
    if (!row.equals(""))
    result.add(row);

    return result;
    }

    /**
    * This class implements a single page to print
    */
    class Page implements Printable {

    // A Vector of the items that will be printed in this page
    private Vector text;

    /**
    * The contructor
    *
    * @param text The Vector of the items that will be printed in this page
    */
    public Page(Vector text) {
    this.text = text;
    }

    /**
    * Implements the Printable interface
    */
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    g.setColor(Color.black);

    for (int i = 0; i < text.size(); i++) {
    if (text.elementAt(i) instanceof StringToPrint) {
    // draw a string in the page
    StringToPrint currentString = (StringToPrint)text.elementAt(i);
    g.setFont(currentString.font);
    g.drawString(currentString.theString, currentString.xPos, currentString.yPos);
    } else {
    // draw a line in the page
    LineToPrint currentLine = (LineToPrint)text.elementAt(i);
    g.drawLine(currentLine.x1, currentLine.y1, currentLine.x2, currentLine.y2);
    }
    }
    return Printable.PAGE_EXISTS; // print the page
    }
    }

    /**
    * An empty book used to communicate with the printer and obtain the Graphics object
    */
    class DummyPage implements Printable {

    public DummyPage() { }

    /**
    * Implements the Printable interface
    */
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    graphics = g;
    return Printable.NO_SUCH_PAGE;
    }
    }


    /**
    * This class holds all the information about a table to print
    */
    class TableToPrint {

    public int[] columnWidth;
    public boolean[] rightAlligned;
    public String[] header;
    public Vector data;
    public int columnNr;
    public Font font;

    /**
    * Construct a new table to print
    *
    * @param font The font in which the table will be printed
    * @param header The header for the column in the table
    * @param data The data of the table: each item of the Vector represent a row in the table
    * and must be a String[] of length equals to the number of columns.
    * @columnWidth Each item of this array represent the relavite width of the corresponding column
    * @rightAlligned Each item of this array indicates if the cell of the corresponding
    * column must be right alligned
    */
    public TableToPrint(Font font, String[] header, Vector data, int[] columnWidth, boolean[] rightAlligned) {
    this.header = header;
    this.data = data;
    this.columnWidth = columnWidth;
    this.columnNr = header.length;
    this.font = font;

    // set the column width
    if (columnWidth == null)
    setDefaultColumnWidth();
    else {
    int totalWidth = 0;
    for (int i = 0; i < columnNr; i++)
    totalWidth += this.columnWidth[i];
    for (int i = 0; i < columnNr; i++)
    this.columnWidth[i] = this.columnWidth[i] * 1000 / totalWidth;
    }

    // set the right alligned
    if (rightAlligned == null)
    setDefaultRightAlligned();
    else
    this.rightAlligned = rightAlligned;
    }


    /**
    * Set the column width equal for each column if isn't it defined by the user
    */
    private void setDefaultColumnWidth() {
    int cw = 1000 / columnNr;
    this.columnWidth = new int[columnNr];
    for (int i = 0; i < columnNr; i++)
    this.columnWidth[i] = cw;
    }

    /**
    * Set the right alligned false if isn't it defined by the user
    */
    private void setDefaultRightAlligned() {
    this.rightAlligned = new boolean[columnNr];
    for (int i = 0; i < columnNr; i++)
    this.rightAlligned[i] = false;
    }
    }


    class TextToPrint {
    public String text;
    public Font font;

    public TextToPrint(String text, Font font) {
    this.text = text;
    this.font = font;
    }
    }

    class StringToPrint {
    public String theString;
    public Font font;
    public int xPos;
    public int yPos;

    public StringToPrint(String s, Font f, int x, int y) {
    this.theString = s;
    this.font = f;
    this.xPos = x;
    this.yPos = y;
    }
    }

    class LineToPrint {
    int x1;
    int y1;
    int x2;
    int y2;

    public LineToPrint(int x1, int y1, int x2, int y2) {
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    }
    }
    }
    [/CODE]

    Se la compilo con JBuilder 5 con la jvm versione 1.3.0_02 non mi dà nessun errore, mentre se la compilo con la jvm versione 1.5.0_06-b05
    mi dà il seguente errore:

    "Printer.java": Error #:750: initialization error: com.borland.compiler.symtab.LoadError: class file has wrong version 49.0


    Perchè?

    Grazie

  3. #3
    Utente di HTML.it L'avatar di netarrow
    Registrato dal
    Apr 2004
    Messaggi
    1,425
    Se la compilo con JBuilder 5 con la jvm versione 1.3.0_02 non mi dà nessun errore, mentre se la compilo con la jvm versione 1.5.0_06-b05
    mi dà il seguente errore:

    "Printer.java": Error #:750: initialization error: com.borland.compiler.symtab.LoadError: class file has wrong version 49.0


    Perchè?
    ogni versione del compilatore quando compila genera classi che hanno una determinata versione, ad esempio 49.0(la 5 per dire), se dopo classi di questa versione vengono usate da interpreti/compilatori che usano 48.0(1.4 x esempio) da quel'errore.

    Può esserti utile:
    http://forum.java.sun.com/thread.jsp...17933&tstart=0

    Imparare è un'esperienza, tutto il resto è solo informazione. (Albert Einstein)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.