/* * Copyright (c) 1998 Kevan Stannard. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and * without fee is hereby granted. * * Please note that this software comes with * NO WARRANTY * * BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED * BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES * PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS * TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. * * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER * PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, * INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE * THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED * BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER * OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */ import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import java.util.*; public class JZOOBanner extends Applet implements Runnable { private Thread thread; private int appletWidth, appletHeight; private Image db; private Graphics dbGraphics; private MediaTracker tracker; private Image currImage, nextImage; private int nextImageX, nextImageY; private int currImageX, currImageY; private int moveX, moveY; // The 'wait' variable contains a number which gets decremented // each time we go round the main loop. // When delay == 0, then we check if the next image is ready // to display. Otherwise we wait private int delay = 1; private int wait; // speed of scroll int speed = 2; // url to go to if the image is clicked URL url, nextURL; boolean showURLStatus = false; boolean updateURLStatus = false; // general animation bits int pc=0; boolean needImage = true; boolean doAnim = true; boolean animDone = false; boolean nextImageReady = false; // border stuff int borderSize = 1; Color borderColor = Color.black; // bgcolor stuff Color bgColor = Color.white; // list of instructions for display String defFileName = null; Vector def = null; Vector defParsed = null; // command variables private final int UP = 1; private final int DOWN = 2; private final int LEFT = 3; private final int RIGHT = 4; private final int UP_LEFT = 5; private final int UP_RIGHT = 6; private final int DOWN_LEFT = 7; private final int DOWN_RIGHT = 8; private final int FLICK = 9; private int direction; private boolean randomDirection = false; // applet routines public void init() { setBackground(bgColor); appletWidth = this.size().width; appletHeight = this.size().height; db = createImage(appletWidth, appletHeight); dbGraphics = db.getGraphics(); getParameters(); tracker = new MediaTracker(this); } public void start() { if (thread==null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread!=null) { thread.stop(); thread = null; } } public void update(Graphics g) { paint(dbGraphics); g.drawImage(db,0,0,null); } public void paint(Graphics g) { g.setColor(bgColor); g.fillRect(0,0,appletWidth, appletHeight); if (currImage != null) { int w=currImage.getWidth(this); int h=currImage.getHeight(this); int xPos = (appletWidth-w) >> 1; int yPos = (appletHeight-h) >> 1; g.drawImage(currImage,xPos+currImageX,yPos+currImageY,null); } if (doAnim && nextImageReady) { int w=nextImage.getWidth(this); int h=nextImage.getHeight(this); int xPos = (appletWidth-w) >> 1; int yPos = (appletHeight-h) >> 1; g.drawImage(nextImage,xPos + nextImageX,yPos+nextImageY,null); } // draw our border if (borderSize>0) { g.setColor(borderColor); for (int i=1; i<=borderSize; i++) { g.drawRect(i-1,i-1,appletWidth-((i-1)*2 + 1), appletHeight-((i-1)*2 + 1)); } } } public boolean mouseEnter(Event e, int x, int y) { showURLStatus = true; updateURLStatus = true; return true; } public boolean mouseExit(Event e, int x, int y) { showURLStatus = false; if (url != null) { showStatus(""); } return true; } public boolean mouseDown(Event e, int x, int y) { if (url!=null) { AppletContext context = getAppletContext(); context.showDocument(url); } return true; } public void run() { // get the definition file if (defFileName == null) { error("'definition' parameter missing"); } else { def = getFile(defFileName); } pc=0; needImage = true; doAnim = true; while (def != null) { // if we need an image, then process the command file // until we've got a new image to display while (needImage) { // firstly, get the two components of a command // which we'll call comName and comValue // (where a command is of the form // [comName]=[comValue] String comString = (String) def.elementAt(pc); int equPos = comString.indexOf('='); if (equPos<0) { pc = (pc+1) % def.size(); continue; } String comName = comString.substring(0,equPos); String comValue = comString.substring(equPos+1, comString.length()); // now process the command and value // the 'direction' command changes the direction // in which the images scroll onto the screen // adjust delay if (comName.equals("delay")) { try { delay = Integer.parseInt(comValue); } catch (NumberFormatException e) { error("Error converting delay value in command file: " + comValue); delay = 3; } } // do bgcolor stuff if (comName.equals("bgcolor")) { try { int newCol = Integer.parseInt(comValue,16); bgColor = new Color(newCol); } catch (NumberFormatException e) { error("Error converting bgcolor value in command file: " + comValue); } } // do border color stuff if (comName.equals("bordercolor")) { try { int newCol = Integer.parseInt(comValue,16); borderColor = new Color(newCol); } catch (NumberFormatException e) { error("Error converting bordercolor value in command file: " + comValue); } } // do bordersize stuff if (comName.equals("bordersize")) { try { borderSize = Integer.parseInt(comValue); } catch (NumberFormatException e) { error("Error converting bordersize value in command file: " + comValue); borderSize = 1; } } if (comName.equals("url")) { // convert the url to a real URL object try { nextURL = new URL(comValue); } catch (MalformedURLException e) { nextURL = null; error("Malformed URL in command file: " + comValue); } } // adjust speed if (comName.equals("speed")) { try { speed = Integer.parseInt(comValue); } catch (NumberFormatException e) { error("Error converting speed value in command file: " + comValue); speed = 2; } } if (comName.equals("direction")) { if (comValue.equals("up")) { direction = UP; randomDirection = false; } if (comValue.equals("down")) { direction = DOWN; randomDirection = false; } if (comValue.equals("left")) { direction = LEFT; randomDirection = false; } if (comValue.equals("right")) { direction = RIGHT; randomDirection = false; } if (comValue.equals("upright")) { direction = UP_RIGHT; randomDirection = false; } if (comValue.equals("upleft")) { direction = UP_LEFT; randomDirection = false; } if (comValue.equals("downright")) { direction = DOWN_RIGHT; randomDirection = false; } if (comValue.equals("downleft")) { direction = DOWN_LEFT; randomDirection = false; } if (comValue.equals("flick")) { direction = FLICK; randomDirection = false; } if (comValue.equals("random")) { randomDirection = true; } } // the 'image' command provides the path // to an image to display if (comName.equals("image")) { // get the image nextImage = getImage(getCodeBase(), comValue); tracker.addImage(nextImage,0); needImage = false; // set up the initial image variables if (randomDirection) { direction = (int) ((Math.random()*100)%8)+1; } switch (direction) { case UP: { nextImageX = 0; nextImageY = appletHeight; break; } case DOWN: { nextImageX = 0; nextImageY = -appletHeight; break; } case LEFT: { nextImageX = appletWidth; nextImageY = 0; break; } case RIGHT: { nextImageX = -appletWidth; nextImageY = 0; break; } case UP_LEFT: { int min = (appletWidth0) nextImageY=0; animDone = (nextImageY==0); break; } case LEFT: { nextImageX -= speed; if (nextImageX<0) nextImageX=0; animDone = (nextImageX==0); break; } case RIGHT: { nextImageX += speed; if (nextImageX>0) nextImageX=0; animDone = (nextImageX==0); break; } case UP_LEFT: { nextImageX -= speed; nextImageY -= speed; if (nextImageX<0) nextImageX=0; if (nextImageY<0) nextImageY=0; animDone = (nextImageX==0) && (nextImageY==0); break; } case UP_RIGHT: { nextImageX += speed; nextImageY -= speed; if (nextImageX>0) nextImageX=0; if (nextImageY<0) nextImageY=0; animDone = (nextImageX==0) && (nextImageY==0); break; } case DOWN_LEFT: { nextImageX -= speed; nextImageY += speed; if (nextImageX<0) nextImageX=0; if (nextImageY>0) nextImageY=0; animDone = (nextImageX==0) && (nextImageY==0); break; } case DOWN_RIGHT: { nextImageX += speed; nextImageY += speed; if (nextImageX>0) nextImageX=0; if (nextImageY>0) nextImageY=0; animDone = (nextImageX==0) && (nextImageY==0); break; } case FLICK: { animDone = true; } } // finished moving? if (animDone) { // stop animating doAnim = false; // continue displaying our image currImage = nextImage; nextImage = null; // set up the new delay wait = delay*100; // get the next image needImage = true; } } // update out url status if (showURLStatus && updateURLStatus) { if (url != null) { showStatus(url.toString()); } updateURLStatus = false; } // do the painting repaint(); try { thread.sleep(10); } catch (InterruptedException e) { } } } public void error(String errorString) { System.out.println(errorString); showStatus(errorString); } private void getParameters() { // definition file name defFileName = getParameter("definition"); System.out.println("Using definition file: " + defFileName); } private Vector getFile(String fileName) { Vector data = null; String urlString; if (fileName != null) { data = new Vector(); urlString = getCodeBase() + defFileName; try { URL url = new URL(urlString); InputStream inStream = url.openStream(); DataInputStream dataStream = new DataInputStream(new BufferedInputStream(inStream)); String inLine = null; while ((inLine = dataStream.readLine()) != null) { inLine.trim(); // ignore comments (ie lines beginning with '#') // and ignore blank lines. if (inLine.length()>0 && inLine.charAt(0)!='#') { data.addElement(inLine); } } } catch (Exception e) { showStatus("Exception: " + e); data = null; } } return data; } }