Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

Applet game Template.

Copy the following to a file
called Template.java. Compile it,
than place the class in the same
directory as your HTML file
*************************/
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
public class Template extends Applet implements Runnable, KeyListener


{
//Some variables
protected int refreshRate = 50; //how long each 'second' is in your game
protected Thread thisThread;
//This will handle our animations and general sequencing
protected Graphics offscreen;
//Allows double Buffering of painting graphics, see the paint(Graphics ) method
//for more info
protected Image offscreenImage;
int x, y;
/*********************
Init is called when your applet is loaded. I start the thread here,
but you may wish to start it elsewhere. (When the user presses a start
button perhaps)
*********************/
public void init()


{
startUp();
}
public void startUp()


{
//
//At this point you usually want to add the buttons,
//textboxs or textfields
//
offscreenImage=createImage(bounds().width,bounds().height);
offscreen=offscreenImage.getGraphics();
thisThread=new Thread(this); //initalizes our thread
thisThread.start(); //calls the run function
//these are for the moving message
x=200;
y=200;
System.out.println("System Started");
}
//
//A mandatory function for applets, it is responsible for painting
//graphics and pictures to the screen
//DOUBLE BUFFERING
//We will use a process where we paint to a canvas that is not visible,
//than when we have painted everything we need, we show that canvas to the
//user, wipe our old canvas off and start over.
//this will reduce if not remove any annoying flickering that the user might
//see. (Note this techinque is not proven, on some older machines the flickering
//is still aparent)
//
public void paint(Graphics g)


{
//
//wipe our old screen clean, by creating a large black rectangle on it
//
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,bounds().width,bounds().height);
//
//Draw a message
//
offscreen.setColor(Color.red);
offscreen.drawString("Hello this is a message", x,y); //Paint the Message
//
//Now that we are done drawing on our old canvas, we will show the
//finished image to our users.
//
g.drawImage(offscreenImage,0,0,this);
}
//Another mandatory function, not sure what difference this makes but if you don't have it
//the screen flickers intermidently
public void update(Graphics g)


{
paint(g);
}
//public void paintCanvas(Grapics g);
//
//The following functions will handle the key event caller.
//The one I use the most is key up
//
//JDK 1.3 specific
public void keyTyped(KeyEvent e){} //Fully Typed Key
public void keyPressed(KeyEvent e){} //Key Held Down
public void keyReleased(KeyEvent e){} //Key released
public boolean keyPressed(Event e, int key){return true;}
public boolean keyUp(Event e, int key){return true;}
//
//JDK 1.0 (a little safer cause I'm old school
//
public boolean keyDown(Event e, int key)


{
System.out.println(key + " was pressed");
//
//Some valuable key values to know
//
switch(key)


{
case 1004: //UP Arrow Key
break;
case 1007: //RIGHT Arrow Key
break;
case 1005: //DOWN Arrow Key
break;
case 1006: //LEFT Arrow Key
break;
case 32://SPACE
break;
}
return true;
}
//
//This will be called one time, when the thread is started
//
public void run()


{
while(! isGameOver()) //As long as the game is not over


{
//do some stuff here
x--;
try{thisThread.sleep(refreshRate);} //let screen catch up with the eyeballs
catch(Exception e){}
//you usually have changed the screen at this point so you may want to refresh it
this.repaint();
}
}
public boolean isGameOver(){return false;}
}

No comments: