![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 4
Rep Power: 0
![]() |
Java Applet Problem
Im trying to get a picture to move up and down when i press W and S however it will not move, im repainting the screen and everything, here is the code
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[i].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){}
}Last edited by Mjordan2nd; Apr 23rd, 2006 at 3:21 PM. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Unfortunately, I don't know Java, so I can't help you. However, I'd recommend using [code] tags next time - they'll help people read your code, which should hopefully result in better answers.
|
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
~Code tags added
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2006
Posts: 4
Rep Power: 0
![]() |
thank you mjordan
|
|
|
|
|
|
#5 |
|
Sexy Programmer
|
I am not so good at graphics with Applets yet but you might be interested in an MouseAdapter and KeyAdapter adapter classes. They have the same methods as MouseListener and KeyListener but don't require you to use all the methods in that class but only the ones you want to re-define yourself. All you have to do is replace "MouseListener" and "KeyListener" with "MouseAdapter" and "KeyAdapter". That will save you some lines of code there! Sorry I can't help with your problem though!
Example [PHP] import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MouseTrackerOne extends JFrame { private JLabel statusbar; public MouseTrackerOne() { super("MouseListener Test One"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); statusbar = new JLabel(); getContentPane().add(statusbar, BorderLayout.SOUTH); addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent event) { statusbar.setText("Clicked at [" + event.getX() + ", " + event.getY() + "]"); } } ); setLocation(300,200); setSize(300,300); setVisible(true); } public static void main(String args[]) { new MouseTrackerOne(); } } [/PHP] You see what I mean? But I wish I could help you out though
__________________
I would love to change the world, but they won't give me the source code! Last edited by ReggaetonKing; Apr 23rd, 2006 at 5:02 PM. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Posts: 4
Rep Power: 0
![]() |
i see wat u mean, but our java teacher is teaching us mouselistener and keylistener so thats wat i use, thanks for the advice.
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2006
Posts: 4
Rep Power: 0
![]() |
ok i seemed to have fixed it if i take the buffer G out. Any reason why?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|