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


Partners & Affiliates











advertisement

SimplePong


//SimplePong is a java applet written by Daniel Moscufo
//of scufo http://www.esearch.com.au/scufo. It can be
//considered freeware.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import java.net.*;
import java.awt.image.*;

public class SimplePong extends Applet
{
    public void init()
    {
	//Set default colour=black, width=300, height=80
	//Difficulty =5, image = backing.gif
	int bcRed = 0;
	int bcBlue = 0;
	int bcGreen = 0;
	int Width = 300;
	int Height = 80;
	int Difficulty = 5;
	String backimage = "backing.gif";

	//Check for user parameters
	if(getParameter("Red")!=null)
	    bcRed = new Integer(getParameter("Red").trim()).intValue();
	if(getParameter("Blue")!=null)
	    bcBlue = new Integer(getParameter("Blue").trim()).intValue();
	if(getParameter("Green")!=null)
	    bcGreen = new Integer(getParameter("Green").trim()).intValue();
	if(getParameter("Width")!=null)
	    Width = new Integer(getParameter("Width").trim()).intValue() -20;
	if(getParameter("Height")!=null)
	    Height = new Integer(getParameter("Height").trim()).intValue()-20;
	if(getParameter("Difficulty")!=null)
	    Difficulty = new Integer(getParameter("Difficulty").trim()).intValue();
	if(getParameter("BackImage")!=null)
	    backimage= getParameter("BackImage");

	//Set these above values
	setBackground(new Color(bcRed,bcGreen,bcBlue));
	Image ball = getImage(getCodeBase(), "ball.gif");
	Image paddle1 = getImage(getCodeBase(),"panel1.gif");
	Image paddle2 = getImage(getCodeBase(),"panel2.gif");
	Image back = getImage(getCodeBase(),backimage);
	Image toplay = getImage(getCodeBase(),"Toplay.gif");

	//Create new Pongtable with correct values
	PongTable pongtable = new PongTable(Width,Height,ball,paddle1,paddle2,back,toplay, Difficulty);

	//this below code is just to add a centered pong game
	setLayout(new BorderLayout());
	Panel centredpanel = new Panel();
	centredpanel.add(pongtable);
	add(centredpanel, "Center");
    }

}

class PongTable extends Canvas implements Runnable, MouseMotionListener, MouseListener
{
    int Xsize, Ysize, Xball, Yball, Xbat, Ybat, XbatC, YbatC, difficulty;
    double direction[] = new double[2];
    Thread thisthread;
    int Xbatdir[] = new int[2];
    int Ybatdir[] = new int[2];
    boolean Centred = true;
    Image ball, paddle1, paddle2, back, toplay;
    //AudioClip pong;

    PongTable(int xsize, int ysize, Image Ball, Image Paddle1, Image Paddle2, Image Back, Image Toplay, int Difficulty)
    {

	//This method just setups all the variables
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	addMouseMotionListener(this);
	addMouseListener(this);
	setSize(xsize,ysize);
	Xsize = xsize;
	Ysize = ysize;
	startpositions();

	Xball = (int)Xsize/2;
	Yball = (int)Ysize/2;
	direction[0] = 0;
	direction[1] = 0;

	ball = Ball;
	paddle1 = Paddle1;
	paddle2 = Paddle2;
	back = Back;
	toplay = Toplay;
	difficulty = Difficulty;

	//Start the thread.
	thisthread = new Thread(this);
	thisthread.start();

    }

    void startpositions()
    {
	//Specify the start positions
	Xball = 20;
	Yball = 20;
	Xbat = 20;
	Ybat = 40;
	XbatC = Xsize -20;
	YbatC = 40;
	direction[0] = 4.0; //X velocity vector
	direction[1] = 4.0; //Y velocity vector
    }

    public void update(Graphics g)
    {
	//This is just your standard double buffering update method
	Graphics offgraph;
	Image offscreen = null;
	Dimension d = getSize();
       	offscreen = createImage(d.width, d.height);
	offgraph = offscreen.getGraphics();
	offgraph.setColor(getBackground());
	offgraph.fillRect(0, 0, d.width, d.height);
	offgraph.setColor(getForeground());
	paint(offgraph);
	g.drawImage(offscreen, 0, 0, this);
	offscreen.flush();
    }

    public void paint(Graphics g)
    {
	//Draw Background Image
	g.drawImage(back,5,5,Xsize-10,Ysize-10,this);
	//Draw users bat
	g.drawImage(paddle1,XbatC-2,YbatC-10,this);
	//Draw computer bat
	g.drawImage(paddle2,Xbat-2,Ybat-10,this);
	//draw ball
	g.drawImage(ball,Xball-5, Yball-5,this);

	//check if no one is playing and display "click to play"
	if(Centred)
	    g.drawImage(toplay,10, 10,this);
    }

    public void run()
    {
	double Xballdb, Yballdb;
	Xballdb = Xball;
	Yballdb = Yball;
	int n;

	while(true)
	    {
		//We check for ball hitting surface 4 times for every repaint()
		for(n=0;n<4;n++)
		    {
			//increase X & Y by velocity/4
			Xballdb = Xballdb + direction[0]/4;
			Yballdb = Yballdb + direction[1]/4;

			//check if ball hit either "goal"
			//centred tells that game is over
			if(Xballdb<10 || Xballdb>Xsize-10)
			    {
				Xballdb = Xsize/2;
				Yballdb = Ysize/2;
				direction[0] = 0;
				direction[1] = 0;
				Centred = true;
			    }

			//Check if ball hits either top or bottom wall
			// if so change y to = -y
			if(Yballdb<10 || Yballdb>Ysize-10)
			    {
				direction[1]=-1*direction[1];
			    }

			//Now we see if bat hits players bat
			if(Xballdb>18 && Xballdb<27)//X thickness of bat
			    //check if ball is within 10 pixels Y dirn
			    //of bat
			    if(Math.abs(Yballdb-Ybat)<=10)
				{
				    //if so x = -x + x bat movement dirn,
				    //y = -y + y bat movement dirn
				    direction[0] = -1*direction[0]+(int)((Xbatdir[0]-Xbatdir[1])/8);
				    direction[1] = direction[1]+(int)((Ybatdir[0]-Ybatdir[1])/8);
				    //place ball at front of bat
				    Xballdb=28;
				}

			//Now we do the same for computer bat
			if(Xballdb<(Xsize-15) && Xballdb>(Xsize-22))
			    if(Math.abs(Yballdb-YbatC)<=10)
				{
				    //x = -x - random no.
				    direction[0] = -1*direction[0] + -1*Math.random();
				    //y = -y + random no proportional to x speed
				    //This helps in stopping the ball going
				    //back and further on same y value
				    direction[1] = direction[1] + (direction[0]/4)*Math.sin(Math.random()*2*Math.PI);
				    //place ball on front of bat
				    Xballdb=Xsize-23;
				}
		    }

		//send paint correct coords
		Xball = (int)Xballdb;
		Yball = (int)Yballdb;

		//ok now do Computer AI
		//check ball in our half
		if(Xball>Xsize/2)
		    {
			//ok this algorithym is simple but effective
			//firstly check the bat and ball aren't the same height
			//then find if bat is above or below the bat
			//then move bat up/down by difficulty pixels
			if((Yball-YbatC)!=0)
			    YbatC = (Yball-YbatC)/(Math.abs(Yball-YbatC))*difficulty + YbatC;
		    }

		//recall the paint method
		repaint();

		//Ok sleep the thread fo 50 ms
		try
		    {
			thisthread.sleep(50);
		    }

		catch(Exception e)
		    {
			System.out.println("Error on sleep");
		    }

	    }
    }

    public void mouseMoved(MouseEvent e)
    {
	int x = e.getX();
	int y = e.getY();

	//Ok here we move the bat by setting it equal to mouse height
	if(y>15 && y<Ysize-10)
	    {
		Ybat = y;
		//below we set up the bat movement dir'n. so the player
		//can influence the ball dir'n.
		Xbatdir[1] = Xbatdir[0];
		Ybatdir[1] = Ybatdir[0];
		Xbatdir[0] = x;
		Ybatdir[0] = y;
	    }

	repaint();
    }

    public void mouseDragged(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
    }

    public void mouseReleased(MouseEvent e)
    {
    }

    public void mouseClicked(MouseEvent e)
    {
	//Check if "start new game"
	if(Centred)
	    {
		startpositions();
		Centred = false;
	    }

    }

     public void mouseEntered(MouseEvent e)
    {
    }

     public void mouseExited(MouseEvent e)
    {
    }
}

Back to the SimplePong 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.

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

Google Hopes Chrome Will Help, Not Hurt Firefox
Remember Figlets? They're Back With Zend
Microsoft Readies an App Store Competitor?
Google: Chrome Browser Will Make Money
Sam Ramji: Microsoft's Man in Open Source
Google to Shake Up Browsers With Own Launch
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity

Code Around C#'s Using Statement to Release Unmanaged Resources
Writing Functional Code with RDFa
BitLocker Brings Encryption to Windows Server 2008
Network Know-How: Exploring Network Algorithms
Create a Durable and Reliable WCF Service with MSMQ 4.0
The Baker's Dozen: 13 Tips for SQL Server 2008 and SSRS 2008
Book Excerpt: Microsoft Expression Blend Unleashed
Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element

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
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
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
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES