Converting the File into an Image


The ConvertIntoHTML.java and ConvertIntoTextImage.java files convert the file that is open in the user interface of the Fax application to images of HTML and text pages, respectively. The ConvertIntoHTML.java and ConvertIntoTextImage.java files implement the interface defined by the PreparedFaxDoc.java file.

Listing 6-2 shows the PreparedFaxDoc.java file:

Listing 6-2: The PreparedFaxDoc.java File

start example
 /*Imports required java.awt.Image class.*/ import java.awt.Image; /* interface PreparedFaxDoc - This interface declare an abstract method. Methods:    getFaxPage */ public interface PreparedFaxDoc {     public abstract Image getFaxPage(int i); } 
end example

Download this listing.

In the above listing, the getFaxPage()method returns the converted image. The image returned by the getFaxPage()method shows the contents of the file sent as a fax.

The ConvertIntoTextImage.java file converts the file opened in the edit pane to a text image. This image contains the contents of the file.

Listing 6-3 shows the ConvertIntoTextImage.java file:

Listing 6-3: The ConvertIntoTextImage.java File

start example
 /*Imports required javax.comm package classes.*/ import java.awt.*; /* class ConvertIntoTextImage - This class is used to convert editpane Contents into text page Constructor:    ConvertIntoTextImage - This constructor initializes parameters Methods:    getFaxPage - Returns fax Page.    getTextImage - Returns text Page.  getTextString - This method is called to convert String into String array. prepare - This method is called to invoke getTextString method. */ public class ConvertIntoTextImage implements PreparedFaxDoc { public Font textfont;        public String text;        public int linespage;        public int topmargin;        public int leftmargin;        public Image pageimage;        private String textstring[];        private int linenumber;        public ConvertIntoTextImage()        {            textfont = new Font("Serif", 0, 12);            text = "";            linespage = 66;            topmargin = 4;            leftmargin = 4;            pageimage = null;            textstring = new String[1000];            linenumber = 0;     }    /*    getFaxPage - This method is called to return fax Page.    Parameters: page - Variable of integer type.    Return Value: Image    */     public Image getFaxPage(int page)     {         if(page * linespage >= linenumber)             return null;         else             return getTextImage(page);     }    /*    getTextImage - This method is called to return text Page.    Parameters: page - Variable of integer type.    Return Value: Image    */     private Image getTextImage(int page)     {          Graphics g = pageimage.getGraphics();          g.setColor(Color.white);          g.fillRect(0, 0, pageimage.getWidth(null), pageimage.getHeight(null));          g.setColor(Color.black);          g.setFont(textfont);          int lineH = g.getFontMetrics().getHeight();          int charW = g.getFontMetrics().stringWidth("X");          int linesImage = pageimage.getHeight(null) / lineH - topmargin;          if(linesImage < linespage)          linespage = linesImage;          int currentY = lineH * topmargin;          int currentLine = 0;          for(int currentTotalLine = page * linespage; currentLine < linespage &&          currentTotalLine < linenumber; currentTotalLine++)          {             if(textstring[currentTotalLine] != null)             g.drawString(textstring[currentTotalLine], leftmargin * charW, currentY);             currentY += lineH;             currentLine++;          }          return pageimage;        }       /*       prepare - This method is called to invoke getTextString method.       Parameters:  NA       Return Value: NA       */       public void prepare()        {         getTextString(text);        }       /*       getTextString - This method is called to convert String into String array.       Parameters: text -Object of String object.       Return Value: NA       */        private void getTextString(String text)        {           int p = 0;           linenumber = 0;           for(p = text.indexOf("\n"); p >= 0; p = text.indexOf("\n"))           {             textstring[linenumber++] = text.substring(0, p);             text = text.substring(p + 1, text.length());          }         textstring[linenumber++] = text;     } } 
end example

Download this listing.

In the above listing, the ConvertIntoTextImage class converts the text of the file into a image. The methods defined in the above listing are:

  • getFaxPage():Returns the page as an image to send as a fax. This method is an abstract method that is defined in the PreparedFaxDoc.java interface.

  • getTextImage():Creates an object of the Graphics class. The method converts the file opened in the edit pane to a text image.

  • prepare():Invokes the getTextString() method.

  • getTextString():Retrieves the contents of the text file that is open in the edit pane into a String array.

The ConvertIntoHTML.java file converts the file opened in the edit pane to an image of the HTML page sent as a fax.

Listing 6-4 shows the ConvertIntoHTML.java file:

Listing 6-4: The ConvertIntoHTML.java File

start example
 /*Imports required java.awt package classes*/ import java.awt.*; /*Imports required javax.swing classes*/ import javax.swing.JComponent; import javax.swing.JEditorPane; import javax.swing.text.JTextComponent; /* class ConvertIntoHTML - This class is the use to convert editpane Contents into HTML image. Constructor: ConvertIntoHTML - This constructor initializes parameters Methods:    getFaxPage - Returns fax Page    getHTMLPage - Returns HTML Page  */ public class ConvertIntoHTML implements PreparedFaxDoc {     public String text[];     public Image pageimage;     private JEditorPane htmleditorpanel;     public ConvertIntoHTML()     {         pageimage = null;         htmleditorpanel = new JEditorPane();     }    /*    getFaxPage - This method is called to return fax Page.    Parameters: page - Variable of integer type.    Return Value: Image    */     public Image getFaxPage(int page)     {        if(text == null)             return null;         if(page >= text.length)             return null;         else             return getHTMLPage(page);     }    /*    getHTMLPage - This method is called to return HTML Page.    Parameters: page - Variable of integer type.    Return Value: Image    */     private Image getHTMLPage(int page)     {          Graphics g = pageimage.getGraphics();          htmleditorpanel.setSize(pageimage.getWidth(null), pageimage.getHeight(null));          htmleditorpanel.setBackground(Color.white);          htmleditorpanel.setEditable(false);             htmleditorpanel.setContentType("text/html");          htmleditorpanel.setText(text[page]);          htmleditorpanel.paint(g);          return pageimage;     } } 
end example

Download this listing.

In the above listing, the ConvertIntoHTML.java file implements the PreparedFaxDoc.java interface. The various methods defined in the ConvertIntoHTML.java file are:

  • getFaxPage():Returns the page as an HTML page to send as a fax. This method is an abstract method that is defined in the PreparedFaxDoc.java interface.

  • getHTMLPage():Converts the HTML page of the text file open in the user interface of the Fax application to an image.




Developing Applications Using JCA
Developing Applications Using JCA
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 43

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net