advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

AnimNav


/*
AnimNav.java
Version 1.0.0
Written by Elijah Dean Meeker 1/4/96
Clicking it navagates to the given URL. It
uses off-screen buffering to avoid flicker. Here is a valid applet tags:
[Optional params are in brackets][Second brackets contain defaults]
<APPLET
codebase="classes"
CODE="AnimNav.class" WIDTH=75 HEIGHT=75>		    	SIZE of button images
[<PARAM NAME="spriteX" VALUE="10">]				    	LEFT pos. to draw sub-images[0]
[<PARAM NAME="spriteY" VALUE="10">]				    	TOP pos. to draw sub-images [0]
[<PARAM NAME="sleeptime" VALUE="500">]				 	TIME between images[autorun off]
<PARAM NAME="imageCount" VALUE="8">		 		 		Image Count
<PARAM NAME="translation" VALUE="0|0|1|1|2|2|3|3"> Which URL goes with which frame of animation
<PARAM NAME="URLcount" VALUE="4">		 			 	URL count
<PARAM NAME="dest0" VALUE="http://www.realtime.net/~elijah/">		URL to navigate to
<PARAM NAME="dest1" VALUE="http://www.mel.dit.csiro.au/~brendan/">URL to navigate to
<PARAM NAME="dest2" VALUE="http://198.3.117.222">						URL to navigate to
<PARAM NAME="dest3" VALUE="http://www.sun.com/">						URL to navigate to
[<PARAM NAME="desc0" VALUE="Homepage of Elijah Dean Meeker">]     URL description
[<PARAM NAME="desc1" VALUE="Homepage of Brendan Hills">]          URL description
[<PARAM NAME="desc2" VALUE="BreakFast Cereal Hall Of Fame">]      URL description
[<PARAM NAME="desc3" VALUE="Sun Microsystems">]                   URL description
[<PARAM NAME="target0" VALUE="_jmainview">]                       target window for URL[_parent]
[<PARAM NAME="background" VALUE="images/cub_bg.jpg"> ]BACKGROUND image(offset from codebase)
<PARAM NAME="prefix" VALUE="images/nav">		 		  Prefix of images(offset from codebase)
[<PARAM NAME="imagetype" VALUE="jpg">]               Image type-WITHOUT '.'[jpg]
</APPLET>


Please feel free to improve this code. It would not be here but for the
freely given help of others. I would love to see your improvements.
Elijah.

elijah@bga.com
http://www.realtime.net/~elijah/
*/
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.lang.InterruptedException;
import java.net.MalformedURLException;
import java.net.URL;
/******************************************************************************/
/**************************AnimNav.class***************************************/

public class AnimNav extends java.applet.Applet {
	//Graphics
	MediaTracker tracker;
	Dimension d;	//for sizing offscreen buffer
	Graphics offscreen;
	Image buf;
	Image bg;	//background image
	Image img[];//array of sprite images
	//AutoRun class
	NavAutoRun autoRun;

	int spriteX,spriteY;//sprite offset on background
	int imageCount;      //total number of sprite images

	int oldx = 0;      	//tested in mouseDrag
	int downx=0;         //tested in mouseUp
	public int frame = 0;     	//current frame of animation
	public int URLcount=0;

