// gr2pictlet.java // derived from Jlpr (C) 1996 E.J.Friedman-Hill and Sandia National Labs /** Prints/saves applets using the Gr2PICT PICT Graphics context @author Don Gilbert, 1996 */ import java.awt.*; import java.applet.*; import java.net.*; import java.io.*; import java.lang.*; import java.util.*; import dclap.*; // for Gr2PICT, QD, SavePICT public class Gr2PICTlet extends Applet implements Runnable { String host, path, label, file; int port = 80; PrintMenu pm; Thread menuThread, printThread; Button print,refresh; Choice appletChoice; TextField tf; Checkbox useFile, useNet; CheckboxGroup saveMethod; TestDraw testd; public static void main(String argv[]) { Gr2PICTlet app = new Gr2PICTlet(); app.init(argv); } public Gr2PICTlet() { } public String getAppletInfo() { return "Gr2PICTlet by Don Gilbert, based on Jlpr by E.J.Friedman-Hill"; } public String[][] getParameterInfo() { String[][] info = { {"LABEL", "string", "Displayed on print button"}, {"HOST", "string", "Internet host where file echoer lives"}, {"PORT", "integer", "Internet port where file echoer lives"}, {"PATH", "string", "Path of file echoer (e.g., /cgi-bin/echo"} }; return info; } public void init() { init(null); } public void init(String[] args) { try { host = getParameter("HOST"); String sport= getParameter("PORT"); if (sport!=null) port= Integer.parseInt( sport); path = getParameter("PATH"); label = getParameter("LABEL"); } catch (NullPointerException nullex) { try { URL baseu= getCodeBase(); if (baseu!=null && host==null) { host= baseu.getHost(); port= baseu.getPort(); } } catch (Exception ex) {} if (path==null) path= "/cgi-bin/echo"; } if (file == null && (host == null || path == null)) { showStatus("Sorry, failed to initialize app"); return; } if (label == null) label = "Print"; setLayout(new BorderLayout()); Panel np = new Panel(); np.setLayout(new FlowLayout()); add("South", np); saveMethod = new CheckboxGroup(); np.add(useNet = new Checkbox("Use Net", saveMethod, true)); useNet.setBackground(Color.white); np.add(useFile = new Checkbox("Use File", saveMethod, false)); useFile.setBackground(Color.white); np.add(tf = new TextField("myapplet.pict")); tf.setBackground(Color.white); np.add( new Checkbox("Check", null, true)); np = new Panel(); np.setLayout(new FlowLayout()); add("Center", np); np.add(testd= new TestDraw()); np = new Panel(); np.setLayout(new FlowLayout()); add("North", np); np.add(print = new Button(label)); print.setBackground(Color.white); np.add(refresh = new Button("Refresh")); refresh.setBackground(Color.white); np.add(appletChoice = new Choice()); appletChoice.addItem(getClass().getName()); appletChoice.select(0); pm = new PrintMenu(this); } /** * Start the applet by forking a thread * which will look for other applets -- note that start() * doesn't call run() for this class! */ public void start() { if (menuThread == null) { menuThread = new Thread(pm); menuThread.start(); } } public void stop() { if (menuThread != null) { menuThread.stop(); menuThread = null; } if (printThread != null) { printThread.stop(); printThread = null; } } private Applet printWhat() { // what applet type is selected in choice? String type = appletChoice.getSelectedItem().trim(); // Find an applet of this type Enumeration en = getAppletContext().getApplets(); while (en.hasMoreElements()) { Object a = en.nextElement(); if (a == null) continue; String atype = a.getClass().getName(); if (type.equals(atype)) return (Applet) a; } return null; } public void run() { Applet printThis = printWhat(); if (printThis == null) return; SavePICT saver= new SavePICT( this); String data; try { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); ByteArrayOutputStream baos = new ByteArrayOutputStream(600000); saver.saveAsPict( printThis, baos); data = baos.toString(); } catch (Throwable t) { showStatus("Error during rendering."); System.out.println(t); return; } boolean done= false; if (saveMethod.getCurrent() == useFile) { file = tf.getText(); if (file == null || file.length() == 0) { file = "myapplet.pict"; tf.setText(file); } done= saver.saveToFile( file, data); } if (!done) { saver.saveThruServer( host, port, path, data); } } public boolean handleEvent(Event e) { if (e.target == appletChoice && e.id == Event.ACTION_EVENT) { // leave things alone on error return true; } else if (e.target == print && e.id == Event.ACTION_EVENT) { if (printThread == null || !printThread.isAlive()) { printThread = new Thread(this); printThread.start(); return true; } } else if (e.target == refresh && e.id == Event.ACTION_EVENT) { if (menuThread != null) { menuThread.stop(); menuThread = null; } start(); return true; } return false; } } class PrintMenu implements Runnable { Gr2PICTlet p; private static int exceptionCount = 0; public PrintMenu(Gr2PICTlet p) { this.p = p; } private boolean findInChoice(String s, Choice c) { for (int i=0; i< c.countItems(); i++) if (c.getItem(i).trim().equals(s)) return true; return false; } public void run() { Choice c = p.appletChoice; Object a; String type; try { Enumeration e = p.getAppletContext().getApplets(); // put all applets on the menu. while (e.hasMoreElements()) { a = e.nextElement(); if (a == null) continue; type = a.getClass().getName(); if (!findInChoice(type,c)) c.addItem(type); } } catch (Throwable ex) { // if anything bad happens, wait a bit, then try again, five times ++exceptionCount; try { System.out.println(ex); Thread.currentThread().sleep(200); } catch (java.lang.InterruptedException ex2) { // whatever } if (exceptionCount < 5) run(); } c.layout(); exceptionCount = 0; } } class TestDraw extends Canvas { TestDraw() { setBackground(Color.white); resize( minimumSize()); } public Dimension minimumSize() { return new Dimension(200, 130); } public void paint(Graphics g) { int x, y, w, h; Dimension d= size(); x= 0; y= 0; w= d.width-1; h= d.height-1; g.drawRect( x, y, w, h); x += 20; y += 20; w *= 0.5; h *= 0.6; g.setFont( new Font("Times",Font.PLAIN, 12)); g.drawString("TestDraw", x, y); g.setColor( Color.red); g.fillRect( x, y, w, h); g.setColor( Color.black); g.drawLine( x, y, x+w, y+h); x += 20; y += 20; w *= 0.5; h *= 0.6; g.setColor( Color.green); g.fillOval( x, y, w, h); g.setFont( new Font("Courier",Font.BOLD, 14)); g.drawString("TestDraw", x, y); x += 20; y += 20; w *= 0.5; h *= 0.6; g.setColor( Color.blue); g.fillRoundRect( x, y, w, h, 10, 10); g.setFont( new Font("Helvetica",Font.ITALIC, 18)); g.drawString("TestDraw", x, y); } }