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


Partners & Affiliates











advertisement

ShootingGallery


import java.awt.*;
import java.applet.Applet;
import java.util.Hashtable;

public class ShootingGallery extends Applet	{
	static Image background;
	static Graphics g;
	static int score;
	static int loadX=10,loadY=10,loadXStep=5,loadYStep=5;
	Target tempTarget;
	Bullet tempBullet;
	static Hashtable targetHashtable=new Hashtable(),keyHashtable=new Hashtable();	//hashtables for targets & keys
	MediaTracker imageTracker;
	Button newGameButton;
	public void init()	{
		imageTracker=new MediaTracker(this);
		for (int i=1;i<37;i++)	{
			Gun.gunArray[i-1]=getImage(getCodeBase(),"gun" + i + ".gif");
			imageTracker.addImage(Gun.gunArray[i-1],0);
		}
		background=getImage(getCodeBase(),"ShootingGalleryBg.gif");
		imageTracker.addImage(background,0);
		Target.duck_left=getImage(getCodeBase(),"duckLeft.gif");
		imageTracker.addImage(Target.duck_left,0);
		Target.duck_right=getImage(getCodeBase(),"duckRight.gif");
		imageTracker.addImage(Target.duck_right,0);
		for (int i=1;i<5;i++)	{
			Target.duckFallLeft[i-1]=getImage(getCodeBase(),"duckLeft" + i + ".jpg");
			imageTracker.addImage(Target.duckFallLeft[i-1],0);
		}
		for (int i=1;i<5;i++)	{
			Target.duckFallRight[i-1]=getImage(getCodeBase(),"duckRight" + i + ".jpg");
			imageTracker.addImage(Target.duckFallRight[i-1],0);
		}
		imageTracker.checkID(0,true);
		g=getGraphics();
		Target.g=getGraphics();
		Bullet.g=getGraphics();
		score=0;
		Bullet.ammo=15;
		setLayout(new BorderLayout());
		newGameButton=new Button("New Game");
		add("South",newGameButton);
		g.setColor(Color.white);
		g.fillRect(0,0,300,350);
		while(!imageTracker.checkAll())	{
			g.setColor(Color.black);
			g.drawString("Loading Images...",loadX,loadY);
			System.out.println("Loading images...");
			try	{
			Thread.sleep(250);
			} catch(Exception e)	{}
			g.setColor(Color.white);
			g.drawString("Loading Images...",loadX,loadY);
			if ((loadX>=290) || (loadX<=0))
				loadX=0-loadX;
			loadX+=loadX;
			if ((loadY>=340) || (loadY<=0))
				loadY=0-loadY;
			loadY+=loadY;
		}
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
		tempTarget=new Target(0,true);
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
		tempTarget=new Target(40,false);
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
		try	{
			Thread.sleep(Target.delay*110);
		} catch(Exception e)	{}
		tempTarget=new Target(160,true);
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
		tempTarget=new Target(200,false);
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
		try	{
			Thread.sleep(Target.delay*200);
		} catch(Exception e)	{}
		tempTarget=new Target(80,true);
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
		tempTarget=new Target(120,false);
		g.drawImage(background,0,0,this);
		g.setColor(Color.black);
		g.drawString("Score: " + score,10,310);
	}
	public void stop()	{
		for (int i=0;i<6;i++)	{
			if (keyHashtable.get(new Integer(i))!=null)	{
				tempTarget=(Target)targetHashtable.get(new Integer(i).toString());
		//		tempTarget.fall();
				tempTarget.stop();
			}
		}
	}
	public void start()	{
		for (int i=0;i<6;i++)	{
			if (keyHashtable.get(new Integer(i))!=null)	{
				tempTarget=(Target)targetHashtable.get(new Integer(i).toString());
				tempTarget.start();
			}
		}
	}
	public void destroy()	{
		for (int i=0;i<6;i++)	{
			if (keyHashtable.get(new Integer(i))!=null)	{
				tempTarget=(Target)targetHashtable.get(new Integer(i).toString());
		//		tempTarget.fall();
				tempTarget.stop();
			}
		}
	}
	public void paint(Graphics g)	{
		//if (!imageTracker.checkAll())	{
		//	g.drawString("Loading Images...",130,150);
		//} else	{
			g.drawImage(background,0,0,this);
			g.setColor(Color.black);
			g.drawString("Score: " + score,10,310);
			g.drawString("Ammo: " + Bullet.ammo,240,310);
		//}
	}
	public boolean mouseMove(Event e, int x, int y)	{
		if(y<=40)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[0],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[1],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[2],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[3],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[4],140,241,this);
			else
				g.drawImage(Gun.gunArray[5],140,241,this);
		}
		else if (y<=80)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[6],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[7],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[8],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[9],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[10],140,241,this);
			else
				g.drawImage(Gun.gunArray[11],140,241,this);
		}
		else if (y<=120)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[12],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[13],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[14],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[15],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[16],140,241,this);
			else
				g.drawImage(Gun.gunArray[17],140,241,this);
		}
		else if (y<=160)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[18],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[19],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[20],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[21],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[22],140,241,this);
			else
				g.drawImage(Gun.gunArray[23],140,241,this);
		}
		else if (y<=200)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[24],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[25],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[26],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[27],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[28],140,241,this);
			else
				g.drawImage(Gun.gunArray[29],140,241,this);
		}
		else if (y<=240)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[30],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[31],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[32],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[33],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[34],140,241,this);
			else
				g.drawImage(Gun.gunArray[35],140,241,this);
		}
		return true;
	}
	public boolean mouseDrag(Event e, int x, int y)	{
		if(y<=40)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[0],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[1],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[2],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[3],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[4],140,241,this);
			else
				g.drawImage(Gun.gunArray[5],140,241,this);
		}
		else if (y<=80)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[6],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[7],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[8],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[9],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[10],140,241,this);
			else
				g.drawImage(Gun.gunArray[11],140,241,this);
		}
		else if (y<=120)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[12],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[13],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[14],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[15],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[16],140,241,this);
			else
				g.drawImage(Gun.gunArray[17],140,241,this);
		}
		else if (y<=160)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[18],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[19],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[20],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[21],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[22],140,241,this);
			else
				g.drawImage(Gun.gunArray[23],140,241,this);
		}
		else if (y<=200)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[24],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[25],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[26],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[27],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[28],140,241,this);
			else
				g.drawImage(Gun.gunArray[29],140,241,this);
		}
		else if (y<=240)	{
			if (x<=50)
				g.drawImage(Gun.gunArray[30],140,241,this);
			else if (x<=100)
				g.drawImage(Gun.gunArray[31],140,241,this);
			else if (x<=150)
				g.drawImage(Gun.gunArray[32],140,241,this);
			else if (x<=200)
				g.drawImage(Gun.gunArray[33],140,241,this);
			else if (x<=250)
				g.drawImage(Gun.gunArray[34],140,241,this);
			else
				g.drawImage(Gun.gunArray[35],140,241,this);
		}
		return true;
	}
	public boolean mouseDown(Event e, int x, int y)	{
		if (Bullet.ammo>0)	{
			if ((y<238) && (y>=0) && (x>=0) && (x<=300))	{	//make sure shot is on-screen
				tempBullet=new Bullet(x,y);
				tempBullet.start();
				for (int i=0;i<6;i++)	{	//loop through targets to check for hits
					if (keyHashtable.get(new Integer(i))!=null)	{
						tempTarget=(Target)targetHashtable.get(new Integer(i).toString());
						if ((x+Bullet.radius>=tempTarget.x) && (x-Bullet.radius<=tempTarget.x+46) && (y+Bullet.radius>tempTarget.y) && (y-Bullet.radius<tempTarget.y+39))	{
							tempTarget.fall();
							if (targetHashtable.size()==0)
								newLevel();
						}
					}
				}
				try	{
					Thread.sleep(300);
				} catch(Exception ee)	{}
				Bullet.ammo--;
				repaint();
			}
		}
		return true;
	}
	public boolean action(Event e, Object arg)	{
		if (e.target instanceof Button)	{
			newGame();
		}
		return super.action(e,arg);
	}
	public boolean mouseEnter(Event e, int x, int y)	{
		setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
		return true;
	}
	public boolean mouseExit(Event e, int x, int y)	{
		setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		return true;
	}
	public void newGame()	{
		for (int i=0;i<6;i++)	{
			if (keyHashtable.get(new Integer(i))!=null)	{
				tempTarget=(Target)targetHashtable.get(new Integer(i).toString());
				tempTarget.fall();
				tempTarget.stop();
			}
		}
		score=0;
		Bullet.ammo=15;
		Target.masterKey=0;
		Target.delay=15;
		Target.step=1;
		keyHashtable=new Hashtable();
		tempTarget=new Target(0,true);
		tempTarget.start();
		tempTarget=new Target(40,false);
		tempTarget.start();
		try	{
			Thread.sleep(Target.delay*70);
		} catch(Exception e)	{}
		tempTarget=new Target(80,true);
		tempTarget.start();
		tempTarget=new Target(120,false);
		tempTarget.start();
		try	{
			Thread.sleep(Target.delay*70);
		} catch(Exception e)	{}
		tempTarget=new Target(160,true);
		tempTarget.start();
		tempTarget=new Target(200,false);
		tempTarget.start();
		repaint();
	}
	public void newLevel()	{
		Bullet.ammo=15;
		Target.masterKey=0;
		if (score%1800==0)
			Target.step++;
		else
			Target.delay--;
		keyHashtable=new Hashtable();
		tempTarget=new Target(0,true);
		tempTarget.start();
		tempTarget=new Target(40,false);
		tempTarget.start();
		try	{
			Thread.sleep(Target.delay*(long)(Math.random()*90+90));
		} catch(Exception e)	{}
		tempTarget=new Target(80,true);
		tempTarget.start();
		tempTarget=new Target(120,false);
		tempTarget.start();
		try	{
			Thread.sleep(Target.delay*(long)(Math.random()*90+90));
		} catch(Exception e)	{}
		tempTarget=new Target(160,true);
		tempTarget.start();
		tempTarget=new Target(200,false);
		tempTarget.start();
		repaint();
	}
}
public class Gun	{
	static Image[] gunArray=new Image[36];
}
public class Bullet extends Thread	{
	int x, y;
	static Color col=Color.magenta;
	static int radius=3;
	static Graphics g;
	static int ammo;
	Bullet(int x, int y)	{
		this.x=x;
		this.y=y;
	}
	public void run()	{
		g.setColor(col);
		g.fillOval(this.x-radius,this.y-radius,2*radius,2*radius);
		try	{
			sleep(250);
		} catch(Exception e)	{}
		g.setColor(Color.white);
		g.fillOval(this.x-radius,this.y-radius,2*radius,2*radius);
		this.stop();
	}

}
public class Target extends Thread	{
	static Image duck_left;
	static Image duck_right;
	static Image[] duckFallLeft=new Image[4];
	static Image[] duckFallRight=new Image[4];
	static Graphics g;
	static int masterKey=0;
	int x,y,key;
	String s;
	static int delay=15,step=1;
	boolean dir, alive=true;
	Target(int y, boolean dir)	{
		if (dir)	{
			this.x=-45;
		} else	{
			this.x=299;
		}
		this.y=y;
		this.dir=dir;
		this.key=masterKey;
		ShootingGallery.targetHashtable.put(new Integer(this.key).toString(),this);
		ShootingGallery.keyHashtable.put(new Integer(this.key),new Integer(this.key));
		masterKey++;
	}
	public void run()	{
		while (alive)	{
			if (dir)	{
				while	(this.x<300)	{
					g.drawImage(duck_right,this.x,this.y,new ShootingGallery());
					try	{
						sleep(delay);
					} catch (Exception e)	{}
					g.setColor(Color.white);
					g.fillRect(this.x,this.y,step,39);
					this.x+=step;
				}
			}
			else	{
				while	(this.x>(-45))	{
					g.drawImage(duck_left,this.x,this.y,new ShootingGallery());
					try	{
						sleep(delay);
					} catch (Exception e)	{}
					g.setColor(Color.white);
					g.fillRect(this.x+45,this.y,step,39);
					this.x-=step;
				}
			}
			dir=!dir;
		}
	}
	public void fall()	{
		this.alive=false;
		this.stop();
		ShootingGallery.targetHashtable.remove(new Integer(this.key).toString());
		ShootingGallery.keyHashtable.remove(new Integer(this.key));
		ShootingGallery.score+=100;
		if (!dir)	{
			for (int i=0;i<4;i++)	{
				try	{
				sleep(200);
				} catch(Exception e)	{}
				g.drawImage(duckFallLeft[i],this.x,this.y,new ShootingGallery());
			}
		}
		else	{
			for (int i=0;i<4;i++)	{
				try	{
				sleep(200);
				} catch(Exception e)	{}
				g.drawImage(duckFallRight[i],this.x,this.y,new ShootingGallery());
			}
		}
		g.setColor(Color.white);
		g.fillRect(this.x,this.y,47,39);
	}
}

Back to the ShootingGallery 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 RIA Development Center
 IBM Rational Resource Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
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%.

Free VMware Server 2.0 Now Release Candidate
Linux Player Xandros Grabs Storied Rival Linspire
Hey Enterprise: Here Comes the 3G iPhone
MySpace Opens Profile Portability API
Microsoft Jumps Into Virtualization Fray
Eclipse Ganymede Makes It Easier for Devs
Open Source Nokia a Threat to Microsoft, Google?
Salesforce, Google Head for 2nd on Apps
HP Open Sources Unix File System for Linux
Red Hat Opens Its Network to Space

Build a Generic Histogram Generator for SQL Server
Beyond XML and JSON: YAML for Java Developers
Mastering the Windows Mobile Emulators
Avaya AE Services Provide Rapid Telephony Integration with Facebook
Featured Algorithm: Intel Threading Building Blocks: parallel_reduce
Getting Started with Windows Live Admin Center
Eight Key Practices for ASP.NET Deployment
Java ME User Interfaces: Do It with LWUIT!
Talking VPro: Transcript
Bringing Semantic Technology to the Enterprise

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 eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
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
HP Video: StorageWorks EVA4400 and Oracle
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
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES