Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 23rd, 2006, 10:28 AM   #1
Ross11988
Newbie
 
Join Date: Apr 2006
Posts: 4
Rep Power: 0 Ross11988 is on a distinguished road
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 2:21 PM.
Ross11988 is offline   Reply With Quote
Old Apr 23rd, 2006, 12:55 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 23rd, 2006, 2:21 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
~Code tags added
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 23rd, 2006, 2:32 PM   #4
Ross11988
Newbie
 
Join Date: Apr 2006
Posts: 4
Rep Power: 0 Ross11988 is on a distinguished road
thank you mjordan
Ross11988 is offline   Reply With Quote
Old Apr 23rd, 2006, 3:40 PM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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 4:02 PM.
ReggaetonKing is offline   Reply With Quote
Old Apr 23rd, 2006, 4:38 PM   #6
Ross11988
Newbie
 
Join Date: Apr 2006
Posts: 4
Rep Power: 0 Ross11988 is on a distinguished road
i see wat u mean, but our java teacher is teaching us mouselistener and keylistener so thats wat i use, thanks for the advice.
Ross11988 is offline   Reply With Quote
Old Apr 24th, 2006, 9:45 AM   #7
Ross11988
Newbie
 
Join Date: Apr 2006
Posts: 4
Rep Power: 0 Ross11988 is on a distinguished road
ok i seemed to have fixed it if i take the buffer G out. Any reason why?
Ross11988 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC