//************************************************************* //Java application //「PrintTest」 // //          作 成 者:ルート高菜    //          作成開始月:2007/2 //          最終更新月:2007/2 [TN110/J100] //************************************************************* import java.awt.*; import java.awt.event.*; //import java.awt.print.*; import java.io.*; import javax.print.*; import javax.print.attribute.*; import javax.swing.*; public class PrintTest extends JFrame implements ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // public static void main(String[] args) { PrintTest frame=new PrintTest(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } // String fileName="image file not selected"; JButton selectButton=new JButton("画像ファイル選択(JPEG、GIF、PNG)"); JButton printNoDialogButton=new JButton("デフォルトのプリンターで印刷"); JButton printDialogButton=new JButton("ダイアログを表示して印刷"); JLabel fileLabel=new JLabel(fileName); // public PrintTest(){ // setTitle("PrintTest"); setSize(700,200); // JPanel panel=new JPanel(); panel.add(selectButton); panel.add(printNoDialogButton); panel.add(printDialogButton); panel.add(fileLabel); //コンテナ枠 Container cp=getContentPane(); cp.add(panel,BorderLayout.CENTER); // selectButton.addActionListener(this); printNoDialogButton.addActionListener(this); printDialogButton.addActionListener(this); // printNoDialogButton.setEnabled(false); printDialogButton.setEnabled(false); } //ボタンイベント public void actionPerformed(ActionEvent e) { if(e.getSource()==selectButton){ String file=getFileName(); if(file!=null){ fileName=file; fileLabel.setText(fileName); printNoDialogButton.setEnabled(true); printDialogButton.setEnabled(true); } }else if(e.getSource()==printNoDialogButton){ printImageNoDialog(); }else if(e.getSource()==printDialogButton){ printImageDialog(); } } // private void printImageNoDialog(){ //印刷するオブジェクトのMIME (Multipurpose Internet Mail Extensions) タイプの特定 DocFlavor flavor; String ext=getExtension(fileName); if(ext.equals("jpg") || ext.equals("jpeg")){ flavor=DocFlavor.INPUT_STREAM.JPEG; }else if(ext.equals("gif")){ flavor=DocFlavor.INPUT_STREAM.GIF; }else if(ext.equals("png")){ flavor=DocFlavor.INPUT_STREAM.PNG; }else{ JOptionPane.showMessageDialog(this,"開ける画像ファイルではありません。","エラー",JOptionPane.ERROR_MESSAGE); return; } //ファイル入力 FileInputStream fis; try{ fis=new FileInputStream(fileName); }catch(FileNotFoundException e){ JOptionPane.showMessageDialog(this,"ファイルが見つかりません。","エラー",JOptionPane.ERROR_MESSAGE); return; } //データを印刷ジョブに与えるためのインターフェース DocAttributeSet das=new HashDocAttributeSet(); Doc doc=new SimpleDoc(fis,flavor,das); //どのように印刷するかを示す属性 PrintRequestAttributeSet pras=new HashPrintRequestAttributeSet(); //デフォルトの印刷サービス(プリンター・オブジェクト)を取得 PrintService defaultService=PrintServiceLookup.lookupDefaultPrintService(); //************************************************************************************* //印刷ジョブを作成 DocPrintJob job=defaultService.createPrintJob(); //************************************************************************************* // try{ job.print(doc,pras); }catch(PrintException e){ JOptionPane.showMessageDialog(this,"印刷に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } } private void printImageDialog(){ //印刷するオブジェクトのMIME (Multipurpose Internet Mail Extensions) タイプの特定 DocFlavor flavor; String ext=getExtension(fileName); if(ext.equals("jpg") || ext.equals("jpeg")){ flavor=DocFlavor.INPUT_STREAM.JPEG; }else if(ext.equals("gif")){ flavor=DocFlavor.INPUT_STREAM.GIF; }else if(ext.equals("png")){ flavor=DocFlavor.INPUT_STREAM.PNG; }else{ JOptionPane.showMessageDialog(this,"開ける画像ファイルではありません。","エラー",JOptionPane.ERROR_MESSAGE); return; } //ファイル入力 FileInputStream fis; try{ fis=new FileInputStream(fileName); }catch(FileNotFoundException e){ JOptionPane.showMessageDialog(this,"ファイルが見つかりません。","エラー",JOptionPane.ERROR_MESSAGE); return; } //データを印刷ジョブに与えるためのインターフェース DocAttributeSet das=new HashDocAttributeSet(); Doc doc=new SimpleDoc(fis,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; } //************************************************************************************* // try{ job.print(doc,pras); }catch(PrintException e){ JOptionPane.showMessageDialog(this,"印刷に失敗しました。","エラー",JOptionPane.ERROR_MESSAGE); return; } } // private String getExtension(String file){ int comma=file.lastIndexOf('.'); if(comma!=-1){ return file.substring(comma+1).toLowerCase(); }else{ 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; } } }