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


Partners & Affiliates











advertisement


Mandel


Java Source:

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

public class Mandel extends Applet implements Runnable{
	int[] pixel;
	int[] color;
	Thread mainthread;
	Graphics bbg;
	Image bb;
	Image fractimg;
	
	// Configuration:
	// ----------------------------------------
	int nw = 400;				// Image width
	int nh = nw;				// Image height
	// !! Image should be quadratic!
	
	int max_i = 300;			// max iteration count
	int n_bailout = 4;			// bailout value
	int colMult = 5;			// palette index multiplication factor
	double sx = -2;				// start X
	double sy = -2;				// start Y
	double ex = 2;				// end X
	double ey = 2;				// end Y
	
	int pal_len = 2000;			// palette size
	int col_period = 15;		// palette color fade 'period'
	
	// Other vars:
	int zoom_sx = 0;
	int zoom_sy = 0;
	int zoom_ex = 0;
	int zoom_ey = 0;
	boolean mdown = false;
	
	public void init(){
		// Init stuff:
		pixel = new int[nw*nh];
		bb = createImage(nw,nh);
		fractimg = createImage(nw,nh);
		bbg = bb.getGraphics();
		initPal();	// initialize palette
		calcMandel();
		
		setBackground(Color.black);
		if(mainthread!=null){
			mainthread = new Thread(this);
			mainthread.start();
		}
	}
	
	public void stop(){
		if(mainthread!=null){
			mainthread.stop();
			mainthread = null;
		}
	}
	
	public void destroy(){
		bb = null;
		bbg = null;
		fractimg = null;
	}
	
	public void run(){
		while(true){
			repaint();
			try{
				mainthread.sleep(30);
			}catch(InterruptedException e){}
		}
	}
	
	public boolean mouseDown(Event e, int x, int y){
		zoom_sx = x;
		zoom_sy = y;
		mdown = true;
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y){
		int tmp = 0;
		double midx = 0;
		double midy = 0;
		double dx = 0;
		
		if (mdown){
			// Zoom in!
			
			mdown = false;
			zoom_ex = x;
			zoom_ey = y;
			
			if((zoom_sx != zoom_ex)&&(zoom_sy != zoom_ey)){
				if(zoom_sx > zoom_ex){
					tmp = zoom_sx;
					zoom_sx = zoom_ex;
					zoom_ex = tmp;
				}
				if(zoom_sy > zoom_ey){
					tmp = zoom_sy;
					zoom_sy = zoom_ey;
					zoom_ey = tmp;
				}
				
				midx = sx+((((zoom_sx+zoom_ex)/2)*(ex-sx))/(double)nw);
				midy = sy+((((zoom_sy+zoom_ey)/2)*(ey-sy))/(double)nh);
				
				if ((zoom_ex-zoom_sx)>(zoom_ey-zoom_sy)){
					dx = ((zoom_ex-zoom_sx)*(ex-sx))/(double)nw;	
				}
				else{
					dx = ((zoom_ey-zoom_sy)*(ey-sy))/(double)nh;
				}
				
				sx = midx-(dx/2);
				sy = midy-(dx/2);
				ex = midx+(dx/2);
				ey = midy+(dx/2);
				
				// Recalculate & show:
				calcMandel();
				repaint();
			}
		}
		return true;
	}
	
	public boolean mouseDrag(Event e, int x, int y){
		if(mdown){
			zoom_ex = x;
			zoom_ey = y;
			repaint();
		}
		return true;
	}
	
	public void update(Graphics g){
		paint(g);
	}
	
	public void paint(Graphics g){
		int rect_sx, rect_ex, rect_sy, rect_ey;
		
		bbg.drawImage(fractimg,0,0,null);
		
		// Show zoom rectangle:
		if(mdown){
			if(zoom_sx < zoom_ex){
				rect_sx = zoom_sx;
				rect_ex = zoom_ex;
			}
			else{
				rect_sx = zoom_ex;
				rect_ex = zoom_sx;
			}
			if(zoom_sy < zoom_ey){
				rect_sy = zoom_sy;
				rect_ey = zoom_ey;
			}
			else{
				rect_sy = zoom_ey;
				rect_ey = zoom_sy;
			}
			bbg.setColor(Color.black);
			bbg.drawRect(rect_sx,rect_sy,rect_ex-rect_sx,rect_ey-rect_sy);
			
		}
		g.drawImage(bb,0,0,null);
	}
	
	public void calcMandel(){
		double nx=0;
		double ny=0;
		double x0=0;
		double x1=0;
		double y0=0;
		double y1=0;
		double lower=0;
		double upper=0;
		double middle=0;
		double fade_factor=0;
		int pixx, pixy, i, ncol;
		boolean bailout = false;
		
		double col1[] = new double[3];
		double col2[] = new double[3];
		int r, g, b;
		
		// Loop through entire pixel int array:
		for(pixy=0;pixy<nh;pixy++){
			for(pixx=0;pixx<nw;pixx++){
				nx = sx + (((ex-sx)*(double)pixx)/(double)nw);
				ny = sy + (((ey-sy)*(double)pixy)/(double)nh);
				
				i  = 0;
				x0 = 0;
				y0 = 0;
				bailout = false;
				
				// Loop until bailout value is reached or the iteration count exceeds
				// the maximum iteration count specified:
				while(i < max_i){
					
					x1 = x0*x0 - y0*y0 + nx;
					y1 = 2*x0*y0 + ny;
					
					if((x1*x1+y1*y1)>n_bailout){
						// Bailout.
						bailout = true;
						lower = x0*x0+y0*y0;
						middle = n_bailout-lower;
						upper = x1*x1+y1*y1;
						break;
					}
					
					x0 = x1;
					y0 = y1;
					i++;
				}
				
				if (bailout){
					// Compute color:
					ncol = (i*colMult) % pal_len;
					col1[0] = (double)(color[ncol]>>16 & 255);
					col1[1] = (double)(color[ncol]>>8 & 255);
					col1[2] = (double)(color[ncol] & 255);
					
					col2[0] = (double)(color[ncol+colMult]>>16 & 255);
					col2[1] = (double)(color[ncol+colMult]>>8 & 255);
					col2[2] = (double)(color[ncol+colMult] & 255);
					
					fade_factor = upper-lower;
					r = (int)(col1[0] + (((col2[0]-col1[0])*middle)/fade_factor));
					g = (int)(col1[1] + (((col2[1]-col1[1])*middle)/fade_factor));
					b = (int)(col1[2] + (((col2[2]-col1[2])*middle)/fade_factor));
					
					pixel[(pixy*nw)+pixx] = (255<<24)|(r<<16)|(g<<8)|(b);
				}
				else{
					// Use white color:
					pixel[(pixy*nw)+pixx] = (255<<24)|(16777215);
				}
			}
		}
		fractimg = createImage(new MemoryImageSource(nw,nh,pixel,0,nw));
		
	}
	
	public void initPal(){
		int b1, b2, b3;
		int ppos = 0;	// The color fade 'period position'
		int col1[] = new int[3];
		int col2[] = new int[3];
		
		color = new int[pal_len];
		col1[0] = (int)(Math.random()*256);
		col1[1] = (int)(Math.random()*256);
		col1[2] = (int)(Math.random()*256);
		col2[0] = (int)(Math.random()*256);
		col2[1] = (int)(Math.random()*256);
		col2[2] = (int)(Math.random()*256);
		
		for(int i=0;i<pal_len;i++){	
			b1 = col1[0] + (((col2[0]-col1[0])*ppos)/col_period);
			b2 = col1[1] + (((col2[1]-col1[1])*ppos)/col_period);
			b3 = col1[2] + (((col2[2]-col1[2])*ppos)/col_period);
			
			color[i] = (255<<24)|(b1<<16)|(b2<<8)|(b3);
			
			if(ppos==col_period){
				ppos=0;
				col1[0] = col2[0];
				col1[1] = col2[1];
				col1[2] = col2[2];
				
				col2[0] = (int)(Math.random()*256);
				col2[1] = (int)(Math.random()*256);
				col2[2] = (int)(Math.random()*256);
			}
			ppos++;
		}
	}
}

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
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 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 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%.

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
Trolltech Expands WebKit Footprint

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