package bmpviewer; import java.awt.*; import java.awt.event.*; import java.io.*; /** * BMP file viewer *

* @author Hiroyuki Murata * @since JDK1.1 */ class BmpViewer extends Frame implements WindowListener { BmpCanvas canvas; FileDialog diaLoad; TextInputDialog diaSave; Button btnLoad = new Button("Load"), btnSave = new Button("Save"), btnInfo = new Button("Info") ; String strCurrentDir; BmpViewer(String filename) { setupBasic(); setupDialog(); //setupMenu(); // buggy! setupButton(); setupCanvas(filename); pack(); show(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - private void setupBasic() { setBackground(Color.white); addWindowListener(this); } private void setupDialog() { diaLoad = new FileDialog(this, "select BMP|GIF|JPG file"); diaLoad.setFilenameFilter( new FilenameFilter() { public boolean accept(File dir, String name) { String lowname = name.toLowerCase(); return lowname.endsWith(".bmp") || lowname.endsWith(".gif") || lowname.endsWith(".jpg") || lowname.endsWith(".jpeg") ; } }); strCurrentDir = diaLoad.getDirectory(); diaSave = new TextInputDialog( this, "TextInputDialog", "save BMP file as:", 20 ); } private void setupButton() { Panel pBtns = new Panel(); add("South",pBtns); pBtns.add(btnLoad); btnLoad.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { diaLoad.show(); String strDir = diaLoad.getDirectory(); String strFile = diaLoad.getFile(); if (strDir == null || strFile == null) return; File file = new File(strDir, strFile); strCurrentDir = strDir; remove(canvas); canvas = new BmpCanvas(BmpViewer.this, file); add("Center",canvas); canvas.view(); setTitle(strFile); btnSave.setEnabled(true); btnInfo.setEnabled(canvas.isBmp()); pack(); } }); pBtns.add(btnSave); btnSave.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { diaSave.setVisible(true); } }); // diaSave.addEnter( new ActionListener() { public void actionPerformed(ActionEvent ev) { String filename = diaSave.getText(); String lowName = filename.toLowerCase(); if (!lowName.endsWith(".bmp")) filename += filename.equals(lowName) ? ".bmp" : ".BMP"; File savepath = new File(strCurrentDir, filename); if (savepath.exists()) { MessageDialog md = new MessageDialog( BmpViewer.this, "warning", "File \"" + filename + "\":\n already exists." ); } else { boolean complete = canvas.saveBMP(savepath); if (!complete) { MessageDialog md = new MessageDialog( BmpViewer.this, "warning", "File \"" + filename + "\":\n save failed." ); } } diaSave.setVisible(false); } }); pBtns.add(btnInfo); btnInfo.setEnabled(false); btnInfo.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { MessageDialog md = new MessageDialog( BmpViewer.this, "BMP header", canvas.getReaderInfo() ); } }); } private void setupCanvas(String filename) { File file; if (filename == null) { file = null; btnSave.setEnabled(false); } else { file = new File(System.getProperty("user.dir"), filename); } canvas = new BmpCanvas(this, file); add("Center",canvas); canvas.view(); } //------------------------------------------------------ public static void main(String[] args) { if (args.length == 0) { new BmpViewer(null); } else { new BmpViewer(args[0]); } } // as WindowListener ----------------------------------- public void windowClosing(WindowEvent ev) { System.exit(0); } public void windowOpened(WindowEvent ev) {} public void windowClosed(WindowEvent ev) {} public void windowIconified(WindowEvent ev) {} public void windowDeiconified(WindowEvent ev) {} public void windowActivated(WindowEvent ev) {} public void windowDeactivated(WindowEvent ev) {} }