// savepict.java // d.g.gilbert, 1997 package dclap; // pack edu.indiana.bio.dclap; import java.awt.*; import java.applet.*; import java.net.*; import java.io.*; import java.lang.*; import java.util.*; import Acme.JPM.Encoders.*; public interface ImageLast { } public class SavePICT { public static Point gTranslate= new Point(0,0); Applet app = null; Component view; OutputStream os; int format; public SavePICT() { } public SavePICT( Applet app) { this.app= app; } public SavePICT( int format, Component theView, OutputStream os) { this(null, format, theView, os); } public SavePICT( Applet app, int format, Component theView, OutputStream os) { this.app= app; this.format= format; this.view= theView; this.os= os; saveComponent(); } public void setView( Component theView ) { this.view= theView; } public void setStream( OutputStream os) { this.os= os; } public void setFormat( int format) { this.format= format; } public void saveComponent() { if (view!=null && os!=null) switch (format) { case DocFormat.PICT: toPICT( view, os); break; case DocFormat.JPEG: toJPEG( view, os); break; case DocFormat.GIF: toGIF( view, os); break; case DocFormat.POSTSCRIPT: toPostscript( view, os); break; } } protected void showStatus( String msg) { if (app==null) Debug.println( msg); else app.showStatus( msg); } public boolean saveToFile(String file, String data) { boolean okay= false; try { PrintStream f = new PrintStream(new FileOutputStream(file)); f.print(data); f.close(); showStatus("Printing Complete."); okay= true; } catch (Throwable t) { showStatus("Error while saving file."); System.out.println(t); } return okay; } public boolean saveThruServer(String host, int port, String path, String data) { boolean okay= false; try { //Socket datapipe = new Socket(host, port); URL datau= new URL("http", host, port, ""); URLConnection datapipe= datau.openConnection(); DataInputStream dis = new DataInputStream(datapipe.getInputStream()); PrintStream dos = new PrintStream(datapipe.getOutputStream()); dos.println("POST " + path + " HTTP/1.0"); dos.print("Content-length: "); dos.println(data.length()); dos.println(); dos.println(data); while (!(dis.readLine().equals(""))); // protocol responses String newfile = dis.readLine(); dis.close(); dos.close(); URL u = new URL(newfile); // display the data in a new frame if (app!=null) app.getAppletContext().showDocument(u,"_pict"); // otherwise, do what with it?? presumably can't save to disk... showStatus("Printing Complete."); okay= true; } catch (Throwable t) { showStatus("Error during data transmission."); System.out.println(t); } return okay; } public void toPICT(Component theView, OutputStream os) { Gr2PICT pict= new Gr2PICT(os, theView.getGraphics()); paintInside(theView,pict); pict.finalize(); // make sure pict end is written } public void toPostscript(Component theView, OutputStream os) { dclap.util.PSGr pict = new dclap.util.PSGr(os, theView.getGraphics()); paintInside(theView,pict); pict.finalize(); } public void toGIF(Component theView, OutputStream os) { Image img= getImage( theView); if (img==null) return; // debug code try { ImageEncoder enc= new GifEncoder(img, os); enc.encode(); } catch (Throwable t) { showStatus("Error during image encoding."); System.out.println(t); } } public void toJPEG(Component theView, OutputStream os) { /** Image img= getImage( theView); if (img==null) return; try { //!! JpegEncoder is incomplete - writes only header !? ImageEncoder enc= new JpegEncoder(img, os); enc.encode(); } catch (Throwable t) { showStatus("Error during image encoding."); System.out.println(t); } **/ } /*** // debugging !!!! protected void grabImage(Image img) { Debug.println("SavePICT.getImage - grabbing pixels..."); int w = (short)img.getWidth(null); int h = (short)img.getHeight(null); //if (w>50) w= 50; if (h>50) h= 50; Debug.println("SavePICT.grabImage - PixelConsumer..."); dclap.util.PixelConsumer pc = new dclap.util.PixelConsumer(img); Hashtable colortab = new Hashtable(); for (int y=0; y