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


Partners & Affiliates











advertisement

NewsScroller: WebBase.java


package crackers.Scroller;

/**
* A super-class for animated applets. This class contains many standard methods
* and parameters * for providing simple animation using double-buffering (where
* the image is drawn "off-screen" and then drawn all at one time to the
* receiving Graphics object.
* <P>
* This program is copyrighted 1999 by the author under the GNU Public License
* (GPL). Full text of this license is available at
* <A HREF="http://www.gnu.org">GNU.org</A>.
* <P>
* For full usage information, see
* <A HREF="http://www.io.com/~crackers">my web pages</A>.
*
* @author E. A. Graham, Jr. (crackers@io.com)
* @version 2.0
*/

import java.awt.*;
import java.applet.*;
import java.net.URL;
import java.net.MalformedURLException;

public abstract class WebBase extends Applet implements Runnable
{
	/**
	* Default sleep time (milliseconds).
	*/
	private final int DEFAULT_SLEEP = 150;
	/**
	* Default scroll increment (pixels).
	*/
	private final int DEFAULT_SCROLL = 10;
	/**
	* The animation thread.
	*/
	protected Thread  myThread = null;
	/**
	* Maximum width of the graphics image.
	*/
	protected int maxWidth;
	/**
	* Maximum height of the graphics image.
	*/
	protected int maxHeight;
	/**
	* Width of the border on the image.
	*/
	protected int borderWidth;
	/**
	* The increment to advance the animation (pixels).
	*/
	protected int scrollUnit;
	/**
	* The amount of time (ms) to wait between updates.
	* Can be considered to be the "frame-rate" of the animation.
	*/
	protected long sleepTime;
	/**
	* The off-screen image to draw to.
	*/
	protected Image appImage;
	/**
	* The graphics object for the off-screen image.
	*/
	protected Graphics appGC;
	/**
	* The foreground color to use while drawing the image.
	*/
	protected Color foreColor;
	/**
	* The background color to use while drawing the image.
	*/
	protected Color backColor;
	/**
	* An internal flag to indicate whether or not the
	* animation thread is running.
	*/
	protected boolean loaded = false;
	/**
	* The image object for the background graphics, if used.
	*/
	protected Image backgroundImage = null;
	/**
	* An HTML frame "target" parameter, if used.
	*/
	protected String  frameTarget = null;

	/**
	* Applet initialization.
	*/
	public void init()
	{
		// get the size of the applet
		Dimension d = getSize();
		maxWidth = d.width;
		maxHeight = d.height;

		// used for children of this class to target URL jumps
		//	  defaults to "_top"
		frameTarget = readParam("TARGET","_top");

		// get some numeric parameters
		sleepTime = readLong("SleepTime",DEFAULT_SLEEP);
		scrollUnit = readInteger("ScrollBy",DEFAULT_SCROLL);
		borderWidth = readInteger("BORDER",0);

		// the color parameters
		foreColor = readColor("FOREGROUND",Color.black);
		backColor = readColor("BACKGROUND",Color.white);
		setBackground(backColor);

		// a background picture (go ahead and wait for full load)
		String tparam = readParam("PICTURE",null);
		if (tparam != null)
		{
			MediaTracker imgTrack;
			URL theURL = null;
			try
			{
				// get the image url
				if (tparam.indexOf("http://") >= 0 )
					theURL = new URL(tparam);
				else
					theURL = new URL(getDocumentBase(),tparam);

				// load the image
				backgroundImage = getImage(theURL);
				if (backgroundImage != null)
				{
					imgTrack = new MediaTracker(this);
					imgTrack.addImage(backgroundImage,0);
					imgTrack.waitForAll();
					if (imgTrack.isErrorAny())
						backgroundImage = null;
				}
			}
			catch (Exception e)
			{
				backgroundImage = null;
			}
		}
		appImage = createImage(maxWidth, maxHeight);
		appGC = appImage.getGraphics();
	}

	/**
	* Read an HTML-supplied parameter.
	* @param param the HTML PARAM tag to find
	* @param defaultValue the default value to return if the parameter is not
	* found.
	* @return the String of the parameter or the defaultValue on error
	*/
	protected String readParam(String param,String defaultValue)
	{
		String rtn = null;
		try
		{
			rtn = getParameter(param);
		}
		catch (Exception e) { }
		if (rtn == null) rtn = defaultValue;
		return rtn;
	}

	/**
	* Read an HTML-supplied parameter.
	* @param param the HTML PARAM tag to find
	* @return the String of the parameter, or null on error
	*/
	protected String readParam(String param)
	{
		return readParam(param,null);
	}

	/**
	* Read an HTML-supplied parameter as an integer.
	* @param param the HTML PARAM tag to find
	* @param defaultValue the default value
	* @return the parameter or the default value if no parameter
	*/
	protected int readInteger(String param, int defaultValue)
	{
		try
		{
			String s = readParam(param);
			int x = Integer.parseInt(s);
			return x;
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}


	/**
	* Read an HTML-supplied parameter as a long.
	* @param param the HTML PARAM tag to find
	* @param defaultValue the default value
	* @return the parameter or the default value if no parameter
	*/
	protected long readLong(String param, long defaultValue)
	{
		try
		{
			String s = readParam(param);
			long x = Long.parseLong(s);
			return x;
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	* Reads a color HTML-supplied parameter (either R,G,B or hex RGB value).
	* @param param name of the parameter to read
	* @param defColor default color to use if no parameter present or is
	* an invalid format
	* @return a Color corresponding to the parameter string.
	* @see readParam
	*/
	protected Color readColor(String sParam, Color defColor)
	{
		String tparam = readParam(sParam);
		Color tcolor;
		int rc,gc,bc;

		// set the component colors to invalid values
		rc = gc = bc = -1;

		// a parameter was used...
		if (tparam != null)
		{
			// looks like it might be "HTML" format
			if (tparam.startsWith("#"))
			{
				// if it's the right length, try to separate it properly
				if (tparam.length()==7)
				{
					try
					{
						rc = Integer.parseInt(tparam.substring(1,3),16);
						gc = Integer.parseInt(tparam.substring(3,5),16);
						bc = Integer.parseInt(tparam.substring(5),16);
					}
					catch (Exception e) { }
				}
			}

			// not "HTML" format: try to separate it as CSV
			else
			{
				int x,y;
				x = tparam.indexOf(',');
				y = tparam.lastIndexOf(',');
				if (x>0 && y>0 && x!=y)
				{
					try
					{
						rc = Integer.parseInt(tparam.substring(0,x));
						gc = Integer.parseInt(tparam.substring(x+1,y));
						bc = Integer.parseInt(tparam.substring(y+1));
					}
					catch (Exception e) { }
				}
			}
		}

		// try to make a new color with the three RGB values, or get the
		// default
		tcolor = (rc>=0 && rc<=255 && gc>=0 && gc<=255 && bc>=0 && bc<=255) ? new Color(rc,gc,bc) : defColor;
		return (tcolor);
	}

	/**
	* Start the applet thread. Initialize the animation thread.
	*/
	public void start()
	{
		if (myThread == null)
		{
			myThread = new Thread(this);
			myThread.start();
		}
		loaded = true;
	}

	/**
	* Stop the applet thread. If the animation thread is alive, kill it.
	*/
	public void stop()
	{
		if ((myThread != null) && myThread.isAlive())
			myThread.stop();
		loaded = false;
		myThread = null;
	}

	/**
	* The main thread method.
	* @see java.lang.Runnable
	*/
	public void run()
	{
		// get which is the current thread and set a low priority on it
		Thread me = Thread.currentThread();
		me.setPriority(Thread.MIN_PRIORITY);

		// craete a blank image and it's Graphic object
		appImage = createImage(maxWidth, maxHeight);
		appGC = appImage.getGraphics();

		// force an early paint, even though we haven't actually drawn anything
		// to paint, yet
		repaint();

		// while we have a hold of the animation thread...
		while (myThread == me)
		{
			// just to make sure the applet is running
			if (loaded)
			{
				// do our thing, repaint, and go to sleep
				doAppThing();
				repaint();
				try
				{
					Thread.sleep(sleepTime);
				}
				catch(InterruptedException e){}
			}
		}
	}

	/**
	* Sub-classes must implement this method. This method is where the
	* sub-classes will update the image to be painted.
	* @see update
	*/
	protected abstract void doAppThing();


	/**
	* Over-ride of the superclass method. Because the acutal painting takes
	* place in the update method, we don't want any painting to occur here.
	*/
	public void paint(Graphics g)
	{ }

	/**
	* Over-ride of the superclass method. Draws all of the current elements
	* onto the visible Graphics object of the applet. Sub-classes will also
	* over-ride this method to supply their own implementations.
	* @param g the Graphics object to update
	*/
	public synchronized void update(Graphics g)
	{
		// the animation is running
		if (loaded)
		{
			// if there's a background picture, use it
			if (backgroundImage != null)
				appGC.drawImage(backgroundImage,0, 0, maxWidth, maxHeight,this);

			// otherwise, draw background rectangle...
			else
			{
				appGC.setColor(backColor);
				appGC.fillRect(0, 0, maxWidth, maxHeight);
			}
		}
	}

	/**
	* Draws a 3-D border on the applet image Graphics object.
	* Uses the width, height, and BORDER parameter to determine the wheres and
	* whats of how to draw the border.
	*/
	protected void drawBorder()
	{
		if (borderWidth == 0) return;

		Color bc_light,bc_dark;
		int pWidth = Math.abs(borderWidth);
		Polygon bp_below = new Polygon();
		Polygon bp_above = new Polygon();

		// if the background is white, we can't make the border lighter, so we
		// come up with two darker colors
		if (backColor.equals(Color.white))
		{
			bc_light = backColor.darker();
			bc_dark = bc_light.darker();
		}
		else if (backColor.equals(Color.black))
		{
			bc_light = Color.gray;
			bc_dark = Color.gray.darker();
		}
		else
		{
			bc_light = backColor.brighter();
			bc_dark = backColor.darker();
		}

		// add the vertices to the polygons
		bp_above.addPoint(0,0);
		bp_above.addPoint(maxWidth,0);
		bp_above.addPoint(maxWidth-pWidth,pWidth);
		bp_above.addPoint(pWidth,pWidth);
		bp_above.addPoint(pWidth,maxHeight-pWidth);
		bp_above.addPoint(0,maxHeight);

		bp_below.addPoint(maxWidth,0);
		bp_below.addPoint(maxWidth,maxHeight);
		bp_below.addPoint(0,maxHeight);
		bp_below.addPoint(pWidth,maxHeight-pWidth);
		bp_below.addPoint(maxWidth-pWidth,maxHeight-pWidth);
		bp_below.addPoint(maxWidth-pWidth,pWidth);

		// draw the polygons with appropriate colors
		appGC.setColor(borderWidth < 0 ? bc_dark : bc_light);
		appGC.fillPolygon(bp_above);
		appGC.setColor(borderWidth < 0 ? bc_light : bc_dark);
		appGC.fillPolygon(bp_below);
	}
}


Index

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%.

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
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'

"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
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization

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
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