	boolean bgP=true;            //background image loaded flag
	boolean autorunP=false;      //autorun setup flag
	URL URLdest[];             //list of URLs
	String URLdescription[];   //URLdescriptions
	String targetWindow[];
	int translate[];           //Which URLs go with which frames of the animation

/****************************STATE CHANGES*************************************/
	public void init(){
	String str;
	String imageType;
		//offscreen buffer
		//System.out.println("offscreen buffer");
		d = size();
		buf= createImage(d.width,d.height);
		offscreen = buf.getGraphics();
		//MediaTracker
		//System.out.println("MediaTracker");
		tracker = new MediaTracker(this);
		/************************LOAD spriteX****************************/
		str =getParameter("spritex");
		if(str== null){
			spriteX=0;
		}else{
		 spriteX=Integer.parseInt(str);
		}
		/************************LOAD spriteY****************************/
		str =getParameter("spritey");
		if(str== null){
			spriteY=0;
		}else{
			spriteY=Integer.parseInt(str);
		}
		/*********************LOAD NavAutoRun init************************/
		str =getParameter("sleeptime");
		if(str!= null){
			autorunP=true;
			autoRun=new NavAutoRun(this,Integer.parseInt(str));
		}
			/************************LOAD urlcount**************************/
		//System.out.println("URLdest");
		str= getParameter("urlcount");
		if (str == null){
				System.out.println
				("Error Loading: urlcount, Not Optional");
		}else{
			URLcount= Integer.parseInt(str);
			URLdescription=new String[URLcount];
			targetWindow=new String[URLcount];
			URLdest=new URL[URLcount];

		}//ned if
		/************************LOAD URLs**************************/
		//System.out.println("URLs");
		for(int c=0;c<URLcount;c++){
			 URLdescription[c]= getParameter("dest"+c);

			try{
				if (URLdescription[c] != null) //this is here for development
				URLdest[c] = new URL(URLdescription[c]);
			}catch(MalformedURLException mal){
			System.out.println("Malformed URL: Check Applet Tag.");
			}
		}//end for
		/************************LOAD URL Descriptions*********************/

		for(int c=0;c<URLcount;c++){
			str= getParameter("desc"+c);
			if (str != null){
				URLdescription[c]=str;
			}
		}//end for
		/************************LOAD URL targetWindow*********************/

		for(int c=0;c<URLcount;c++){
			str= getParameter("target"+c);
			if (str == null){
				targetWindow[c]=" _parent";
			}else{
				targetWindow[c]=str;
			}
		}//end for
		/************************LOAD imageCount**************************/
		//System.out.println("imageCount");
		str =getParameter("imagecount");
		if (str == null){
				System.out.println
				("Error getting Parameter: imagecount, Not Optional.");
		}else{
			imageCount= Integer.parseInt(str);
			img = new Image[imageCount];
		}
		/************************LOAD translate[]*************************/
		str = getParameter("translation");
		if (str == null){
				System.out.println
				("Error getting Parameter: translation, Not Optional.");
		}else{
			translate=new int[imageCount];
			int index=0;
			for (int i = 0; i < str.length(); ) {
				if (index >= imageCount) break;
				int next=str.indexOf('|', i);
				if (next == -1) next = str.length();
				if (i != next) {
					translate[index]=Integer.parseInt(str.substring(i, next));
					index++;
				}//end if
				i = next + 1;
			}//end for
	  }//end if str==null
	  /*
		  //this would also work for above
		  StringTokenizer st = new StringTokenizer(s,"|");
		  while (st.hasMoreTokens()) {
					 translate[index]=Integer.parseInt(st.nextToken());
					 index++;
		  }

	  */
		/************************LOAD imagetype**************************/
		//System.out.println("imagetype");
		str = getParameter("imagetype");
					 if (str == null){
						imageType="jpg";
					 }else{
						imageType=str;
					 }//end if

		/************************LOAD background image**************************/
		//System.out.println("background");
		str = getParameter("background");
					 if (str == null){
						bgP=false; //not using background image
						spriteX=0;spriteY=0;
					 }else{
						showStatus("Loading Background Image");
						bg =  getImage(getCodeBase(),str);
						tracker.addImage(bg, 0);
						try {
						tracker.waitForAll();
						} catch (InterruptedException e) {
						System.out.println("Error waiting for Background image to load.");
						}//end catch
						showStatus("");
					 }//end if

		/************************LOAD animation images*********************/
		//System.out.println("Sprites");
		str = getParameter("prefix");

		if (str == null){
				System.out.println
				("Error Loading image: "+str+", Not Optional");

		}else{
			for (int i = 0; i < imageCount; i++) {
						showStatus("AnimNav Loading Image :"+(i+1)+ " of " + imageCount);
						img[i] =  getImage(getCodeBase(),str+i+"."+imageType);
						tracker.addImage(img[i], 1);
						try {
						tracker.waitForAll();
						} catch (InterruptedException e) {
						System.out.println("Error waiting for image"+i+" to load");
						}//end catch
						showStatus("");
			}//end for
	  }//end if str == null
	}//end init
/******************************************************************************/

  public void start(){
	if(autorunP==true)autoRun.start();
	frame=downx=oldx=0;
	repaint();
  }//end start
/******************************************************************************/

  public void stop(){
		if(autorunP==true)autoRun.stop();

  }//end stop
/******************************************************************************/

  public void destroy(){
  }//end destroy
/****************************END STATE CHANGES********************************/
/*******************************EVENTS****************************************/

  public boolean mouseDown(Event e, int x, int y){
	downx=oldx=x;

	return(true);
  }//end mouseDown
/******************************************************************************/

  public boolean mouseUp(Event e, int x, int y){
	if(downx<x+2 && downx>x-2)//set in mouseDown
				getAppletContext().showDocument(URLdest[translate[frame]],targetWindow[translate[frame]]);

	return(true);
  }//end mouseUp
/******************************************************************************/

  public boolean mouseEnter(Event e, int x, int y){

	if(autorunP==true)autoRun.stop();
	showStatus(URLdescription[translate[frame]]);

	return(true);
  }//end mouseEnter
/******************************************************************************/

  public boolean mouseExit(Event e, int x, int y){
	if(autorunP==true)autoRun.start();
	showStatus("");

	return(true);
  }//end mouseExit
/******************************************************************************/

  public boolean mouseDrag(Event e,int x, int y){

  if(x>=(oldx + 20)){
	oldx=x;
	frame--;
	if (frame<0){frame=imageCount-1;}
  }else if(x<=(oldx - 20)){
	oldx=x;
	frame++;
	if (frame>=imageCount){frame=0;}
  }
  showStatus(URLdescription[translate[frame]]);
  repaint();

  return(true);
  }//end mouseMove
/*******************************END EVENTS*************************************/
/*******************************METHODS****************************************/

	public  void update(Graphics g){
		paint(g);
	}//end update
/******************************************************************************/

	public void paint(Graphics g){
		if (offscreen != null) {
				paintApplet(offscreen);
				g.drawImage(buf, 0, 0, this);
		} else {
				 paintApplet(g);
		}
	}//end paint
/******************************************************************************/

	 public void paintApplet(Graphics g) {
		if(bgP==true)
		g.drawImage(bg,0,0,null);

		//System.out.println("paintApplet: frame="+frame);
		g.drawImage(img[frame],spriteX,spriteY,null);

	 }//end paintApplet
/******************************************************************************/
	// Applet info
	public String getAppletInfo(){
		return "AnimNav.class By Elijah Meeker 1/5/96";
	}//end getAppletInfo
/******************************************************************************/
	public String[][] getParameterInfo() {
		  String[][] info = {
				{"[spriteX]",
				 "int",
				 "X offset of images on background [0]"},
				{"[spriteY]",
				 "int",
				 "Y offset of images on background[0]"},
				{"[sleeptime]",
				 "int",
				 "pause between images in AutoRun[AutoRun off]"},
				{"imageCount",
				 "int",
				 "Number of animation images"},
				{"translation",
				 "parsed string",
				 "which URLs go with which images,form:0|0|1|1"},
				{"URLcount",
				 "int",
				 "number of URLs"},
				{"dest+int",
				 "url",
				 "URL to navigate to(int min=0, max=URLcount-1"},
				{"[desc+int]",
				 "string",
				 "URL description[string of dest+int]"},
				 {"[target+int]",
				 "string",
				 "target Window for URL[_parent]"},
				{"[imagetype]",
				 "string",
				 "image type suffix W/O '.'[jpg]"},
				{"[background]",
				 "string",
				 "Background image(offset from CodeBase)[no background, spriteX=spriteY=0]"},
				{"prefix",
				 "string",
				 "Prefix to sprites(offset from CodeBase)"}

		 };
		  return info;
	}//end getParameterInfo
/*****************************END METHODS**************************************/

}//end class AnimNav.class
/******************************************************************************/
/******************************************************************************/
/*****************************AutoRun.class************************************/

class NavAutoRun implements Runnable {
	Thread T = null;
	AnimNav applet;
	int howLong;
/*****************************Constructors*************************************/

	NavAutoRun(AnimNav applet,int time){
		this.applet = applet;
		howLong=time;
	}//end constructor
/****************************State Changes*************************************/

	public void start(){
		if (T == null) {
				T = new Thread(this,"T");
				T.start();
				T.setPriority(Thread.MIN_PRIORITY+1);
		}
	}//end start
/******************************************************************************/

	public void run(){
		while(T!=null){
			applet.repaint();
			applet.frame++;
			if (applet.frame>=applet.imageCount){applet.frame=0;}
				applet.showStatus(applet.URLdescription[applet.translate[applet.frame]]);
				try {Thread.sleep(howLong);} catch (InterruptedException e){}
		}//end while
  }//end run
/******************************************************************************/

  public void stop(){
	if (T != null) {
		T.stop();
		T=null;
	}
  }//end stop
/******************************************************************************/
}//end AutoRun.class


How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio

Use Explicit Conversion Functions to Avert Reckless Implicit Conversions
Polyglot Programming: Building Solutions by Composing Languages
Automated testing for .NET by Ben Hall
"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES