Ken Liang

Greenhorn
+ Follow
since Jan 26, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ken Liang

Thank you, Jelda! I am trying.........
My print source code is enclosed. It can print multipage but jump page . It only print page 2,4,6,8....., can't print 1,3,5,7..... Who can give me a guidance? Thank in advance!
Ken

====== Source Code: begin ==========
package logger;
import javax.print.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.print.*;

public class Log2Printer implements Printable {
public Log2Printer() {}
public void print() {
try {
this.getPrintContent();
DocFlavor df = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService ds = PrintServiceLookup.lookupDefaultPrintService();
Doc d = new SimpleDoc(this, df, null);
DocPrintJob job = ds.createPrintJob();
job.print(d, null);
}
catch (Exception pe) {pe.printStackTrace();}
}
private Vector m_vecPrintContent = new Vector();
private void getPrintContent() {
try {
FileReader fr = new FileReader("C:/a.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
m_vecPrintContent.add(line);
line = br.readLine();
}
}
catch (Exception e) {e.printStackTrace();}
}
public int print(Graphics g, PageFormat pf, int pageIndex) {
Graphics2D g2D = (Graphics2D) g;
g2D.translate(pf.getImageableX(), pf.getImageableY());
Font font = new Font("Serif", Font.PLAIN, 14);
g2D.setFont(font);
int pageWidth = g2D.getClipBounds().width;
int pageHeight = g2D.getClipBounds().height;
FontMetrics fontMetrics = g2D.getFontMetrics(font);
int position = fontMetrics.getHeight();
int height = fontMetrics.getAscent();
while (!m_vecPrintContent.isEmpty()) {
String line = (String)m_vecPrintContent.firstElement();
int lineWidth = fontMetrics.stringWidth(line);
while (lineWidth > pageWidth) {
String lineCopy = line;
String firstPart = "";
while (lineWidth > pageWidth) {
int index = lineCopy.lastIndexOf(' ');
index = (index != -1)? index : (int)((double)lineCopy.length()*((double)pageWidth/(double)lineWidth));
firstPart = lineCopy.substring(0, index);
lineWidth = fontMetrics.stringWidth(firstPart);
lineCopy = firstPart;
}
g2D.drawString(firstPart, 0, position);
System.out.println(firstPart);
position += height;
line = line.substring(firstPart.length(), line.length()).trim();
lineWidth = fontMetrics.stringWidth(line);
if (position >= pageHeight) {
m_vecPrintContent.set(0,line);
return Printable.PAGE_EXISTS;
}
}
g2D.drawString(line, 0, position);
System.out.println(line);
m_vecPrintContent.remove(0);
position += height;
if (position >= pageHeight) return Printable.PAGE_EXISTS;
}
return Printable.NO_SUCH_PAGE;
}

public static void main(String[] args) {
Log2Printer lp = new Log2Printer();
lp.print();
}
}
====== Source Code: end ==========
20 years ago
Hi,
It is known that we can use PropertyConfigurator.configureAndWatch("propertiesfile") to monitor the change of the property file. I want to catch the file change event then when it changes I can do some renew operation. I don't know if I need add a listener or create my own event? How? May someone kindly give me a smaple?
Appreciate for reply.
Ken
20 years ago
Hi,
It is known that we can use PropertyConfigurator.configureAndWatch("propertiesfile") to monitor the change of the property file. I want to catch the file change event then when it changes I can do some renew operation. I don't know if I need add a listener or create my own event? How? May someone kindly give me a smaple?
Appreciate for reply.
Ken
I'm working with the JDK 1.4.2_01-b02 on XP. I try to print out my log
file.
Here is my printer's return information:
image/gif; image; [B
image/gif; image; java.io.InputStream
image/gif; image; java.net.URL
image/jpeg; image; [B
image/jpeg; image; java.io.InputStream
image/jpeg; image; java.net.URL
image/png; image; [B
image/png; image; java.io.InputStream
image/png; image; java.net.URL
application/x-java-jvm-local-objectref; application;
java.awt.print.Pageable
application/x-java-jvm-local-objectref; application;
java.awt.print.Printable
application/octet-stream; application; [B
application/octet-stream; application; java.net.URL
application/octet-stream; application; java.io.InputStream
Here is my code:
try {
FileInputStream textStream = new FileInputStream("C:/my.log");
DocFlavor textInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc textDoc = new SimpleDoc(textStream, textInFormat, null);
PrintService printer =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printer.createPrintJob();
job.print(textDoc, null);
} catch (Exception e) {e.printStackTrace();}
Here is the content of my.log file:
"Testing javax.print"
Here is the situation:
1. I can print out JPEG file with this javax.print API.
2. I can see my.log appear in the printer's job queue and indicate it
was printed out successfully.
3. But in fact printer didn't act! (If I print JPEG, it works)
4. I even try this way:
"Doc strDoc = new SimpleDoc("OK!", DocFlavor.STRING.TEXT_PLAIN,
null);"
But error message has been throwed out:
"sun.print.PrintJobFlavorException: invalid flavor"

Any help appreciate!!

Ken
20 years ago
I'm working with the JDK 1.4.2_01-b02 on XP. I try to print out my log
file.
Here is my printer's return information:
image/gif; image; [B
image/gif; image; java.io.InputStream
image/gif; image; java.net.URL
image/jpeg; image; [B
image/jpeg; image; java.io.InputStream
image/jpeg; image; java.net.URL
image/png; image; [B
image/png; image; java.io.InputStream
image/png; image; java.net.URL
application/x-java-jvm-local-objectref; application;
java.awt.print.Pageable
application/x-java-jvm-local-objectref; application;
java.awt.print.Printable
application/octet-stream; application; [B
application/octet-stream; application; java.net.URL
application/octet-stream; application; java.io.InputStream
Here is my code:
try {
FileInputStream textStream = new FileInputStream("C:/my.log");
DocFlavor textInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc textDoc = new SimpleDoc(textStream, textInFormat, null);
PrintService printer =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printer.createPrintJob();
job.print(textDoc, null);
} catch (Exception e) {e.printStackTrace();}
Here is the content of my.log file:
"Testing javax.print"
Here is the situation:
1. I can print out JPEG file with this javax.print API.
2. I can see my.log appear in the printer's job queue and indicate it
was printed out successfully.
3. But in fact printer didn't act! (If I print JPEG, it works)
4. I even try this way:
"Doc strDoc = new SimpleDoc("OK!", DocFlavor.STRING.TEXT_PLAIN,
null);"
But error message has been throwed out:
"sun.print.PrintJobFlavorException: invalid flavor"

Any help appreciate!!

Ken
20 years ago
I'm working with the JDK 1.4.2_01-b02 on XP. I try to print out my log
file.
Here is my printer's return information:
image/gif; image; [B
image/gif; image; java.io.InputStream
image/gif; image; java.net.URL
image/jpeg; image; [B
image/jpeg; image; java.io.InputStream
image/jpeg; image; java.net.URL
image/png; image; [B
image/png; image; java.io.InputStream
image/png; image; java.net.URL
application/x-java-jvm-local-objectref; application;
java.awt.print.Pageable
application/x-java-jvm-local-objectref; application;
java.awt.print.Printable
application/octet-stream; application; [B
application/octet-stream; application; java.net.URL
application/octet-stream; application; java.io.InputStream
Here is my code:
try {
FileInputStream textStream = new FileInputStream("C:/my.log");
DocFlavor textInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc textDoc = new SimpleDoc(textStream, textInFormat, null);
PrintService printer =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printer.createPrintJob();
job.print(textDoc, null);
} catch (Exception e) {e.printStackTrace();}
Here is the content of my.log file:
"Testing javax.print"
Here is the situation:
1. I can print out JPEG file with this javax.print API.
2. I can see my.log appear in the printer's job queue and indicate it
was printed out successfully.
3. But in fact printer didn't act! (If I print JPEG, it works)
4. I even try this way:
"Doc strDoc = new SimpleDoc("OK!", DocFlavor.STRING.TEXT_PLAIN,
null);"
But error message has been throwed out:
"sun.print.PrintJobFlavorException: invalid flavor"

Any help appreciate!!

Ken
20 years ago