In this program i want to stop GenerateImage & MovingImage Thread both...
And i want to start those threads from begining.
Can u send me the solution?
Here is the code........
package Game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.MalformedURLException;
import java.net.URL;
public class ThreadInApplet extends Applet implements KeyListener
{
private static final long serialVersionUID = 1L;
Image[] asteroidImage;
Image spaceshipImage;
String levelstr="Easy Level";
int[] XPos,YPos;
int number=0,XPosOfSpaceship,YPosOfSpaceship,NoOfObstacles=5,speed=1,level=1,spaceBtnPressdCntr=0;
boolean gameStart=false,pauseGame=false,collideUp=false,collideDown=false,collideLeft=false,collideRight=false;
private Image offScreenImage;
private Dimension offScreenSize;
private Graphics offScreenGraphics;
Thread GenerateImages,MoveImages;
public void init()
{
try
{
GenerateImages=new Thread () //thread to create obstacles
{
synchronized public void run ()
{
for(int g=0;g<NoOfObstacles;g++)
{
try
{
sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
number++; // Temporary counter to count the no of obstacles created
}
}
} ;
MoveImages=new Thread () //thread to move obstacles
{
@SuppressWarnings("deprecation")
synchronized public void run ()
{
while(YPos[NoOfObstacles-1]!=600)
{
pauseGame=false;
if(collide()==true)
{
GenerateImages.suspend();
repaint();
}
else
GenerateImages.resume();
for(int l=0;l<number;l++)
{
if(collide()==false)
YPos[l]++;
else GenerateImages.suspend();
}
repaint();
try
{
sleep(speed);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
if(YPos[NoOfObstacles-1]>=600) //level complete state
{
level++;
try
{
levelUpdation(level);
System.out.println("aahe");
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
repaint();
}
}
};
initialPos();
spaceshipImage=getImage(new URL(getCodeBase(),"images/space.png"));
for(int i=0;i<NoOfObstacles;i++)
{
asteroidImage[i]=getImage(new URL(getCodeBase(),"images/asteroid.png"));
XPos[i]=(int) (Math.random()*700);
YPos[i]=0;
}
MediaTracker tracker = new MediaTracker (this);
for(int i=0;i<NoOfObstacles;i++)
{
tracker.addImage (asteroidImage[i], 0);
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
setBackground(Color.black);
addKeyListener(this);
}
//Sets initial positions of spaceship & obstacle images------------------------------------------------------
public void initialPos() throws MalformedURLException
{
asteroidImage=new Image[NoOfObstacles];
XPos=new int[NoOfObstacles];
YPos=new int[NoOfObstacles];
XPosOfSpaceship=getWidth()/2-35;
YPosOfSpaceship=getHeight()-100;
collideUp = false;
collideDown=false;
collideLeft=false;
collideRight=false;
}
//level finished updations------------------------------------------------------------------------------
@SuppressWarnings("deprecation")
public void levelUpdation(int level) throws MalformedURLException
{
NoOfObstacles=NoOfObstacles+20;
speed=speed-3;
System.out.println(NoOfObstacles+" "+speed);
pauseGame=true;
initialPos();
repaint();
}
//paint method of graphics to print the messages---------------------------------------------------------
public void paint(Graphics g)
{
g.setColor(Color.white);
if(gameStart==false)
{
g.drawString("SPACE to start", (getWidth()/2)-15, getHeight()/2);
g.drawString(levelstr, (getWidth()/2), getHeight()/2+20);
}
if(level>1)
{
if(level==2)
levelstr="Medium Level";
else
levelstr="High Level";
g.drawString("Level Complete ", (getWidth()/2)-15, getHeight()/2);
g.drawString(levelstr, (getWidth()/2), getHeight()/2+20);
//g.drawString("SPACE to start", (getWidth()/2)-15, getHeight()/2+40);
}
for(int n=0;n<number;n++)
{
if(n>0)
g.drawImage(asteroidImage[n],XPos[n],YPos[n],this);
}
g.drawImage(spaceshipImage,XPosOfSpaceship,YPosOfSpaceship,this);
}
//update method of graphics to print the messages---------------------------------------------------------
@SuppressWarnings("deprecation")
public void update(Graphics g)
{
Dimension d = size();
if((offScreenImage == null) || (d.width != offScreenSize.width) || (d.height != offScreenSize.height))
{
offScreenImage = createImage(d.width, d.height);
offScreenSize = d;
offScreenGraphics = offScreenImage.getGraphics();
}
offScreenGraphics.clearRect(0, 0, d.width, d.height);
paint(offScreenGraphics);
g.drawImage(offScreenImage, 0, 0, null);
}
public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0) {}
//---------------------Key pressed event to start game & to move the spaceship--------------------------------------
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==32)
{
spaceBtnPressdCntr++;
if(spaceBtnPressdCntr==1)
{
gameStart=true;
GenerateImages.start();
MoveImages.start();
}
}
if(gameStart==true)
{
if(e.getKeyCode()==37)
{
new Thread ()
{
@SuppressWarnings("deprecation")
synchronized public void run ()
{
for(int cnt1=1;cnt1<=10;cnt1++)
{
if(collide()==true && collideLeft == true)
{
GenerateImages.suspend();
}
else
{
if(XPosOfSpaceship>0)
XPosOfSpaceship--;
}
}
repaint();
}
}.start();
}
if(e.getKeyCode()==38)
{
new Thread ()
{
@SuppressWarnings("deprecation")
synchronized public void run ()
{
for(int cnt1=1;cnt1<=10;cnt1++)
{
if(collide()==true && collideUp == true)
{
GenerateImages.suspend();
}
else
{
if(YPosOfSpaceship>10)
YPosOfSpaceship--;
}
}
repaint();
}
}.start();
}
if(e.getKeyCode()==39)
{
new Thread ()
{
@SuppressWarnings("deprecation")
synchronized public void run ()
{
for(int cnt1=1;cnt1<=10;cnt1++)
{
if(collide()==true && collideRight == true)
{
GenerateImages.suspend();
}
else
{
if(XPosOfSpaceship<750)
XPosOfSpaceship++;
}
}
repaint();
}
}.start();
}
if(e.getKeyCode()==40)
{
new Thread ()
{
@SuppressWarnings("deprecation")
synchronized public void run ()
{
for(int cnt1=1;cnt1<=10;cnt1++)
{
if(collide()==true && collideDown == true)
{
GenerateImages.suspend();
}
else
{
if(YPosOfSpaceship<550)
YPosOfSpaceship++;
}
}
repaint();
}
}.start();
}
}
}
//------------------------------Collision checking between Spaceship & obstacles------------------------------
public boolean collide()
{
int x1,y1,x2,y2,x3,y3,x4,y4; //coordinates of obstacles
int a1,b1,a2,b2,a3,b3,a4,b4; //coordinates of spaceship
a1 =XPosOfSpaceship;
b1=YPosOfSpaceship;
a2=a1+spaceshipImage.getWidth(this);
b2=b1;
a3=a1;
b3=b1+spaceshipImage.getHeight(this);
a4=a2;
b4=b3;
for(int a=0;a<number;a++)
{
x1 =XPos[a];
y1=YPos[a];
x2=x1+asteroidImage[a].getWidth(this);
y2=y1;
x3=x1;
y3=y1+asteroidImage[a].getHeight(this);
x4=x2;
y4=y3;
/********checking asteroid touch spaceship from up direction********/
if(y3==b1 && x4>=a1 && x4<=a2)
{
collideUp = true;
collideDown=false;
collideLeft=false;
collideRight=false;
return(true);
}
if(y3==b1 && x3>=a1 && x3<=a2)
{
collideUp = true;
collideDown=false;
collideLeft=false;
collideRight=false;
return(true);
}
/********checking asteroid touch spaceship from left direction******/
if(x2==a1 && y4>=b1 && y4<=b3)
{
collideLeft=true;
collideUp = false;
collideDown=false;
collideRight=false;
return(true);
}
if(x2==a1 && y2>=b1 && y2<=b3)
{
collideLeft=true;
collideUp = false;
collideDown=false;
collideRight=false;
return(true);
}
/********checking asteroid touch spaceship from right direction*****/
if(x1==a2 && y3>=b2 && y3<=b4)
{
collideRight=true;
collideLeft=false;
collideUp = false;
collideDown=false;
return(true);
}
if(x1==a2 && y1>=b2 && y1<=b4)
{
collideRight=true;
collideLeft=false;
collideUp = false;
collideDown=false;
return(true);
}
/********checking asteroid touch spaceship from down direction*****/
if(y1==b3 && x2>=a3 && x2<=a4)
{
collideDown=true;
collideRight=false;
collideLeft=false;
collideUp = false;
return(true);
}
if(y1==b3 && x1>=a3 && x1<=a4)
{
collideDown=true;
collideRight=false;
collideLeft=false;
collideUp = false;
return(true);
}
}
return(false);
}
}