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


Partners & Affiliates











advertisement

fireworks


/* ---------------------------------------------------------------------------------
 * Fireworks applet, Copyright (c) 1995 Erik Wistrand, All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for NON-COMMERCIAL purposes and without fee is hereby
 * granted provided that this copyright notice and appropiate documention
 * appears in all copies.
 *
 * ERIK WISTRAND MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ERIK WISTRAND SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 * ---------------------------------------------------------------------------------
 * fireworks applet v2.0f (beta API)
 *
 * Supported actions
 *  - mouseDown()
 *      applet is restarted
 *
 * Supported tags</h3>
 *  - ROCKETS n
 *      Number of rockets to animate (default 3)
 *  - POINTS n
 *      Number of points in each rocket (default 3)
 *  - POINTSIZE n
 *      Pixelsize of points (default 2)
 *  - LIFELENGTH n
 *      Duration of rockets, counted in number of frames (default 100)
 *  - GRAV n
 *      Gravity. Large value => rockets fall down fast (default 10)
 *  - DELAY n
 *      Delay in milliseconds between frames (default 50)
 *      (Warning: delay=0 consumes a lot of browser cpu time)
 *  - TRAIL n
 *      Number of trailing points (default 10)
 *  - COLOR rrggbb
 *      RGB hex triple for applet background (default browser background)
 *  - WIDTH n
 *      java.applet.Applet size().width in pixels (default tiny = browser default?)
 *  - HEIGHT n
 *      java.applet.Applet size().height in pixels (default tiny = browser default?)
 *
 *
 * Comments or problems with the fireworks applet?
 *   Contact me at:
 *
 *   wistrand@cs.chalmers.se
 *   URL: http://www.cs.chalmers.se/~wistrand
 *
 */

import java.awt.Graphics;
import java.awt.Color;

class Bullet {
  double xx;
  double yy;
  double x[] = null;
  double y[] = null;
  double dx;
  double dy;
  double g;
  double f         = 1.0f;
  int    lastx     = 0;
  int    lasty     = 0;
  int    dotsize;
  int    _trail    = 10;
  int    _life_len = 100;
  int    t         = 0;
  int    t2        = 0;
  int    count     = 0;
  Color  col       = Color.black;
  Color  remcol    = Color.white;

  double xmax;
  double ymax;

  Bullet(int x0,
	 int y0,
	 double dx0,
	 double dy0,
	 double g0,
	 int size,
	 int w,
	 int h,
	 int trail,
	 int life_len,
	 Color fg,
	 Color bg)
    {
      col    = fg;
      remcol = bg;

      _trail  = trail;
      _life_len    = life_len;

      t  = 0;
      t2 = 1;

      x = new double [_trail];
      y = new double [_trail];

      int i;

      for(i = 0; i < _trail; i++)
	{
	  x[i] = -dotsize * 2.0f;
	  y[i] = -dotsize * 2.0f;
	}

      xx      = x0;
      yy      = y0;
      dx      = dx0;
      dy      = dy0;
      g       = g0;
      dotsize = size;


      xmax = w;
      ymax = h;
    }

  void move()
    {
      xx += dx;
      yy += dy;

      dx *= f;
      dy *= f;
      dy += g;
    }

  boolean draw(Graphics g)
    {
      if(count++ < _life_len)
	{
	  x[t] = xx;
	  y[t] = yy;

	  int sx = (int)xx;
	  int sy = (int)yy;
	  g.setColor(col);

	  g.fillRect(sx, sy, dotsize, dotsize);

	  t++;
	  if(t >= _trail) t = 0;

	  return ((sx >= 0)   && (sy >= 0) &&
		  (sx < xmax) && (sy < ymax));
	}

      return (t2 != t);
    }

  void undraw(Graphics g)
    {
      int sx = (int)x[t2];
      int sy = (int)y[t2];

      t2++;
      if(t2 >= _trail) t2 = 0;

      g.setColor(remcol);
      g.fillRect(sx, sy, dotsize, dotsize);
    }

  void clearall(Graphics g)
    {
      int i = 0;

      while(i < _trail)
	{
	  undraw(g);
	  i++;
	}
    }
}

class Rocket {
  Bullet bullets[];

  int    _nof_bullets;
  int    _xmax;
  int    _ymax;
  int    _size;
  double _g;
  int    _trail;
  int    _life_len;
  Color  _fg;
  Color  _bg;

  Rocket(int    nof_points,
	 int    x0,
	 int    y0,
	 double dx0,
	 double dy0,
	 double g,
	 int    dotsize,
	 int    w,
	 int    h,
	 int    trail,
	 int    life_len,
	 Color  fg,
	 Color  bg)
    {
      _fg = fg;
      _bg = bg;
      _nof_bullets = nof_points;
      _g           = g;
      _xmax        = w;
      _ymax        = h;
      _size        = dotsize;
      _trail       = trail;
      _life_len    = life_len;
      bullets = new Bullet[_nof_bullets];

      reinit(x0, y0, dx0, dy0);
    }

  void reinit(int    x0,
	      int    y0,
	      double dx0,
	      double dy0)
    {
      int i;

      for(i = 0; i < _nof_bullets; i++)
	{
	  double a = Math.random() * 2.0f * 3.1415f;
	  double r = Math.random() * 1.0f;

	  double dx = Math.cos(a) * r;
	  double dy = Math.sin(a) * r;

	  bullets[i] = new Bullet(x0, y0,
				  dx0 + dx,
				  dy0 + dy,
				  _g,
				  _size,
				  _xmax, _ymax,
				  _trail,
				  _life_len,
				  _fg, _bg);
	}
    }

  void draw(Graphics g)
    {
      int i;
      boolean inside = false;
      for(i = 0; i < _nof_bullets; i++)
	{
	  inside = bullets[i].draw(g) || inside;
	  bullets[i].move();
	}

      if(!inside)
	{
	  int x0 = 10 + (int)(Math.random() * (double)(_xmax - 20));
	  int y0 = _ymax - (int)(Math.random() * 20.0f);

	  double dx0 = (Math.random() - 0.5f) * 2.0f;
	  double dy0 = -(Math.random() * 6);

	  reinit(x0, y0, dx0, dy0);
	}
    }

  void update(Graphics g)
    {
      int i;
      boolean inside = false;

      for(i = 0; i < _nof_bullets; i++)
	{
	  bullets[i].undraw(g);
	  bullets[i].move();
	  inside = bullets[i].draw(g) || inside;
	}
      if(!inside)
	{
	  int x0 = 10 + (int)(Math.random() * (double)(_xmax - 20));
	  int y0 = _ymax - (int)(Math.random() * 20.0f);

	  double dx0 = (Math.random() - 0.5f) * 2.0f;
	  double dy0 = -(Math.random() * 6);

	  for(i = 0; i < _nof_bullets; i++)
	    {
	      bullets[i].clearall(g);
	    }

	  reinit(x0, y0, dx0, dy0);
	}
    }
}

public class fireworks extends java.applet.Applet implements Runnable {
  int nof_rockets = 3;
  int delay       = 50;
  int nof_points  = 20;
  int trail       = 10;
  int dotsize     = 2;
  int life_len    = 100;
  double g        = 0.0f;

  Thread kicker    = null;
  Rocket rockets[] = null;
  int clear        = 0;
  boolean first    = true;

  Color bg         = Color.black;

  Color parseCol(String s)
    {
      if(s.length() >= 6)
	{
	  String sr = s.substring(0, 2);
	  String sg = s.substring(2, 4);
	  String sb = s.substring(4, 6);
	  int r, g, b;

	  try {
	    r = Integer.parseInt(sr, 16);
	  } catch(Exception e) {
	    r = 0;
	  }
	  try {
	    g = Integer.parseInt(sg, 16);
	  } catch(Exception e) {
	    g = 0;
	  }
	  try {
	    b = Integer.parseInt(sb, 16);
	  } catch(Exception e) {
	    b = 0;
	  }
	  return new java.awt.Color(r, g, b);
	}
      return new java.awt.Color(128, 128, 128);
    }

  public void init() {
    int i;

    try {
      nof_rockets = Integer.parseInt(getParameter("ROCKETS"));
    } catch(Exception e) {
      nof_rockets = 3;
    }
    try {
      delay = Integer.parseInt(getParameter("DELAY"));
    } catch(Exception e) {
      delay = 50;
    }
    try {
      trail = Integer.parseInt(getParameter("TRAIL"));
    } catch(Exception e) {
      trail = 10;
    }
    try {
      life_len = Integer.parseInt(getParameter("LIFELENGTH"));
    } catch(Exception e) {
      life_len = 100;
    }
    try {
      nof_points = Integer.parseInt(getParameter("POINTS"));
    } catch(Exception e) {
      nof_points = 3;
    }
    try {
      dotsize = Integer.parseInt(getParameter("POINTSIZE"));
    } catch(Exception e) {
      dotsize = 2;
    }
    try {
      g = (double)Integer.parseInt(getParameter("GRAV")) / 1000.0f;
    } catch(Exception e) {
      g = .01f;
    }
    String bgcol = getParameter("COLOR");

    if(bgcol != null)
      {
	bg = parseCol(bgcol);
      }



    rockets = new Rocket[nof_rockets];

    initrockets();

  }

  void initrockets()
    {
      int i;
      for(i = 0; i < nof_rockets; i++)
	{
	int x = 10 + (int)(Math.random() * (double)(size().width - 20));
	int y = size().height - (int)(Math.random() * 20.0f);

	double dx = (Math.random() - 0.5f) * 2.0f;
	double dy = -(Math.random() * 6);

	Color fg ;

	double cs = Math.random();

	if(cs < .33f)
	  {
 	    fg = new java.awt.Color(155 + (int)(Math.random() * 100),
			  10, 10);
	  }
	else if(cs < .66f)
	  {
 	    fg = new java.awt.Color(155 + (int)(Math.random() * 100),
			  155 + (int)(Math.random() * 100),
			  10);
	  }
	else
	  {
 	    fg = new java.awt.Color(10, 10,
			  155 + (int)(Math.random() * 100));
	  }
	rockets[i] = new Rocket(nof_points, x, y, dx, dy, g,
				dotsize, size().width, size().height, trail, life_len,
				fg,
				bg);
      }
    }

  public void paint(Graphics g) {
      int i;

      g.setColor(Color.white);
      g.drawRect(0, 0, size().width - 1, size().height - 1);
      g.setColor(bg);
      g.fillRect(0, 0, size().width - 1, size().height - 1);

      g.clipRect(2, 2, size().width - 4, size().height - 4);

      for(i = 0; i < nof_rockets; i++)
	{
	  rockets[i].draw(g);
	}
  }


  public void update(Graphics g)
    {
      int i;

      if(clear == 0)
	{
	  g.clipRect(2, 2, size().width - 4, size().height - 4);
	  for(i = 0; i < nof_rockets; i++)
	    {
	      rockets[i].update(g);
	    }
	} else {
	  g.clearRect(0, 0, size().width, size().height);
	  paint(g);
	  clear = 0;
	}
    }

  public boolean mouseDown(java.awt.Event evt, int x, int y)
    {
      clear = 1;
      initrockets();
      return true;
    }

  public void run()
    {
      int dx = 1;

      while(kicker != null) {

	repaint();

	try {
	  Thread.sleep(delay);
	} catch(Exception e) {
	  ;
	}
      }
    }

  public void start()
    {
      if(kicker == null)
	{
	  kicker = new Thread(this);
	  kicker.setPriority(kicker.MIN_PRIORITY);
	  kicker.start();
	}
    }
  public void stop()
    {
      kicker = null;
    }

}

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

Adobe's Latest Flash Now Includes 3D Video
Do Ruby on Rails Developers Need Merb?
Cisco Romancing Linux Developers
MobiUI Snares ActionEngine in iPhone/Mobile Push
Windows 7: It's Not Just a Codename Anymore
Microsoft Shines Silverlight on Eclipse
OpenOffice Hits 3.0: Can It Challenge Microsoft?
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

A Replication Architecture for Enterprise-Grade Subversion
Let Your Android Application Out of the Box with SMS Integration
Intel Sees Fewer Power Cords in Your Future
F# 101
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

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: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
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
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
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES