multi multi threading
im trying to get my program to work It a shooting game
For some reason it doenst work
Am I on the right track for multi multi threadin?
Heres me code
import java.applet.*;
import java.awt.*;
import java.net.*;//used for sounds
import java.awt.event.*;
public class GhettoStar extends Applet implements Runnable, ActionListener, MouseListener, MouseMotionListener, FocusListener {
public static Image BG;//Bg Image
Image Title;//Title image
AudioClip BgSound;//Bg sound
AudioClip shot;//gunshot sound
TextField username;//UserName Textfeild
TextField password;//Password textfeild
int x_pos = -700;//controls how the BG image is moving in the background
int x_speed = 1;//controls the pace and direction of movement
private Image dbImage;//image used to create Double Buffering
private Graphics dbg;//Graphic image used to draw on screen
String sButtons[] = {"LOGIN", "DELETE USER", "CREATE USER", "HIGHSCORES"};
Button B[] = new Button[sButtons.length];//Holds buttons
public boolean valid = false;//keeps track of logged in user
public int Level = 0;//keeps track of level
public Image Enemy;//enemy pics
public static Image player;//players hands
AudioClip gunNoise;//sound for shots
public Level1 Level1Obj;//Level1 Obj, loads methods and constructors
public int ShotHand = 1;//controls which gun to shot
Thread starMove = new Thread(this); //Start a thread that will move the stars at certain speeds
Panel p = new Panel();
Label Tammo;
public int iammo = 100;
public int playerHealth = 3;//holds players health
public Label healthMeter;
public TextField delete;
public Label score;
public int playerScore;
public void init() {
BG = getImage(getCodeBase(), "pics/starscroll.gif");//load image
Title = getImage(getCodeBase(), "pics/GhettoStar.gif");
BgSound = getAudioClip(getCodeBase(), "music/shorty.au");// Load an audio file which is in the same directory as the class files of the applet
shot = getAudioClip(getCodeBase(), "music/shot.au");// Load an audio file which is in the same directory as the class files of the applet
this.addMouseListener(this);
this.addMouseMotionListener(this);
gunNoise = getAudioClip(getCodeBase(), "music/GunShot.au");
//setLayout(new FlowLayout());
//load textfeilds at the south
setLayout(new BorderLayout());
username = new TextField("USERNAME", 25);
password = new TextField("PASSWORD", 25);
password.addFocusListener(this);
//p.setLayout(new GridLayout(6,1));
p.setLayout(new FlowLayout());
p.add(username);
p.add(password);
//this.add(p, BorderLayout.SOUTH);
/*
this.add(username);
this.add(password);*/
username.setBounds(0,0,50,15);
password.setBounds(0,0,100,15);
//load buttons to same spot under textfields one by one
//p = new Panel();
//p.setLayout(new GridLayout(4,1));
int ictr = -1;
while(++ictr < sButtons.length) {
B[ictr] = new Button(sButtons[ictr]);
B[ictr].setSize(100,100);
B[ictr].addActionListener(this);
p.add(B[ictr]);
//this.add(B[ictr]);
B[ictr].setBounds(200,100,50, 15);
}
this.add(p, BorderLayout.SOUTH);
starMove.start();
}
public void start() {
BgSound.play();//always start the bg music
}
public void stop() { }
public void destroy() { }
public void update(Graphics g) {
// - This is code for double buffering
if (dbImage == null) {// initialize buffer
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());// clear screen in background
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());// draw elements in background
paint (dbg);
g.drawImage (dbImage, 0, 0, this);// draw image on the screen
// - That was code for double buffering
}
public void paint(Graphics g) {
switch(Level) {
case 0:g.drawImage(BG, x_pos, 0, this);
g.drawImage(Title, 100, 0, this);
break;
case 1:
g.drawImage(BG,0,0,this);
g.drawImage(player, 55,325, this);
//Level1Obj.showEnemies(g);
break;
}
}
public void run() {//right now this controls the starmove thread
while (true) { // run a long while (true) this means in our case "always"
if (x_pos == 0) {
x_speed = -1;
}
else if(x_pos == -700) {
x_speed = 1;
}
x_pos+=x_speed;
repaint();// repaint the applet
try {
// Stop thread for 20 milliseconds
Thread.sleep(10);
}
catch (InterruptedException ex) {
// do nothing
//at all
}
}
}
public void actionPerformed(ActionEvent e) {
Button buttonClicked = (Button) e.getSource();
String sbutton=buttonClicked.getLabel();
if (sbutton.equals("LOGIN")) {
//checkUser();
//if (valid == true) {
Login();
//}
//else {
//username.setText("Sorry, invalid username and password!");
//}
}
else if (sbutton.equals("DELETE USER")) {
checkUser();
if (valid == true) {
deleteUser();
}
else {
username.setText("Sorry, invalid username and password!");
}
}
else if (sbutton.equals("CREATE USER")) {
createUser();
Login();
}
else if (sbutton.equals("HIGHSCORES")) {
highScores();
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
//checkKill(mx, my);
switch (Level) {
case 0: break;
case 1:
if (iammo > 0) {
shot(mx, my);
gunNoise.play();
Tammo.setText("Ammo" +Integer.toString(--iammo));
}
break;
case 2: //Level2Obj.shot(mx, my); break; shot.play();
case 3: //Level3Obj.shot(mx, my); break; shot.play();
}
repaint();
}
public void mouseReleased(MouseEvent e) {
gunPic(e);
//check for collisions
//switch (Level) {
//case 0: break;
//case 1: Level1Obj.checkCollision(e); break;
//}
}
public void mouseMoved(MouseEvent e) {//move hands according to spot its in
gunPic(e);
}
public void mouseDragged(MouseEvent e) {}
public void focusGained(FocusEvent e) {//When the focus is on the password textfield, it sets it the ***** so it is hidden
password.echoCharIsSet();
password.setEchoChar('*');
}
public void focusLost(FocusEvent e) {}
public void gunPic(MouseEvent e) {//move hands according to spot its in
int mx = e.getX();
int my = e.getY();
if (mx < 233 && mx > 0 && my < 340) {
player = getImage(getCodeBase(), "pics/handgun/L.gif");
}
else if (mx < 467 && mx > 233 && my < 340) {
player = getImage(getCodeBase(), "pics/handgun/ST.gif");
}
else if (mx > 467 && my < 340) {
player = getImage(getCodeBase(), "pics/handgun/R.gif");
}
else if (mx < 233 && mx > 0 && my > 340) {
player = getImage(getCodeBase(), "pics/handgun/LL.gif");
}
else if (mx < 467 && mx > 233 && my > 340) {
player = getImage(getCodeBase(), "pics/handgun/LST.gif");
}
else if (mx > 467 && my > 340) {
player = getImage(getCodeBase(), "pics/handgun/LR.gif");
}
}
public void shot(int mx, int my) {
ShotHand = -ShotHand;//switch hand
if (mx < 233 && mx > 0 && my < 340) {
if (ShotHand == -1) {
player = getImage(getCodeBase(), "pics/handgun/shot/Lleft.gif");
}
else {
player = getImage(getCodeBase(), "pics/handgun/shot/Lright.gif");
}
}
else if (mx < 467 && mx > 233 && my < 340) {
if (ShotHand == -1) {
player = getImage(getCodeBase(), "pics/handgun/shot/STleft.gif");
}
else {
player = getImage(getCodeBase(), "pics/handgun/shot/STright.gif");
}
}
else if (mx > 467 && my < 340) {
if (ShotHand == -1) {
player = getImage(getCodeBase(), "pics/handgun/shot/Rleft.gif");
}
else {
player = getImage(getCodeBase(), "pics/handgun/shot/Rright.gif");
}
}
else if (mx < 233 && mx > 0 && my > 340) {
if (ShotHand == -1) {
player = getImage(getCodeBase(), "pics/handgun/shot/LLleft.gif");
}
else {
player = getImage(getCodeBase(), "pics/handgun/shot/LLright.gif");
}
}
else if (mx < 467 && mx > 233 && my > 340) {
if (ShotHand == -1) {
player = getImage(getCodeBase(), "pics/handgun/shot/LSTleft.gif");
}
else {
player = getImage(getCodeBase(), "pics/handgun/shot/LSTright.gif");
}
}
else if (mx > 467 && my > 340) {
if (ShotHand == -1) {
player = getImage(getCodeBase(), "pics/handgun/shot/LRleft.gif");
}
else {
player = getImage(getCodeBase(), "pics/handgun/shot/LRright.gif");
}
}
}
public void deleteUser() {}
public void checkUser() {}
public void createUser() {
String sName = new String(username.getText());
String sPass = new String(password.getText());
//send those 2 variables to the /files/playerdata.txt with also a null totalscore and last score value
}
public void highScores() {}
public void clearScreen() {
username.setVisible(false);
password.setVisible(false);
int ictr = -1;
while(++ictr < sButtons.length) {
B[ictr].setVisible(false);
}
this.setLayout(null);
p.setVisible(false);
repaint();
}
public void Login() {
//starMove.destroy();
x_pos = 0;
clearScreen();
//--add textfields for ammo and health and score
Tammo = new Label("Ammo" +Integer.toString(iammo));//add textfield for ammo counter
Tammo.setBounds(50,500,75,25);
this.add(Tammo);
healthMeter = new Label("Health left " + Integer.toString(playerHealth));
healthMeter.setBounds(50,475,75,25);
this.add(healthMeter);
score = new Label("Score " + Integer.toString(playerScore));
score.setBounds(50,450,75,25);
this.add(score);
//
player = getImage(getCodeBase(), "pics/handgun/ST.gif");
BG = getImage(getCodeBase(), "pics/BG1.gif");
BgSound.stop();
repaint();
Level = 1;
Level1Obj = new Level1();//start enemyPoper Thread for level1
}
}
class Level1 extends GhettoStar implements Runnable {
public int backArray[][] = new int[2][3];//holds info on whats going on in the background
Thread Enemypop;
int ictr = -1;
Enemy Enemyobjs[] = new Enemy[20];
public int ieCtr = -1;
public Level1() {
//delete after works
//Tammo.setText("JUDEN");
//healthMeter.setText("run");
//
Enemypop = new Thread(this);
Enemypop.start();
}
public void showEnemies(Graphics g) {
int x = - 1;
int y = -1;
int iCtr = 0;
while (++x < 2) {
while (++y <3) {
if (backArray[x][y] == 1) {
if (x == 0 && y == 0) {
g.drawImage(Enemyobjs[iCtr].DisplayPic, 0, 0, this);
}
else if (x==1 && y ==0) {
g.drawImage(Enemyobjs[iCtr].DisplayPic, 0, 500, this);
}
else if (x==0 && y ==1) {
g.drawImage(Enemyobjs[iCtr].DisplayPic, 200, 0, this);
}
else if (x==0 && y ==2) {
g.drawImage(Enemyobjs[iCtr].DisplayPic, 400, 0, this);
}
else if (x==1 && y ==1) {
g.drawImage(Enemyobjs[iCtr].DisplayPic, 200, 200, this);
}
else if (x==1 && y ==2) {
g.drawImage(Enemyobjs[iCtr].DisplayPic, 400, 500, this);
}
++iCtr;
}
}
}
}
public void run() {
while (ieCtr < Enemyobjs.length) {// run a long while (true) this means in our case "always"
//healthMeter.setText("SHWAG");
int rndNum1 = (int)Math.random();
int rndNum2 = (int)Math.random()*2;
if (backArray[rndNum1][rndNum2] == 0) {//open
Enemyobjs[++ieCtr] = new Enemy(getCodeBase() + "pics/x.gif",rndNum1,rndNum2);
backArray[rndNum1][rndNum2] = 1;
}
try {
// Stop thread for 20 milliseconds
Thread.sleep(10);
}
catch (InterruptedException ex) {
// do nothing
//at all
}
}
}
}
class Enemy extends GhettoStar implements Runnable {
public Image DisplayPic;
public int secondCounter;
public int x_pos, y_pos;
public int Health = 1;
public Enemy (String source, int pos_x, int pos_y) {
x_pos = pos_x;
y_pos = pos_y;
//DisplayPic = getImage(source);
healthMeter.setText("JEW");
//Thread CountSeconds = new Thread(this);
//CountSeconds.start();
}
public void run() {
while(Health == 1) { //alive
if (secondCounter < 3) {
++secondCounter;
//change pic to a more intimidating pic
}
else if (secondCounter > 3) {
secondCounter = 0;//so he does a new shot method
gunNoise.play();
--playerHealth;
if (playerHealth > 0) {
healthMeter.setText("Health Left" +Integer.toString(--playerHealth));
repaint();// repaint the applet
try {
// Stop thread for 1000 milliseconds
Thread.sleep(1000);
}
catch (InterruptedException ex) {}
}
else if (playerHealth < 0) {
//gameOver();
}
}
}
}
}

