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.

 DevX Skillbuilding from IBM developerWorks
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 48
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%.

Apple Details iPhone/Mac Developer Event
RIM Ups Ante With Mobile Software Push
Novell Readies Silverlight Clone for Linux
Yahoo Pitches The 'Next Generation of Search'
Alfresco's Latest ECM: Prying Open a Sector?
SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads for the JON 2.0
Out with the Old, in with the New at JavaOne

Create Secure Java Applications Productively, Part 1: Use Rational Application Developer and Data Studio
.NET Building Blocks: Custom User Control Fundamentals
Secure Internet File-Sharing with PHP, MySQL, and JavaScript
Getting Started with TBB on Windows
Moving to VoIP: Should You Go It Alone?
Introduction to the WPF Command Framework
7.0, Microsoft's Lucky Version?
Will Hyper-V Make VMware This Decade's Netscape?
Eliminate Fragmentation Frustration with Netbiscuits
Taming Trees: Building Branching Structures

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
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES