ÇÁ¸°Æ® °ü·Ã À̺¥Æ®¸¦ µè°í ±×·¡ÇÈÀ» Á÷Á¢ ÇÁ¸°Æ®ÇÏ´Â ¹æ¹ý
John
Zukowski ȸÀå (JZ Ventures, Inc.) 2002³â 4¿ù
»õ·Î¿î Java Print Service API ¼Ò°³, µÎ ¹ø° ½Ã°£ÀÌ´Ù.
John Zukowski´Â ÇÁ¸°Æ® ÀÛµ¿À» À§ÇØ À̺¥Æ® ¸®½º³Ê(event listener)¸¦ ¼³Á¤ÇÏ´Â ¹æ¹ý, ½ºÅ©¸°À̳ª ÄÄÆ÷³ÍÆ®ÀÇ
³»¿ëÀ» ÇÁ¸°Æ®ÇÏ´Â ¹æ¹ý, ÇÁ¸°Æà ÀÛµ¿¿¡¼ sleep() È£ÃâÀ» Á¦°ÅÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇÑ´Ù.
Part
1 ¿¡¼´Â ÆÄÀÏ ½Ã½ºÅÛ¿¡¼ Á÷Á¢ ÇÁ¸°ÆÃÇÏ´Â ¹æ¹ý°ú ÇÁ¸°ÅÍ ´ÙÀ̾ó·Î±×¸¦ ÀÌ¿ëÇÏ¿© ÇÁ¸°ÅÍ ¼±ÅÃÀ» ÇÁ·ÒÇÁÆ®ÇÏ´Â ¹æ¹ýÀ» ¹è¿ü´Ù. À̹ø
ÁÖ¿¡´Â, À̺¥Æ® ¸®½º³Ê(event listener)¸¦ ÇÁ¸°Æ® ÀÛµ¿¿¡ Àû¿ëÇÏ°í ½ºÅ©¸°À̳ª ÄÄÆ÷³ÍÆ®¿¡¼ Á÷Á¢ ÇÁ¸°Æ®ÇÏ´Â ¹æ¹ýÀ»
¼³¸íÇÏ°Ú´Ù. ÀÌÀü ±Û°ú ¸¶Âù°¡Áö·Î »õ·Î¿î ÇÁ¸°Æ® API¸¦ °æÇèÇÒ ¼ö ÀÖµµ·Ï ½ÇÇà ¿¹Á¦·Î ¸¶¹«¸®ÇÒ °ÍÀÌ´Ù. Print Service
API¿¡ Àͼ÷ÇÏÁö ¾Ê´Ù¸é ÀÌÀü ±ÛÀ» Àб⠹ٶõ´Ù.
ÇÁ¸°Æà À̺¥Æ® µè±â(listen) Part 1ÀÇ
¿¹Á¦ ÇÁ·Î±×·¥ÀÇ ¸¶Áö¸· ¶óÀÎÀÌ´Ù:
ÀÌ·¯ÇÑ È£Ãâ·Î ´Ù¸¥ ¾²·¹µå°¡ ÇÁ·Î±×·¥¿¡¼ ÇÁ¸°ÅÍ·Î µ¥ÀÌÅ͸¦ Àü¼ÛÇÏ´Â µ¿¾È ÁÖ ÇÁ·Î±×·¥Àº 10ÃÊ µ¿¾È ÀÛµ¿À» ¸ØÃá´Ù(sleep).
³í¸®ÀûÀ¸·Î Àü¼Û·üÀ» °í·ÁÇغ¼ ¶§, ´ëºÎºÐÀÇ À̹ÌÁö ÆÄÀÏÀº °èȹµÈ ½Ã°£¿¡ Àü¼ÛÀ» ³¡¸¶ÃÄ¾ß ÇÑ´Ù. Àü¼ÛÀº 10ÃÊ À̳»¿¡ ¿Ï·áµÇ¾î¾ß ÇÒ
°ÍÀÌ´Ù. ÀϹÝÀûÀ¸·Î, Àü¼ÛÀº ºü¸£±â ¶§¹®¿¡ ÃÖÀûÀÇ ÆÛÆ÷¸Õ½º¸¦ À§ÇØ Á¤Áö ½Ã°£(sleep time)À» Á¶ÀÛÇÒ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ÃÖ»óÀÇ
½Ã°£À» °£´ÜÈ÷ °è»êÇÒ ¼ö´Â ¾ø´Ù.
Thread.sleep()
¿¡ ÀÇÁ¸À» ¿øÇÏÁö ¾Ê°Å³ª Àü¼ÛÀÌ ¿Ï·áµÉ ¶§ °è»êÇϱ⸦ ¿øÇÏÁö ¾Ê´Â »ç¶÷µé¿¡°Ô
Java Print Service API´Â PrintJobListener
Çü½ÄÀÇ ´ë¾ÈÀ» Á¦°øÇÑ´Ù.
Listing 1Àº ÀÎÅÍÆäÀ̽ºÀÌ´Ù:
Listing 1. PrintJobListener ÀÎÅÍÆäÀ̽º
public interface PrintJobListener {
public void printDataTransferCompleted(PrintJobEvent e);
public void printJobCompleted(PrintJobEvent e);
public void printJobFailed(PrintJobEvent e);
public void printJobCanceled(PrintJobEvent e);
public void printJobNoMoreEvents(PrintJobEvent e);
public void printJobRequiresAttention(PrintJobEvent e);
}
PrintJobListener
ÀÎÅÍÆäÀ̽º¿¡´Â 6°³ÀÇ ¸Þ¼Òµå°¡ ÀÖ´Ù.
PrintJobListener
´Â ´Ù¸¥ "delegation-based" À̺¥Æ® ¸®½º³Ê ó·³ ÀÛµ¿ÇÑ´Ù: ÀÏ´Ü,
¸®½º³Ê¸¦ ¸¸µé¾î¼ ƯÁ¤ À̺¥Æ®¿¡ À̸¦ µî·ÏÇϸé ÇØ´ç À̺¥Æ®°¡ ¹ß»ýÇÒ ¶§ ¾Ë·ÁÁú °ÍÀÌ´Ù.
ÀڽŸ¸ÀÇ ÀÎÅÍÆäÀ̽ºÀÇ ¸Þ¼Òµå¸¦ ±¸ÇöÇÒ ¿É¼ÇÀ» °®°Ô µÇ°í Print Service API ¿¡´Â ¿©·¯ºÐÀ» À§Çؼ ¹«¾ùÀ̵ç ÇÏ´Â
¾î´ðÅÍ Å¬·¡½º°¡ ÀÖ´Ù. ¿©·¯ºÐÀÌ ÇØ¾ß ÇÒ ÀÏÀº PrintJobAdapter
¸¦ ºÐ·ùÇÏ°í °ü½É ÀÖ´Â
Notification¿ë ¸Þ¼Òµå¸¦ ±¸ÇöÇÏ´Â °ÍÀÌ´Ù.
¿¹¸¦ µé¾î, Àü¼ÛÀÌ ¿Ï·áµÇ´Â ´ë·Î ÇÁ·Î±×·¥À» Á¤Áö½ÃÅ°·Á¸é
printDataTransferCompleted()
ÀÇ System.exit()
¿¡ È£ÃâÀ»
Ãß°¡ÇÏ¸é µÈ´Ù. (Listing 2):
Listing 2. PrintJobListener
µî·ÏÇϱâ
PrintService printService =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printService.createPrintJob();
PrintJobListener listener = new PrintJobAdapter() {
public void printDataTransferCompleted(PrintJobEvent e) {
System.out.println("Good-bye");
System.exit(0);
}
};
job.addPrintJobListener(listener);
PrintJobListener
°¡ ÀÛµ¿À» ÈǸ¢È÷ ¼öÇàÇÏ´Â µ¿¾È ½Ã½ºÅÛÀº Àü¼ÛÀÌ ¹ß»ýÇÒ ¶§±îÁö ±â´Ù·Á¾ß
ÇÑ´Ù. ÇÁ¸°ÅÍ ¼±Åà ´ÙÀ̾ó·Î±×³ª ´Ù¸¥ GUI °ü·Ã ŽºÅ©¸¦ º¸¿©ÁÖÁö ¾ÊÀ¸¸é ÇÁ·Î±×·¥Àº Áï½Ã Á¾·áµÈ´Ù. ÇÁ¸°ÅÍ·ÎÀÇ µ¥ÀÌÅÍ Àü¼ÛÀº µ¥¸ó
¾²·¹µå¿¡¼ ¹ß»ýÇϸç ÀÚ¹Ù ·±Å¸ÀÓÀº Àü¼ÛÀÌ ÁøÇàµÇ´Â µ¿¾È Á¾·áÇÒ ¼ö ÀÖ´Ù. ¾î¶² GUI °ü·Ã ŽºÅ©¸¦ ¸¸µç´Ù´Â °ÍÀº ºñ
µ¥¸ó(non-daemon) AWT ¾²·¹µå¸¦ ¸¸µå´Â °ÍÀÌ´Ù. ÀÌ°ÍÀº µ¥ÀÌÅÍ°¡ Àü¼ÛµÇ´Â µ¿¾È ½Ã½ºÅÛÀ» Áö¼Ó½ÃÄÑÁØ´Ù.
±×·¡ÇÈ ±×¸®±â ÀÌÀü ±Û¿¡¼ µð½ºÅ©¿¡¼ ÆÄÀÏÀ»
ÇÁ¸°Æ®ÇÏ´Â ¹æ¹ýÀ» ¹è¿ü´Ù. ¸Þ¸ð¸® ³»ºÎÀÇ À̹ÌÁö ¶Ç´Â ÄÄÆ÷³ÍÆ®ÀÇ ³»¿ëÀ» ÇÁ¸°ÆÃÇÏ´Â °ÍÀº ¾à°£ ´Ù¸£´Ù. ´ÙÀ½Àº
DocFlavor.SERVICE_FORMATTED
°¡ ÀÛµ¿ÇÏ´Â Àå¼ÒÀÌ´Ù.
DocFlavor.SERVICE_FORMATTED
´Â ¼¼ °³ÀÇ ÀÎÅÍÆäÀ̽º¿Í ÇÔ²² ÀÛµ¿ÇÑ´Ù:
java.awt.print.Pageable
java.awt.print.Printable
java.awt.image.renderable.RenderableImage
Áö³ ±Û¿¡¼ SimpleDoc¸¦ ¸¸µé¾î¼, ÇÁ¸°Æ®¸¦ À§ÇØ DocÀ» Á¤ÀÇÇÒ ¶§, µð½ºÅ© ÆÄÀÏ¿ëÀ¸·Î
InputStream
¿¡¼ Àü´ÞÇß´Ù(Listing 3).
Listing
3. ÄÜÅÙÆ® ¼³Á¤Çϱâ
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
String filename = ...;
FileInputStream fis = new FileInputSteam(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
DocFlavor.SERVICE_FORMATTED
¸¦ ÀÌ¿ëÇÏ¿© ÀÛµ¿ÇÒ ¶§, ¸Þ¸ð¸®¿¡¼ ±×·¡ÇÈÀ» ÇÁ¸°Æ®ÇÏ·Á¸é
À̵é ÀÎÅÍÆäÀ̽º Áß ÇϳªÀÇ ±¸ÇöÀÚ¿¡¼ Àü´ÞÇØ¾ß ÇÑ´Ù.
java.awt.print.Printable
ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏ´Â ÄÄÆ÷³ÍÆ®¸¦ Á¤ÀÇÇغ¸ÀÚ(Listing
4)..
Listing 4. Printable ÀÎÅÍÆäÀ̽º
public interface Printable {
public static final int PAGE_EXISTS;
public static final int NO_SUCH_PAGE;
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException;
}
Printable
ÀÇ print()
¸Þ¼Òµå¿¡¼
paint()
¸¦ È£ÃâÇÏ´Â ÄÄÆ÷³ÍÆ®¸¦ ¸¸µé¾î¼ Printable
ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇß´Ù.
Listing 5´Â ÀϹÝÀûÀÎ ±¸ÇöÀÌ´Ù.
Listing 5. Printable ±¸Çö
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
int x = (int)pageFormat.getImageableX();
int y = (int)pageFormat.getImageableY();
g.translate(x, y);
if (pageIndex == 0) {
paint(g);
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}
ÇÑ ÆäÀÌÁö Á¤µµÀÇ ±æÀ̶ó¸é ¸ðµç ÄÄÆ÷³ÍÆ®¸¦ À§ÇØ ÀÌ·¯ÇÑ ±¸ÇöÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù. ¸Þ¼ÒµåÀÇ Ã¹ ¹ø° ºÎºÐÀº
"µå·ÎÀ×(drawing) ºÎºÐÀ» ÇÁ¸°ÅÍ¿¡¼ »ç¿ëµÇ´Â ±¸¿ªÀ¸·Î ¿Å°Ü¶ó." ¶ó°í ¸í·ÉÇÑ´Ù. µÎ ¹ø° ºÎºÐÀº, "ù ¹ø° ÆäÀÌÁö¿¡¼
paint()
¸¦ È£ÃâÇÏ°í ´Ù¸¥ ÆäÀÌÁöÀÇ °æ¿ì È£ÃâÇÏÁö ¸»¶ó."°í ¸í·ÉÇÏ°í ÀÖ´Ù. ÀÌ´Â ÇÁ¸°Æ®ÇÏ·Á°í ÇÒ ¶§
paint()
·ÎÀÇ ´ÜÀÏ È£ÃâÀ̶ó´Â °á°ú¸¦ ¸¸µç´Ù.
¿¹Á¦ ÀÌÀü ±Û¿¡¼ »õ·Î¿î ÇÁ¸°Æ® ±â´ÉÀ» ½ÃµµÇÒ
¿¹Á¦¸¦ º¸¿©ÁÖ¾ú´Ù. Listing 6Àº Áö±Ý±îÁö ÀÛµ¿ÇÑ ¸ðµç Äڵ带 Á¶ÇÕÇÑ °ÍÀÌ´Ù. ¾Æ·¡ÀÇ ÄÄÆ÷³ÍÆ®¿¡´Â ½ºÅ©¸° Áß¾Ó¿¡ Ä¿½ºÅÒ
"Hello, Printer" ±×¸®°í ÁÖÀ§¿¡ ¹Ú½º¸¦ ±×¸°´Ù. Print ¹öÆ°À» ´©¸£¸é ¾Æ¿ôDzÀÌ µðÆúÆ® ÇÁ¸°ÅÍ·Î Àü¼ÛµÇ°í ÇÁ·Î±×·¥Àº
Á¾·áÇÑ´Ù.
Listing 6. ÇÁ¸°Æà ¿¹Á¦
import javax.print.*;
import javax.print.event.*;
import javax.print.attribute.*;
import java.awt.print.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class PrintPrintable {
static class MyComponent extends JPanel
implements Printable {
Font theFont = new Font("Serif", Font.ITALIC, 48);
public void paint(Graphics g) {
super.paint(g);
String msg = "Hello, Printer";
g.setFont(theFont);
FontMetrics fm = g.getFontMetrics();
// Center line
int width = getWidth();
int stringWidth = fm.stringWidth(msg);
int x = (width - stringWidth)/2;
int height = getHeight();
int stringHeight = fm.getHeight();
int ascent = fm.getAscent();
int y = (height - stringHeight)/2 + ascent;
g.drawString(msg, x, y);
g.drawRect(x, y-ascent, stringWidth, stringHeight);
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
int x = (int)pageFormat.getImageableX();
int y = (int)pageFormat.getImageableY();
g.translate(x, y);
if (pageIndex == 0) {
paint(g);
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}
}
public static void main(String args[]) throws Exception {
final JFrame frame = new JFrame("Printing Graphics");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
final Component printIt = new MyComponent();
contentPane.add(printIt, BorderLayout.CENTER);
JButton button = new JButton("Print");
contentPane.add(button, BorderLayout.SOUTH);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService printService =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = printService.createPrintJob();
PrintJobListener pjlistener = new PrintJobAdapter() {
public void printDataTransferCompleted(PrintJobEvent e) {
System.out.println("Good-bye");
System.exit(0);
}
};
job.addPrintJobListener(pjlistener);
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(printIt, flavor, das);
try {
job.print(doc, pras);
} catch (PrintException pe) {
pe.printStackTrace();
}
}
};
button.addActionListener(listener);
frame.setSize(350, 250);
frame.show();
}
}
´ÙÀ½ Magic with Merlin Ä®·³¿¡¼´Â SwingÀÇ »õ·Î¿î JFormattedTextField ÄÄÆ÷³ÍÆ®¸¦
¿¬±¸ÇÏ°Ú´Ù.
Âü°íÀÚ·á