//************************************************************* //Java application //「PrintTest2」 // //          作 成 者:ルート高菜    //          作成開始月:2007/2 //          最終更新月:2007/2 [TN111/J101] //************************************************************* import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.io.*; import java.util.ArrayList; import javax.print.*; import javax.print.attribute.*; import javax.print.event.*; import javax.swing.*; public class PrintTest2 extends JFrame implements ActionListener,PrintJobListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // public static void main(String[] args) { PrintTest2 frame=new PrintTest2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } //中段(テキスト表示パネル) PrintTest2Panel panel; //上段(テキストファイル指定) String fileName="text file not selected"; JButton selectButton=new JButton("テキストファイル選択"); JLabel fileLabel=new JLabel(fileName); //下段(印刷指定) JButton printNoDialogButton=new JButton("標準のプリンター"); JButton printDialogButton=new JButton("ダイアログ表示"); JButton printNoDialogButtonSimple=new JButton("標準のプリンター"); JButton printDialogButtonSimple=new JButton("ダイアログ表示"); // public PrintTest2(){ // setTitle("PrintTest2"); setSize(500,600); // JPanel panel2=new JPanel(); panel2.setLayout(new GridLayout(2,1)); panel2.add(fileLabel); panel2.add(selectButton); // JPanel panel3=new JPanel(); panel3.setLayout(new GridLayout(2,3)); panel3.add(new JLabel(" javax.print で印刷")); panel3.add(printNoDialogButton); panel3.add(printDialogButton); panel3.add(new JLabel(" java.awt.print で印刷")); panel3.add(printNoDialogButtonSimple); panel3.add(printDialogButtonSimple); //コンテナ枠 Container cp=getContentPane(); cp.add(panel=new PrintTest2Panel(),BorderLayout.CENTER); cp.add(panel2,BorderLayout.NORTH); cp.add(panel3,BorderLayout.SOUTH); // selectButton.addActionListener(this); printNoDialogButton.addActionListener(this); printDialogButton.addActionListener(this); printNoDialogButtonSimple.addActionListener(this); printDialogButtonSimple.addActionListener(this); //テキストファイルを開くまでは無効 printNoDialogButton.setEnabled(false); printDialogButton.setEnabled(false); printNoDialogButtonSimple.setEnabled(false); printDialogButtonSimple.setEnabled(false); } //ボタンイベント public void actionPerformed(ActionEvent e) { if(e.getSource()==selectButton){ String file=getFileName(); if(file!=null){ fileName=file; fileLabel.setText(fileName); // String[] lines=loadTextFile(fileName); if(lines!=null){ panel.setString(lines); } // printNoDialogButton.setEnabled(true); printDialogButton.setEnabled(true); printNoDialogButtonSimple.setEnabled(true); printDialogButtonSimple.setEnabled(true); } // repaint(); }else if(e.getSource()==printNoDialogButton){ printNoDialog(); }else if(e.getSource()==printDialogButton){ printDialog(); }else if(e.getSource()==printNoDialogButtonSimple){ printNoDialogSimple(); }else if(e.getSource()==printDialogButtonSimple){ printDialogSimple(); } } //javax.print を使用 private void printNoDialog(){ //印刷するオブジェクトのMIME (Multipurpose Internet Mail Extensions) タイプの特定 DocFlavor flavor=DocFlavor.SERVICE_FORMATTED.PRINTABLE; //データを印刷ジョブに与えるためのインターフェース DocAttributeSet das=new HashDocAttributeSet(); Doc doc=new SimpleDoc(panel,flavor,das); //どのように印刷するかを示す属性 PrintRequestAttributeSet pras=new HashPrintRequestAttributeSet(); //デフォルトの印刷サービス(プリンター・オブジェクト)を取得 PrintService defaultService=PrintServiceLookup.lookupDefaultPrintService(); //***************************ダイアログ非表示****************************************** //印刷ジョブを作成 DocPrintJob job=defaultService.createPrintJob(); //************************************************************************************* //印刷関連のイベントを受け取るようにする job.addPrintJobListener(this); // try{ job.print(doc,pras); }catch(PrintException e){ JOptionPane.showMessageDialog(this,"印刷に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } } private void printDialog(){ //印刷するオブジェクトのMIME (Multipurpose Internet Mail Extensions) タイプの特定 DocFlavor flavor=DocFlavor.SERVICE_FORMATTED.PRINTABLE; //データを印刷ジョブに与えるためのインターフェース DocAttributeSet das=new HashDocAttributeSet(); Doc doc=new SimpleDoc(panel,flavor,das); //どのように印刷するかを示す属性 PrintRequestAttributeSet pras=new HashPrintRequestAttributeSet(); //デフォルトの印刷サービス(プリンター・オブジェクト)を取得 PrintService defaultService=PrintServiceLookup.lookupDefaultPrintService(); //****************************ダイアログ表示******************************************** //特定の文書タイプ (GIFなど) を特定の属性セット (両面など) での印刷をサポートするプリンターのセット PrintService[] printServices=PrintServiceLookup.lookupPrintServices(flavor,pras); //プリンター選択ダイアログを表示 PrintService service=ServiceUI.printDialog(null,200,200,printServices,defaultService,flavor,pras); //印刷ジョブを作成 DocPrintJob job; if(service!=null){ job=service.createPrintJob(); }else{ return; } //************************************************************************************* //印刷関連のイベントを受け取るようにする job.addPrintJobListener(this); // try{ job.print(doc,pras); }catch(PrintException e){ JOptionPane.showMessageDialog(this,"印刷に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } } //java.awt.print を使用 private void printNoDialogSimple(){ // PrinterJob job=PrinterJob.getPrinterJob(); /*複数のドキュメント印刷の場合はこうする(ページ数指定もある) Book bk=new Book(); bk.append(panel,job.defaultPage(),3); job.setPageable(bk); */ //****************************ダイアログ非表示****************************************** // job.setPrintable(panel); // try{ job.print(); }catch(PrinterException e){ JOptionPane.showMessageDialog(this,"印刷に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } } private void printDialogSimple(){ // PrinterJob job=PrinterJob.getPrinterJob(); /*複数のドキュメント印刷の場合はこうする(ページ数指定もある) Book bk=new Book(); bk.append(panel,job.defaultPage(),3); job.setPageable(bk); */ //****************************ダイアログ表示******************************************** //ページ設定ダイアログ PageFormat fmt=job.pageDialog(job.defaultPage()); // job.setPrintable(panel,fmt); //印刷ジョブのプロパティ変更ダイアログ if(job.printDialog()){ try{ job.print(); }catch(PrinterException e){ JOptionPane.showMessageDialog(this,"印刷に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } } } //ファイルダイアログを表示して目的ファイルのパスを取得(キャンセルしたらnullを返す) private String getFileName(){ // FileDialog fileDialog=new FileDialog(this); fileDialog.setMode(FileDialog.LOAD); fileDialog.setTitle("画像ファイル選択"); fileDialog.setVisible(true); // String dir=fileDialog.getDirectory(); String file=fileDialog.getFile(); // if(file!=null){ return dir+file; }else{ return null; } } //指定されたテキストファイルを開いて、中身を返す private String[] loadTextFile(String file){ // BufferedReader br; try{ br=new BufferedReader(new FileReader(file)); }catch(FileNotFoundException e){ JOptionPane.showMessageDialog(this,"ファイルが見つかりません。","エラー",JOptionPane.ERROR_MESSAGE); return null; } // ArrayList lines=new ArrayList(); try{ String line; while((line=br.readLine())!=null){ lines.add(line); } br.close(); }catch(IOException e){ JOptionPane.showMessageDialog(this,"ファイル読み込みに失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return null; } // String[] ret=new String[lines.size()]; for(int l=0;l