• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Help

roninblade

[H]ard|Gawd
Joined
Sep 22, 2004
Messages
2,031
THis is really pissing me off im trying to make a spacefighter applet and the damn ships wont move up and done, i have no idea wat im missing, anyway heres the program, i really need help if anyone could find the problem that would be great

//Matt Ross
//Spaceship Program
//4/10/2006

import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;


public class SpaceShip extends Applet implements KeyListener, MouseListener, Runnable
{
int ypos1=200,ypos2=200;
int rhit=0,lhit=0;
int [] laserarray = new int[50];
boolean showRules=true;
//TextArea RulesTA = new TextArea();
Image pic,pic2,laser,buffer;
SpaceClass mp;
Thread main;
Graphics bufferG;

public void init()
{

this.resize(1024,768);
this.setLayout(null);
this.addMouseListener(this);
this.addKeyListener(this);

//RulesTA.setBounds(200,200,270,700);
//RulesTA.setBackground(Color.blue);
//this.add(RulesTA);
//RulesTA.setEnabled(false);
//RulesTA.setVisible(showRules);

buffer = createImage(this.getWidth(),this.getHeight());
bufferG = buffer.getGraphics();

pic=this.getImage(this.getCodeBase(),"pic1.jpg");
pic2=this.getImage(this.getCodeBase(),"pic2.jpg");
laser=this.getImage(this.getCodeBase(),"laser.jpg");

mp = new SpaceClass(laser,1100,1100,10,10,30);

for(int ct=0; ct<=49; ct++)
{
laserarray[ct]=new SpaceClass(laser,2000,2000,0,0,25);
}

//mp.start();
//main.start();


}

public void Rules()
{
//RulesTA.setText("Welcome to Spaceship Fighters To win the game your ship must hit the other player three times. Player 1 is on the left \nPlayer2 is on the right. You can only move up and down Player 1 uses 'W' and 'S' to move up and down and 'D' to shoot Player 2 uses 'I' and 'K' to move up and down and 'J' to shoot \n Click the screen to start the game");

}
public void keyReleased(KeyEvent e)
{
char move;
move=e.getKeyChar();

if(move== 'w')
ypos1=ypos1-10;
if(move== 's' && ypos1<750)
ypos1=ypos1+10;
if(move== 'i' && ypos2>0)
ypos2=ypos2-10;
if(move== 'k' && ypos2<750)
ypos2=ypos2+10;
if(move== 'j')
{
}
if(move== 'd')
{
}
}

public void paint(Graphics g)
{
bufferG.setColor(Color.white);
bufferG.fillRect(0,0,1100,800);

for(int ct=0; ct<=45; ct++)
{
laserarray.getImage();
g.drawImage(laser,120,220,40,20,this);

}
if(showRules==true)
Rules();
if(showRules==false)
{

bufferG.setColor(Color.black);
bufferG.drawImage(pic,0,ypos1,this);
bufferG.drawImage(pic2,900,ypos2,this);
bufferG.drawImage(mp.getImage(),mp.getX(),mp.getY(),this);
bufferG.drawString("Player 1 Hits" +lhit,100,50);
bufferG.drawString("Player 2 Hits" +rhit,700,50);
bufferG.drawString(""+ypos1,100,70);
bufferG.drawString(""+ypos2,100,70);
}
g.drawImage(buffer,0,0,this);

}
public void update(Graphics g)
{
paint(g);
}

public void run()
{
while(true)
{
repaint();
try
{
main.sleep(mp.getSpeed());
}
catch(Exception e){}
}
}


public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
showRules=false;
//RulesTA.setVisible(showRules);
repaint();
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){}
}
 
anyone? anyone kno of another forum that may be able to help me with this? thank you
 
try programmingforums dot org, there's some java people there.
 
More specific, which bufferG? you sent G as an arg to paint ... im assuming it was meant it that way. Your code tabbing is horrid man, nobody can read that, no wonder you haven't gotten a reply lol. Either way you have to have a backbuffer to redraw on. Id play with the code but I'm busy ATM.

Can you repost the indented 'working' code?
 
Back
Top