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


Partners & Affiliates











advertisement

CoolScroll


import java.applet.*;
import java.awt.*;
import java.awt.image.PixelGrabber;
import java.awt.image.MemoryImageSource;


public class CoolScroll extends Applet implements Runnable
{
	private Thread	 m_CoolScroll = null;
	
	boolean asodreadisegna = false;

	private double rad = 20.0,
				   rad_inc = 0.5,
				   RAD =  3.1415926535 / 180,
				   ang_x = 0.0,
				   angolo_x = 0.0,
				   inc_factor = 1.5;
	

	int asciioffset = 0;
	int veloce;
	char msg[];
	int msgbounceindex [];
	int lametable[];

	int xadd = 0;
	int scrollpos = 0;
	int mirrorpic[],
		swappic[];

	int startx = 0,
		starty = 0,
		msglenght;

	Color forecolor,
		  backcolor;	

	Image backdrop;
	Image appletimage;
	Image cropfont,
		  upperimage,
		  lowerimage;

	int redmaskadd = 90,
		greenmaskadd = 120,
		bluemaskadd = 140;

	int redadd = 5,
		greenadd = 5,
		blueadd = 5;

	Image letter;

	int extent_x,
		extent_y;

	int letterwidth = 0,
		letterheight = 0;

	private MemoryImageSource screenMem;

	Graphics gfx,
			 lettergfx,
			 appletgfx;

	public void init()
	{
		String messaggio;
		messaggio = getParameter("scrolltext");

		if(messaggio == null)
		{
			messaggio="SAY SOMETHING TO THE WORLD...WRITE IT!";
		}
		veloce = Integer.parseInt(getParameter("scrollspeed"));
		asciioffset = Integer.parseInt(getParameter("asciioffset"));
		//Single letter build
		letterwidth = Integer.parseInt(getParameter("fontx"));
		letterheight = Integer.parseInt(getParameter("fonty"));
		letter = createImage(letterwidth , letterheight );
		lettergfx = letter.getGraphics();
		//Single letter build
		
		appletimage = createImage(size().width , size().height);
		appletgfx = appletimage.getGraphics();

		//Half scroll build
		extent_x = size().width / 2;
		extent_y = size().height / 4;
		upperimage = createImage( extent_x , extent_y );
		gfx = upperimage.getGraphics();
		scrollpos = extent_x;
		swappic = new int[extent_x * extent_y + extent_x];
		mirrorpic = new int[extent_x * extent_y + extent_x];

		//Half scroll build

		//ScrollText build
		msglenght = messaggio.length();	
		msg = new char[msglenght+1];
		//Get ready to bounce...
		msgbounceindex = new int[msglenght+1];
		lametable = new int[16];
		loadlameBounce();
		int nowind = 0;
		for (int loop=0; loop < msglenght; loop++)
		{
			msgbounceindex[loop] = nowind;
			if(++nowind>15)
				nowind = 0;
		}


		

		msg = messaggio.toCharArray();
		//ScrollText build

		MediaTracker tracker = new MediaTracker(this);

		cropfont = getImage(getDocumentBase(), getParameter("fontimage"));
		tracker.addImage(cropfont, 0);
		try 
		{
			tracker.waitForID(0);
		}
		catch (InterruptedException e)
		{

		}
		
		MediaTracker tracker2 = new MediaTracker(this);

		backdrop = getImage(getDocumentBase(), getParameter("backimage"));
		tracker2.addImage(backdrop, 1);
		try 
		{
			tracker2.waitForID(1);
		}
		catch (InterruptedException e)
		{

		}

		forecolor = new Color(255,0,0);
		backcolor = new Color(0,0,0);

		screenMem = new MemoryImageSource(extent_x, 
				  						  extent_y, 
										  mirrorpic, 
										  0, 
										  extent_x);


	}

	public void update(Graphics g)
	{
		gfx.drawImage(backdrop,0,0,this);
		BounceScroll();
		loadPix();
		renderPix();
		appletgfx.drawImage(upperimage,0,0, extent_x * 2, extent_y * 2, this);
		appletgfx.drawImage(lowerimage,0,extent_y*2, extent_x * 2, (extent_y * 2)+xadd, this);
		paint(g);
	}


	public void paint(Graphics g)
	{
		if (appletgfx  != null)
		{
			g.drawImage(appletimage ,0,0, this);
			
		}
	}

	public void start()
	{
		if (m_CoolScroll == null)
		{
			m_CoolScroll = new Thread(this);
			m_CoolScroll.start();
		}
	}
	
	public void stop()
	{
		if (m_CoolScroll != null)
		{
			m_CoolScroll.stop();
			m_CoolScroll = null;
		}
	}

	public void run()
	{
		while (true)
		{
			try
			{
				repaint();
				Thread.sleep(50);
				System.gc();
			}
			catch (InterruptedException e)
			{
				stop();
			}
		}
	}


	private void loadPix()
	{
		int punto = 0;
		int themask = 0;
		asodreadisegna = true;
		
		PixelGrabber grabber = new PixelGrabber( upperimage, 
												 0, 
												 0, 
												 extent_x, 
												 extent_y, 
												 swappic , 
												 0, 
												 extent_x);
		boolean done = false;
		do
		{
			try 
			{ 
				done = grabber.grabPixels( 500 );  
			} 
			catch ( InterruptedException e ) {}
		}
		while( !done );


		asodreadisegna = false;

	}

	private void renderPix()
	{
		int punto = 0;
		int themask = 0;
		int red = 0;
		int green = 0;
		int blue = 0;
		int yindex = extent_y-1;

		redmaskadd += redadd;
		greenmaskadd += greenadd;
		bluemaskadd += blueadd;
		
		if(redmaskadd == 255 || redmaskadd == 90 )
			redadd = redadd * -1;

		if(greenmaskadd == 255 || greenmaskadd == 90 )
			greenadd = greenadd * -1;

		if(bluemaskadd == 255 || bluemaskadd == 90 )
			blueadd = blueadd * -1;

		for(int loopy = 0; loopy < extent_y; loopy++)
		{
			for(int loopx = 0; loopx < extent_x; loopx++)
			{
				punto = swappic[extent_x * yindex + loopx];

				themask = punto & 0x00ff0000;
				red = themask >> 16;

				themask = punto & 0x0000ff00;
				green = themask >> 8;

				themask = punto & 0x000000ff;
				blue = themask;
				
				//red = blue;
				//green = blue;
				red = red;
				green = green;

				red = (int)((red * redmaskadd)<<8);
				green = (int)((green * greenmaskadd));
				blue = (int)((blue * bluemaskadd)>>8);

				

				
				mirrorpic[extent_x * loopy + loopx] = 0xff000000 |(red & 0x00ff0000) | (green & 0x0000ff00) | (blue & 0xff);

			}
			yindex--;
		}

		lowerimage = createImage(screenMem);
	}

	// Lame Pre-Calc bounce table....
	private void loadlameBounce()
	{
		lametable[0] = 0;
		lametable[1] = 0;
		lametable[2] = 0;
		lametable[3] = 1;
		lametable[4] = 1;
		lametable[5] = 2;
		lametable[6] = 4;
		lametable[7] = 6;
		lametable[8] = 10;
		lametable[9] = 15;
		lametable[10] = 10;
		lametable[11] = 6;
		lametable[12] = 4;
		lametable[13] = 2;
		lametable[14] = 1;
		lametable[15] = 1;
	}

	private void BounceScroll()
	{
		int retval = 0;
		double CX;
		

		for (int loopx = 0; loopx < msglenght; loopx++)
		{
			if( scrollpos + (loopx * letterwidth ) < extent_x && scrollpos + (loopx * letterwidth ) > letterwidth * -1)
			{
				getletterIndex((int)msg[loopx]);
				
				//Simulating transparent color....
				lettergfx.drawImage(backdrop, -(scrollpos + (loopx * letterwidth)), -(1+lametable [msgbounceindex[loopx]]),this);
				lettergfx.drawImage(cropfont, -(startx * letterwidth), -(starty * letterheight),this);
				gfx.drawImage(letter,scrollpos + (loopx * letterwidth),1+lametable [msgbounceindex[loopx]],this);

				lettergfx.fillRect(0,0,letterwidth, letterheight);
			}				
			if(++msgbounceindex[loopx] > 15)
				msgbounceindex[loopx] = 0;
		}

		scrollpos -= veloce;
		if(scrollpos * -1 > msglenght * letterwidth)
			scrollpos = extent_x;

		//sinus stuff
		angolo_x += inc_factor;
		if (angolo_x > 90 || angolo_x < 0)				
		{
			inc_factor = inc_factor * -1;
		}
		CX = Math.cos((angolo_x ) * RAD);
		xadd  = (int)(angolo_x * CX);
		//end of sinus stuff
	}

	private void getletterIndex(int ASCIIcode)
	{
		int retval = 0;
		retval = ((int)ASCIIcode)-asciioffset;
		if(retval<=0)
		{
			startx = 0;
			starty = 0;
		}
		else
		{
			starty = ((int)(retval / 10));
			startx = (retval%10);
		}

	}
}

Back to the CoolScroll applet page.

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs