2009년 7월 2일 목요일

print

---FilePageRenderer.java-----------------------------------------------------------

import java.awt.*;
import java.awt.print.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
public class  FilePageRenderer extends JComponent implements Printable{
private int mCurrentPage;
private Vector mLines;
private Vector mpages;
private Font mFont;
private int mFontSize;
private Dimension mPreferredSize;
public FilePageRenderer(File file,PageFormat pageFormat) throws IOException{
mFontSize=12;
mFont=new Font("serif",Font.PLAIN,mFontSize);
BufferedReader in =new BufferedReader(new FileReader(file));
String line;
mLines=new Vector();
while((line=in.readLine()) != null)
mLines.addElement(line);
in.close();
paginate(pageFormat);
}
public void paginate(PageFormat pageFormat){
mCurrentPage=0;
mpages=new Vector();
float y=mFontSize;
Vector page=new Vector();
for(int i=0; i<mLines.size(); i++){
String line=(String)mLines.elementAt(i);
page.addElement(line);
y+=mFontSize;
if(y+mFontSize*2> pageFormat.getImageableHeight()){
y=0;
mpages.addElement(page);
page=new Vector();
}
}
if(page.size()>0) mpages.addElement(page);
mPreferredSize=new Dimension((int)pageFormat.getImageableWidth(),(int)pageFormat.getImageableHeight());
repaint();
}
public void paintComponent(Graphics g){
Graphics2D g2=(Graphics2D)g;
java.awt.geom.Rectangle2D r=new java.awt.geom.Rectangle2D.Float (0,0,mPreferredSize.width,mPreferredSize.height);
g2.setPaint(Color.white);
g2.fill(r);
Vector page=(Vector ) mpages.elementAt(mCurrentPage);
g2.setFont(mFont);
g2.setPaint(Color.black);
float x=0;
float y = mFontSize;
for(int i=0; i<page.size(); i++){
String line=(String)page.elementAt(i);
if(line.length() > 0) g2.drawString (line,(int)x,(int)y);
y+=mFontSize;
}
}
public int print(Graphics g,PageFormat pageFormat, int pageIndex){
if(pageIndex>=mpages.size()) return NO_SUCH_PAGE;
int savedPage= mCurrentPage;
mCurrentPage=pageIndex;
Graphics2D g2=(Graphics2D)g;
g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
paint(g2);
mCurrentPage=savedPage;
return PAGE_EXISTS;
}
public Dimension getPreferredSize(){
return mPreferredSize;
}
public int getCurrentPage(){
return mCurrentPage;
}
public int getNumPages(){
return mpages.size();
}
public void nextPage(){
if(mCurrentPage<mpages.size()-1)
mCurrentPage++;
repaint();
}
public void previousPage(){
if(mCurrentPage>0)
mCurrentPage--;
repaint();
}
}


---FilePrinter.java------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.event.*;
public class FilePrinter extends JFrame{
public static void main(String [] args){
new FilePrinter();
}
private PageFormat mPageFormat;
private FilePageRenderer mPageRenderer;
private String mTitle;
public FilePrinter(){
super("FilePrint");
createUI();
PrinterJob pj=PrinterJob.getPrinterJob();
mPageFormat=pj.defaultPage();
setVisible(true);
}
protected void createUI(){
setSize(350,300);
center();
Container content=getContentPane();
content.setLayout(new BorderLayout());
JMenuBar mb=new JMenuBar();
JMenu file=new JMenu("File",true);
file.add(new FileOpenAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));
file.add(new FilePrintAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK));
file.add(new FilePageSetupAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,Event.CTRL_MASK|Event.SHIFT_MASK));
file.addSeparator();
file.add(new FileQuitAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,Event.CTRL_MASK));
mb.add(file);
JMenu page=new JMenu("page",true);
page.add(new PageNextPageAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN,0));
page.add(new PagePreviousPageAction()).setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0));
mb.add(page);
setJMenuBar(mb);
getContentPane().setLayout(new BorderLayout());
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
}
protected void center(){
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize=getSize();
int x=(screenSize.width-frameSize.width)/2;
int y=(screenSize.height-frameSize.height)/2;
setLocation(x,y);
}
public void showTitle(){
int currentPage=mPageRenderer.getCurrentPage()+1;
int numPages=mPageRenderer.getNumPages();
setTitle(mTitle+"-page"+currentPage+"of"+numPages);
}
public class FileOpenAction extends AbstractAction{
public FileOpenAction(){super("open...");}
public void actionPerformed(ActionEvent ae){
JFileChooser fc=new JFileChooser(".");
int result=fc.showOpenDialog(FilePrinter.this);
if(result !=0){return;}
java.io.File f= fc.getSelectedFile();
if(f==null){return;}
try{
mPageRenderer=new FilePageRenderer (f,mPageFormat);
mTitle="["+f.getName()+"]";
showTitle();
JScrollPane jsp=new JScrollPane(mPageRenderer);
getContentPane().removeAll();
getContentPane().add(jsp,BorderLayout.CENTER);
validate();
}catch(java.io.IOException ioe){
System.out.println(ioe);
}
}
}
public class FilePrintAction extends AbstractAction{
public FilePrintAction(){super("print");}
public void actionPerformed(ActionEvent ae){
PrinterJob pj= PrinterJob.getPrinterJob();
pj.setPrintable(mPageRenderer,mPageFormat);
if(pj.printDialog()){
try{pj.print();}
catch(PrinterException e) {
System.out.println(e);
}
}
}
}

public class FilePageSetupAction extends AbstractAction{
public FilePageSetupAction() {super("page setup...");}
public void actionPerformed(ActionEvent ae){
PrinterJob pj=PrinterJob.getPrinterJob();
mPageFormat=pj.pageDialog(mPageFormat);
if(mPageRenderer !=null){
mPageRenderer.paginate(mPageFormat);
showTitle();
}
}
}
public class FileQuitAction extends AbstractAction{
public FileQuitAction() {super("quit");}
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}
public class PageNextPageAction extends AbstractAction{
public PageNextPageAction(){super("Nextpage");}
public void actionPerformed(ActionEvent ae){
if(mPageRenderer!=null) mPageRenderer.nextPage();
showTitle();
}
}
public class PagePreviousPageAction extends AbstractAction{
public PagePreviousPageAction() {super("previoue page");}
public void actionPerformed(ActionEvent ae){
if(mPageRenderer !=null) mPageRenderer.previousPage();
showTitle();
}
}
}

댓글 없음:

댓글 쓰기