/********* Author: Maan Ashgar ***********
* this Java file is to show two things
* 1. Threads
* 2. the paint() method
* these two things should be known to anyone programming games in Java
* this will be a vary small example of a moving picture in a window.
* I'll try to reduce the code to the absolute minimum (that I can), so you can see
* what is needed to make an image move. I'll add comments where you can, I'll try to
* explain more in the webpage later.
* if you have any question or comments about this, e-mail it to GDClub@Gmail.com
******************************************/
import java.awt.*; //this is imported for the Graphics g
import javax.swing.*; //this is imported for the JFrame, it where we draw things
import java.lang.Thread; //this for the thread, which make the things move
public class GameOne
/* the main class, its start the game and intialis the game units */
{
Render screen; //our drawing (graphics) class
Hero hero; //the hero object, the same class can be used to draw bad guys too.
MainThread m_thread; //our thread that run the game
public GameOne()
{
screen = new Render();
hero = new Hero();
hero.setPos(45,278); //set the place for the first hero picture
m_thread = new MainThread();
m_thread.setGameObj(this); //give the main game to the thread to run it
m_thread.run(true); // start running
}
public static void main(String args[]) // the main method, where everything start
{
//create a new game object, which will create the objects to draw and to draw on.
GameOne game_one = new GameOne();
}
public void update()
/* this method to update the game variables */
{
screen.draw(hero); // draw the hero
hero.updatePos(2,0); // update the hero location
//use this to stop the thread when we reach the end of the frame
if(hero.getPos() > 595)
{
m_thread.run(false);
}
}
}
class Render extends JFrame
/* we extend the JFrame to have something to draw on. later there will be a
special class that can give you a window or a Full screen to draw on */
{
Hero hero_s;
public Render()
/* we create the Frame that we will draw on */
{
// the size of our frame (the drawing area). its in pixals
setSize( 620, 500 );
// stop the user from messing with the size
setResizable(false);
// set the background color for the frame
setBackground(Color.white);
// this is used to make our JFrame visible, remove it and you wont see a thing
show();
}
public void draw(Object obj)
/* the drawing method, this pass the Graphics object of the JFrame to other objects to draw
themself on the same JFrame
note: this is the same as the paint method in the hero class
only in the paint, we dont need to retrive the Graphics g object */
{
//get the grahics object for the frame
Graphics gx = getGraphics();
hero_s = (Hero) obj;
// every time we draw on the frame, we need to clear the frame to wipe out
// the last image, try commeting these two lines and see what happen
gx.setColor(Color.WHITE);
gx.fillRect(0,0,620,500);
//pass the gx to our object that we'll draw, here its our hero
hero_s.paint(gx);
}
}
class Hero
/* this is our hero object class, its called "Sprite", a Sprite is a game object. it can be
2D or 3D. several sprits can interact with each other, like your hero and a bad guy
*/
{
Image hero;
//position_x and position_y is the place where we draw the sprite (from the top left corner)
int position_x = 33;
int position_y = 33;
public Hero()
{
//we assign the image of our hero, you can use any image insted of boogie.gif
hero = Toolkit.getDefaultToolkit().getImage("boogie.gif") ;
}
public int getPos()
// give the current hero location, we will use it to stop the thread when the image
// reach the end of the frame
{
return position_x;
}
public void setPos(int a, int b)
// this method to set the position of the sprite, if we want to change it
{
position_x = a;
position_y = b;
}
public void updatePos(int a, int b)
// this move the current image by a in x-direction and b in y-direction
{
position_x += a;
position_y += b;
}
public void paint(Graphics g)
// the draw method for this sprite, it well draw this object on the main JFrame
{
// we cast the g object to Graphics2D so that we can use the drawImage() method
Graphics2D g2 = (Graphics2D) g ;
//draw the hero with the (Image, x position, y position , another thing you dont need to worry about)
g2.drawImage(hero, position_x, position_y,null);
}
}
class MainThread
{
GameOne game;
boolean running; // use this to start or stop the theard
public void setGameObj(GameOne obj)
{
game = obj; //here we take the main game object
}
public void run(boolean a)
{
if(a)
{
running = true ;
run();
}
else
running = false ;
}
public void run()
// the run method, it the place where the main game loop is placed
{
while(running)
{
game.update(); // this a method in the main class, it update the screen and the hero object
// at the moment, there is no user input
try
{
Thread.sleep(50); // this is how fast the hero image move, increase it for faster movement
}
catch (InterruptedException e)
{
System.out.println("interrupted");
}
}
}
}