home *** CD-ROM | disk | FTP | other *** search
- <$$FILE
- TYPE=JAVA
- DESC="Java Image Handler Applet Generator"
- >
- <$$PARAMS
- CLASSNAME=STRING CLASSNAME_D="Name of class"
- NOTE1=NOTICE NOTE1_D="(Should be the same as the filename, but you can do whatever you want...)"
- PAGE
-
- CNONE=RADIO CNONE_D="No comments"
- CBASE=RADIO CBASE_D="Basic comments"
- CVERBOSE=RADIO CVERBOSE_M=ON CVERBOSE_D="Verbose comments"
- PAGE
-
- DEBUG=CHECK DEBUG_D="Extra debugging support"
- MOUSEHANDLER=CHECK MOUSEHANDLER_D="Generate mouse event handler code"
- PAGE
-
- NOIMAGES=RADIO NOIMAGES_D="Do not handle images at all"
- LOADIMAGES=RADIO LOADIMAGES_D="Generate code to load a series of images"
- ANIMATEIMAGES=RADIO ANIMATEIMAGES_M=ON
- ANIMATEIMAGES_D="Load a series of images and use buffers and threads to animate them"
- PAGE
-
- GENERATEHTML=CHECK GENERATEHTML_M="ON"
- GENERATEHTML_D="Generate a sample HTML page for the applet"
- ><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
-
- // <$$INSERT PARAM=~CLASSNAME>, generated with the Mortar JavaImageHandler gnome
- //
- <$$IF S="CVERBOSE=ON"><$$NO>
- // This applet is customizable through APPLET tags; to gain this functionality
- // you should call it from an HTML document rather than directly as a .class
- // file with the interpreter.
- //
- <$$ELSE><$$ENDIF><$$NO>
- // Parameters
- // NAME DEFAULT/EX VAL. VALUE FORMAT, DESCRIPTION
- //------------------- --------------- ----------------------------------------
- <$$IF S="MOUSEHANDLER=ON"><$$NO>
- // stopOnClick false boolean (true/false), stop on mouse clicks
- // Toggles running the animation thread each time a mouse button is pressed
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="NOIMAGES=ON"><$$ELSE><$$NO>
- // imageX image1 relative URL to the image (from 1)
- <$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- // delay 200 milliseconds between frames
- // clear true clear output before switching images?
- <$$ELSE><$$ENDIF><$$NO>
- // backgroundColor 000000 RRGGBB, in base 16 (hexadecimal); 00-FF
- <$$ELSE><$$ENDIF><$$NO>
-
- <$$IF S="GENERATEHTML=ON"><$$NO>
- /* Here is a sample HTML document which you can cut and paste
- into any HTML file to use your applet, assuming it is placed
- in the same directory.
-
- <$$INSERT PARAM="<">HTML>
- <$$INSERT PARAM="<">HEAD>
- <$$INSERT PARAM="<">TITLE>The <$$INSERT PARAM=~CLASSNAME> Applet<$$INSERT PARAM="<">/TITLE>
- <$$INSERT PARAM="<">/HEAD>
-
- <$$INSERT PARAM="<">BODY>
- <$$INSERT PARAM="<">H1>The <$$INSERT PARAM=~CLASSNAME> Applet<$$INSERT PARAM="<">/H1>
-
- Have a look at an applet generated by the
- Mortar Java Image Handler Applet Creation Gnome!
-
- <$$INSERT PARAM="<">p>
-
- <$$INSERT PARAM="<">APPLET CODE="<$$INSERT PARAM=~CLASSNAME>" WIDTH=400 HEIGHT=400>
- <$$IF S="MOUSEHANDLER=ON"><$$NO>
- <$$INSERT PARAM="<">PARAM NAME=stopOnClick VALUE=true>
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="NOIMAGES=ON"><$$ELSE><$$NO>
- <$$INSERT PARAM="<">PARAM NAME=image0 VALUE="asdf.jpg">
- <$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- <$$INSERT PARAM="<">PARAM NAME=delay VALUE=200>
- <$$INSERT PARAM="<">PARAM NAME=clear VALUE=false>
- <$$ELSE><$$ENDIF><$$NO>
- <$$INSERT PARAM="<">PARAM NAME=backgroundColor VALUE=100030>
-
- <$$INSERT PARAM="<">IMG SRC="does_not_exist.gif" WIDTH=400 HEIGHT=400
- ALT="You must have a Java-capable browser to see the wonder!"
- >
- You must have a Java-capable browser to see the wonder!
- <$$INSERT PARAM="<">/APPLET>
-
- <$$INSERT PARAM="<">/BODY>
- <$$INSERT PARAM="<">/HTML>
-
-
- */
- <$$ELSE><$$ENDIF><$$NO>
-
- import java.applet.Applet;
- import java.awt.*;
- import java.net.*;
- import java.util.*;
-
- public class <$$INSERT PARAM=~CLASSNAME> extends Applet <$$NO>
- <$$IF S="ANIMATEIMAGES=ON">implements Runnable <$$ELSE><$$ENDIF>
- {
- <$$IF S="DEBUG=ON"><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // To turn on printing to System.out (for debugging), change
- // SYSTEMOUTPUT to true. If MOUSEINFO is true, detailed information
- // about mouse events will be printed.
- <$$ELSE><$$ENDIF><$$NO>
- public static final boolean SYSTEMOUTPUT = false;
- public static final boolean MOUSEINFO = false;
-
- <$$ELSE><$$ENDIF><$$NO>
- public Color backgroundColor, foregroundColor;
-
- <$$IF S="MOUSEHANDLER=ON"><$$NO>
- public boolean threadSuspended=false;
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Parameter variables
- <$$ELSE><$$ENDIF><$$NO>
- public int delay;
- public boolean clear;
-
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // the off-screen image, for flicker-free updates,
- // and a Thread variable for our animating thread.
- <$$ELSE><$$ENDIF><$$NO>
- protected Image output;
- protected int imagewidth, imageheight;
- private Thread animatorThread = null;
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON | LOADIMAGES=ON"><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Image-related variables
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
- protected int numImages=0; // the number of frames in the animation (1-based)
- protected MediaTracker tracker; // the Tracker is used to monitor the loading of images
- protected ImageLinkedList images, currentImage;
- // the list of images, and a pointer to a "current" one
-
- <$$ELSE><$$NO>
- protected int numImages=0;
- protected MediaTracker tracker;
- protected ImageLinkedList images, currentImage;
-
- <$$ENDIF><$$NO>
- <$$ELSE><$$ENDIF><$$NO>
- public void init()
- {
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Create the foregroundColor as the complementary to the backgroundColor
- <$$ELSE><$$ENDIF><$$NO>
- backgroundColor = IEExtend.strColor( getParameter( "backgroundColor" ), Color.black);
- foregroundColor = new Color( 255-backgroundColor.getRed(),
- 255-backgroundColor.getGreen(),
- 255-backgroundColor.getBlue() );
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
-
- String junk;
-
- delay = IEExtend.strInt( getParameter( "delay" ), 200 );
-
- junk=getParameter( "clear" );
- if( junk==null )
- clear=false;
- else
- clear = (junk.equals("true") ? true : false);
-
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Create the off-screen image of appropriate size
- <$$ELSE><$$ENDIF><$$NO>
- updateOSI();
-
- Graphics g = output.getGraphics();
- g.setColor( backgroundColor );
- g.fillRect(0,0,size().width-1,size().height-1);
- g.setColor( foregroundColor );
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON | LOADIMAGES=ON"><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Set up a MediaTracker to track the loading of images
- // and initialize the image Linked List.
- <$$ELSE><$$ENDIF><$$NO>
- tracker = new MediaTracker( this );
- images = new ImageLinkedList();
- currentImage = images;
-
- <$$IF S="CVERBOSE=ON"><$$NO>
- // The loop should stop when an image can't be loaded
- // because the parameter for that image doesn't exist.
- <$$ELSE><$$ENDIF><$$NO>
- for( int i=1; numImages+1==i; i++ )
- {
- currentImage.name = this.getParameter( "image" + i );
- if( currentImage.name==null )
- {
- <$$IF S="CVERBOSE=ON"><$$NO>
- // The applet will animate whatever images loaded successfully.
-
- <$$ELSE><$$ENDIF><$$NO>
- currentImage.name="";
- <$$IF S="DEBUG=ON"><$$NO>
- String message = "Bad PARAM tag for next image <$$INSERT PARAM="<">PARAM NAME="
- + "image" + i + " VALUE=" + currentImage.name + "<$$INSERT PARAM=">">" + "\n"
- + "If you only specified this many, then that's good!";
- if( SYSTEMOUTPUT ) System.out.println( message );
- // output.getGraphics().drawString( message, 10, 70 );
- <$$ELSE><$$ENDIF><$$NO>
- if( currentImage.previous != null )
- {
- (currentImage.previous).next=null;
- currentImage=null;
- }
- }
- else
- {
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Everything's good, so register with the tracker.
-
- <$$ELSE><$$ENDIF><$$NO>
- currentImage.image = this.getImage( this.getDocumentBase(), currentImage.name);
- tracker.addImage(currentImage.image, i);
- currentImage.number = i;
-
- <$$IF S="DEBUG=ON"><$$NO>
- if( SYSTEMOUTPUT )
- {
- System.out.println( "Registered image called " + currentImage.name );
- if( currentImage.previous != null )
- System.out.println( "Previous called " + currentImage.previous.name );
- }
-
- <$$ELSE><$$ENDIF><$$NO>
- currentImage.next=new ImageLinkedList();
- (currentImage.next).previous=currentImage;
- currentImage=currentImage.next;
- numImages++;
- }
- }
- repaint();
- <$$ELSE><$$ENDIF><$$NO>
- }
-
- <$$IF S="ANIMATEIMAGES=ON | LOADIMAGES=ON"><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
- // The Applet's start method, called by the browser after init()
- <$$ELSE><$$ENDIF><$$NO>
- public void start()
- {
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
- // If the thread has not been previously initialized, start it.
- <$$ELSE><$$ENDIF><$$NO>
- if( animatorThread == null )
- {
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Start another thread on this object);
- <$$ELSE><$$ENDIF><$$NO>
- animatorThread = new Thread(this);
- <$$IF S="CVERBOSE=ON"><$$NO>
-
- // Will ultimately cause a separate thread to call the run method
- <$$ELSE><$$ENDIF><$$NO>
- animatorThread.start();
- }
- <$$ELSE><$$NO>
- loadResultMessage = loadImages();
- <$$ENDIF><$$NO>
- }
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
- // The Applet's stop method, called by a browser to cease action.
- <$$ELSE><$$ENDIF><$$NO>
- public void stop()
- {
- if(( animatorThread != null) && animatorThread.isAlive())
- animatorThread.stop();
- animatorThread = null;
- }
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="MOUSEHANDLER=ON"><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Mouse button handling code
- <$$ELSE><$$ENDIF><$$NO>
- public boolean mouseDown(Event event, int x, int y)
- {
- <$$IF S="MOUSEHANDLER=ON"><$$NO>
- if( threadSuspended==false )
- threadSuspended=true;
- else
- threadSuspended=false;
-
- <$$ELSE><$$ENDIF><$$NO>
- String message;
- <$$IF S="DEBUG=ON">
- if( SYSTEMOUTPUT )
- {
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Call to System.out for debugging
- <$$ELSE><$$ENDIF><$$NO>
- System.out.println("in mousedown");
- System.out.println("Mods " + event.modifiers);
- }
-
- if( MOUSEINFO )
- {
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- Graphics g = output.getGraphics();
- <$$ELSE><$$NO>
- Graphics g = this.getGraphics();
- <$$ENDIF><$$NO>
- g.setColor( backgroundColor );
- g.fillRect(0, 0, size().width-1, size().height-1);
- g.setColor(foregroundColor);
-
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Right click sets META_MASK
- <$$ELSE><$$ENDIF><$$NO>
- if ((event.modifiers & Event.META_MASK) != 0)
- message = new String("Right button");
- else
- message = new String("Left button");
-
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Check CTRL key
- <$$ELSE><$$ENDIF><$$NO>
- if ((event.modifiers & Event.CTRL_MASK) != 0)
- message += " + CTRL";
-
- <$$IF S="CVERBOSE=ON"><$$NO>
- // Check SHIFT key
- <$$ELSE><$$ENDIF><$$NO>
- if ((event.modifiers & Event.SHIFT_MASK) != 0)
- message += " + SHIFT";
-
- g.drawString(message, 30, 25);
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
-
- repaint();
- <$$ELSE><$$ENDIF><$$NO>
- }
- return true;
- }
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // This method handles drawing.
- <$$ELSE><$$ENDIF><$$NO>
- public void paint(Graphics g)
- {
- Dimension d = this.size();
-
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- if( (imagewidth != d.width) || (imageheight != d.height ))
- {
- updateOSI();
- }
-
- if (output != null)
- {
- g.drawImage(output, 0, 0, this);
- }
- <$$ELSE><$$NO>
- g.setColor( backgroundColor );
- g.fillRect(0,0,size().width-1,size().height-1);
- <$$IF S="LOADIMAGES=ON"><$$NO>
- if( loadResultMessage!="" )
- {
- g.setColor( foregroundColor );
- g.drawString( loadResultMessage, 10, 20 );
- this.showStatus( loadResultMessage );
- }
- <$$ELSE><$$ENDIF><$$NO>
- <$$ENDIF><$$NO>
- }
-
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // This is overridden because the paint() method induces an unneeded clear.
- <$$ELSE><$$ENDIF><$$NO>
- public void repaint()
- {
- Graphics g = this.getGraphics();
- g.drawImage( output, 0, 0, this );
- }
-
- public void updateOSI()
- {
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Handle resizing of the applet and OSI
- <$$ELSE><$$ENDIF><$$NO>
- Graphics g;
- Image newOSI = createImage(size().width-1, size().height-1);
- g = newOSI.getGraphics();
-
- g.setColor( backgroundColor );
- g.fillRect(0,0,size().width-1,size().height-1);
- if( output!=null )
- g.drawImage( output, 0, 0, this );
-
- imagewidth=size().width;
- imageheight=size().height;
-
- output=newOSI;
- }
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Implements Runnable Interface
- <$$ELSE><$$ENDIF><$$NO>
- public void run()
- {
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="LOADIMAGES=ON"><$$NO>
- protected String loadResultMessage = "";
-
- public String loadImages()
- {
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON | LOADIMAGES=ON"><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
- // This method is called when the animatorThread is started,
- // care of Runnable interface.
-
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // This code will wait for the loading of the images to
- // complete before doing anything else, then sequence the images.
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$ELSE><$$ENDIF><$$NO>
- this.showStatus( "Loading images..." );
- try
- {
- tracker.waitForAll();
- }
- catch (InterruptedException e)
- {}
-
- if( tracker.isErrorAny() );
- {
- currentImage=images;
- for( int i=1; i<$$INSERT PARAM="<">=numImages; i++ )
- {
- if (tracker.isErrorID(i))
- {
- String message = "Error loading \"image" + i + "\" ("
- + currentImage.name + "); quitting.";
- this.showStatus( message );
- <$$IF S="DEBUG=ON"><$$NO>
- if( SYSTEMOUTPUT )
- System.out.println( message );
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="LOADIMAGES=ON"><$$NO>
- <$$IF S="CVERBOSE=ON"><$$NO>
-
- // The whole reason for this is because when the start()
- // method exits, any Status message it has set is replaced
- // with something like "Applet started.", so an alternate
- // way of delivering a message was established.
- <$$ELSE><$$ENDIF><$$NO>
- return message;
- <$$ELSE><$$NO>
- return;
- <$$ENDIF><$$NO>
- }
- currentImage=currentImage.next;
- }
- }
-
- <$$IF S="DEBUG=ON"><$$NO>
- /* This an inefficient way to do it, but it's here because it's
- slightly easier to debug.
-
- for( int i=0; i<$$INSERT PARAM="<">numImages;i++ )
- {
- this.showStatus( "Loading image " + i + "." );
- try
- {
- tracker.waitForID(i);
- }
- catch (InterruptedException e)
- {}
-
- if (tracker.isErrorID(i))
- {
- this.showStatus( "Error loading image " + i + "; quitting." );
- }
- }
- */
-
- <$$ELSE><$$ENDIF><$$NO>
- this.showStatus( "Animating " + numImages + " images." );
- <$$ELSE><$$ENDIF><$$NO>
- <$$IF S="ANIMATEIMAGES=ON"><$$NO>
-
- currentImage=images;
- threadSuspended=false;
- while (true)
- {
- try
- {
- <$$IF S="MOUSEHANDLER=ON"><$$NO>
- if( !threadSuspended )
- {
- <$$ELSE><$$NO>
- <$$IF S="CNONE"><$$NO>
- {
- <$$ELSE><$$NO>
- { // braces are for functionality you didn't ask for
- <$$ENDIF><$$NO>
- <$$ENDIF><$$NO>
- Graphics g = output.getGraphics();
- if( clear )
- {
- g.setColor( backgroundColor );
- g.fillRect(0,0,size().width-1,size().height-1);
- }
- g.setColor( foregroundColor );
- <$$IF S="DEBUG=ON"><$$NO>
- // g.drawString( "yummy thread is running", 10, 50 );
- <$$ELSE><$$ENDIF><$$NO>
- g.drawImage( currentImage.image, 0, 0, this );
- repaint();
- Thread.sleep(delay);
- if( currentImage.next != null )
- currentImage = currentImage.next;
- else
- currentImage = images;
- }
- }
- catch (InterruptedException e)
- {}
- }
- }
-
- <$$ELSE><$$NO>
- <$$IF S="LOADIMAGES=ON"><$$NO>
- return (numImages + " images loaded successfully.");
- }
-
- <$$ELSE><$$ENDIF><$$NO>
- <$$ENDIF><$$NO>
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // For running standalone
- <$$ELSE><$$ENDIF><$$NO>
- public static void main(String[] argv)
- {
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // Create the frame and launch animator
-
- // Do note that getParameter doesn't really work without an
- // Applet context from which to retrieve the parameters, so...
- // you will have to make a way around it. I, personally,
- // recommend a global String[], which could easily replace all the
- // getParameter stuff since it already has an index (usually i)
- // which you can use! Isn't that clever? Thanks...
-
- <$$ELSE><$$ENDIF><$$NO>
- Frame f = new Frame("animatorFrame");
- f.reshape(100, 100, 200, 100);
- f.show();
-
- <$$INSERT PARAM=~CLASSNAME> x = new <$$INSERT PARAM=~CLASSNAME>();
- f.add("Center", x);
- x.init();
- x.start();
- }
- }
-
- class IEExtend
- {
- <$$IF S="CBASE=ON | CVERBOSE=ON"><$$NO>
- // The IE Extend class faciliates importing variables
- // in different formats
-
- // Modified for consistency by JAR
-
- <$$ELSE><$$ENDIF><$$NO>
- public final static int strInt(String c, int def)
- {
- if(c!=null && c.length() > 0)
- {
- try
- {
- return Integer.parseInt(c);
- }
- catch(NumberFormatException e)
- {
- return def;
- }
- }
- return def;
- }
-
- public final static double strDouble(String c, double def)
- {
- if(c!=null && c.length() > 0)
- {
- try
- {
- return Double.valueOf(c).doubleValue();
- }
- catch(NumberFormatException e)
- {
- return def;
- }
- }
- return def;
- }
-
- public final static Color strColor(String c, Color def)
- {
- if(c == null)
- return def;
- return colorFromStr(c,def);
- }
-
- public final static Color colorFromStr(String c, Color def)
- {
- if(c.length() <$$INSERT PARAM="<"> 6)
- return def;
- String t;
- t = new String();
- t += c.charAt(0);
- t += c.charAt(1);
- int r = Integer.parseInt(t,16);
- t = new String();
- t += c.charAt(2);
- t += c.charAt(3);
- int g = Integer.parseInt(t,16);
- t = new String();
- t += c.charAt(4);
- t += c.charAt(5);
- int b = Integer.parseInt(t,16);
-
- return new Color(r,g,b);
- }
- }
- <$$IF S="ANIMATEIMAGES=ON | LOADIMAGES=ON"><$$NO>
-
- class ImageLinkedList
- {
- ImageLinkedList next=null;
- ImageLinkedList previous=null;
- Image image=null;
- String name="";
- int number=0;
- }
- <$$ELSE><$$ENDIF><$$NO>
-
-
-