NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
Hi all, as I was just the day before yesterday on the same topic, I thought I'd contribute to the general knowledge of the list. To implement a "save screenshot" JButton I do: snapBut= new JButton("Snapshot"); snapBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveDisplay(); } }); private void saveDisplay(){ Runnable captureImage = new Runnable() { public void run() { try{ JFileChooser fc= new JFileChooser(); fc.setDialogTitle("Save screenshot as Jpeg"); String dir = fc.getCurrentDirectory().getAbsolutePath(); if(fc.showDialog(cframe, "Save") == JFileChooser.APPROVE_OPTION) { System.out.println("Saving image..." ); String cfn = fc.getSelectedFile().getName(); BufferedImage image = display.getImage(); FileOutputStream ffout = new FileOutputStream(dir+File.separator+cfn); JPEGEncodeParam jepar JPEGCodec.getDefaultJPEGEncodeParam(image); jepar.setQuality( 1.0f, true); JPEGImageEncoder jpege = JPEGCodec.createJPEGEncoder(ffout) ; jpege.encode(image, jepar); ffout.close(); System.out.println("Saved "+cfn); } else { System.out.println("No file was chosen or an error occurred"); } } catch(FileNotFoundException e){e.printStackTrace(); } catch(IOException e){e.printStackTrace(); } } }; Thread t = new Thread(captureImage); t.start(); } with credits to Curtis (see the SpreadSheet, where such save display method is implemented). To print a display using a JButton I do: printBut= new JButton("Print"); printBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { printDisplay(); } }); private void printDisplay(){ Runnable printImage = new Runnable() { public void run() { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(display.getPrintable()); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception pe) { pe.printStackTrace(); } } } }; Thread t = new Thread(printImage); t.start(); } Please note that in my app I've only got one display. The catch in the "print display", is that it brings up a dialog where you have to specify the absolute path, when printing to a file, otherwise you get a nasty "no write permission" message, or something like that. I'd be happy if someone could tell me how to call the print dialog with the full path name... Oh, yes, this is done under Unix; I don't have a clue how it behaves in Windows (I mena the stuff with the absolute path). Of course, you can print direct to a printer, too :-) Cheers, Ugo
visad
archives: