Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Image + Mouse Movement
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
YM




PostPosted: Sun Jan 10, 2010 2:09 pm   Post subject: Image + Mouse Movement

Ok So I am attempting to make a space shooter game, so far this is my code, I was able to make the image print to screen in a program seperate from the mouse portion of the program, although when I try to combine them I get a nullpointexception.

Once I finish this I was just going to learn how to do collision detecting between two images (bad guys and ur ship) and then create a random x integer for the bad guys to come on the screen using Math.random and then create a loop with prints ur ship, the baddies (moving towards u) and the score on the screen...

once i got that if i still have time I am going to add bullets coming from the ship to kill em enemies instead of simply dodging them

code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class Mouse2 extends Applet
    implements MouseListener, MouseMotionListener
{




    int width, height;
    int mx, my;  // the mouse coordinates
    boolean isButtonPressed = false;

    public void init ()
    {
        width = getSize ().width;
        height = getSize ().height;


        mx = width / 2;
        my = height / 2;

        addMouseListener (this);
        addMouseMotionListener (this);
    }


    public void mouseEntered (MouseEvent e)
    {
        // called when the pointer enters the applet's rectangular area
    }


    public void mouseExited (MouseEvent e)
    {
        // called when the pointer leaves the applet's rectangular area
    }


    public void mouseClicked (MouseEvent e)
    {
        // called after a press and release of a mouse button
        // with no motion in between
        // (If the user presses, drags, and then releases, there will be
        // no click event generated.)
    }


    public void mousePressed (MouseEvent e)
    { // called after a button is pressed down
        isButtonPressed = true;
        repaint ();
        // "Consume" the event so it won't be processed in the
        // default manner by the source which generated it.
        e.consume ();
    }


    public void mouseReleased (MouseEvent e)
    { // called after a button is released
        isButtonPressed = false;
        repaint ();
        e.consume ();
    }


    public void mouseMoved (MouseEvent e)
    { // called during motion when no buttons are down
        mx = e.getX ();
        my = e.getY ();
        showStatus ("Mouse at (" + mx + "," + my + ")");
        repaint ();
        e.consume ();
    }


    public void mouseDragged (MouseEvent e)
    { // called during motion with buttons down
        mx = e.getX ();
        my = e.getY ();
        showStatus ("Mouse at (" + mx + "," + my + ")");
        repaint ();
        e.consume ();
    }


    Image my_gif;
    URL base;
    MediaTracker mt;

    public void init1 ()
    {
        mt = new MediaTracker (this);
        try
        {
            base = getDocumentBase ();
        }
        catch (Exception e)
        {
        }
        my_gif = getImage (getDocumentBase(), "ship.gif");
        mt.addImage (my_gif, 1);
        try
        {
            mt.waitForAll ();
        }
        catch (InterruptedException e)
        {
        }
    }


    public void paint (Graphics g)
    {
 g.drawImage( my_gif, mx, my, this );


      //  g.fillRect (mx,my,20,20);
    }
}



not sure what the program is here
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: