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.

 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