Results 1 to 4 of 4
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with game applet in java

    Hi, I'm having trouble making a game work. I cant get my mouse to react to my applet at all. I've tried a few different methods, and a few different listeners types but haven't been successful with anything yet. Everything I have tried except for EventListeners has failed. I could use some serious help.
    Her is what I have right now.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class Catcher extends Applet implements MouseMotionListener{
    int x_pos;
    int y_pos;
    
    public void init(){
    addMouseMotionListener(this)
    }
    
    public void mouseMove(MouseEvent e){
    int x_pos=e.getX();
    }
    
    public void paint(Graphics g){
    g.setColor(Color.blue);
    g.fillRect(x_pos-25,y_pos-5,50,10);
    }
    }
    Is there anyone that has done Applet coding before that could help?
    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Declaring x_pos and y_pos as local variables in paint hid the member variables of the same name. Solution: eliminate the declarations in paint.
    In java:
    Java Code:
    public void paint(Graphics g) {
        // Change these - declaration/instantiations of local variables
        int x_pos=e.getX();  // These hide the member variables
        int y_pos=e.getY();  // declared above in class scope.
        // to these - instantiations of member variables
        x_pos=e.getX();
        y_pos=e.getY();
    Java Code:
    //  <applet code="CatcherRx" width="300" height="300"></applet>
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class CatcherRx extends Applet implements MouseMotionListener{
        int x_pos;
        int y_pos;
    
        public void init(){
            addMouseMotionListener(this);
        }
    
        public void mouseMoved(MouseEvent e){
            x_pos=e.getX();
            y_pos=e.getY();
            repaint();
        }
    
        public void paint(Graphics g){
            g.setColor(Color.blue);
            g.fillRect(x_pos-25,y_pos-5,50,10);
        }
    
        public void mouseDragged(MouseEvent e) {}
    }

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Just like hardwired just said, the compiler might not give u an error by doing
    Java Code:
    public void mouseMove(MouseEvent e){
    int x_pos=e.getX();
    }
    But u r declaring another x_pos in that mouseMove() method, in which the class variable x_pos must be refered as this.x_pos, not a very good on this context though

    And btw, if im not mistaken, by implementing MouseMotionListener we must override the method mouseDraggeed() and mouseMoved right?
    Just like hardwired just done there, if not the compiler would complain, maybe ur other listeners don't work because u didn't override the correct methods for that listener?

  4. #4
    winniejr_99 is offline Member
    Join Date
    Feb 2008
    Posts
    6
    Rep Power
    0

    Default

    try this program and just analyze it. okay??

    import java.awt.*;
    import java.applet.*;
    // import an extra class for the MouseListener
    import java.awt.event.*;

    // Tells the applet you will be using the MouseListener methods.

    public class MouseClickExample extends Applet implements MouseListener
    {
    // The X-coordinate and Y-coordinate of the last click.
    int xpos;
    int ypos;

    MyMouseListener ml;

    // The coordinates of the rectangle we will draw.
    // It is easier to specify this here so that we can later
    // use it to see if the mouse is in that area.
    int rect1xco,rect1yco,rect1width,rect1height;

    // The variable that will tell whether or not the mouse
    // is in the applet area.
    boolean mouseEntered;

    // variable that will be true when the user clicked i the rectangle
    // the we will draw.
    boolean rect1Clicked;

    public void init(){
    // Assign values to the rectanagle coordinates.
    rect1xco = 20;
    rect1yco = 20;
    rect1width = 100;
    rect1height = 50;

    // Add the MouseListener to your applet
    addMouseListener(ml);
    }

    public void paint(Graphics g){
    // Rectangle's color
    g.setColor(Color.green);

    g.fillRect(rect1xco,rect1yco,rect1width,rect1heigh t);

    g.setColor(Color.red);

    // When the user clicks this will show the coordinates of the click
    // at the place of the click.
    g.drawString("("+xpos+","+ypos+")",xpos,ypos);

    // If the click was in the rectangle show this message
    if (rect1Clicked) g.drawString("You clicked in the Rectangle",20,120);
    // else this one
    else g.drawString("You clicked outside of the rectangle",20,120);

    if (mouseEntered) g.drawString("Mouse is in the applet area",20,160);
    else g.drawString("Mouse is outside the Applet area",20,160);
    }

    /* These methods always have to present when you implement MouseListener

    public void mouseClicked (MouseEvent me) {}
    public void mouseEntered (MouseEvent me) {}
    public void mousePressed (MouseEvent me) {}
    public void mouseReleased (MouseEvent me) {}
    public void mouseExited (MouseEvent me) {}
    */

    // This method will be called when the mouse has been clicked.
    public void mouseClicked (MouseEvent me) {

    // Save the coordinates of the click lke this.
    xpos = me.getX();
    ypos = me.getY();

    // Check if the click was inside the rectangle area.
    if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco &&
    ypos < rect1yco+rect1height) rect1Clicked = true;
    // if it was not then rect1Clicked is false;
    else
    rect1Clicked = false;
    //show the results of the click
    repaint();

    }

    // This is called when the mous has been pressed
    public void mousePressed (MouseEvent me) {}

    // When it has been released
    // not that a click also calls these Mouse-Pressed and Released.
    // since they are empty nothing hapens here.
    public void mouseReleased (MouseEvent me) {}

    // This is executed when the mouse enters the applet. it will only
    // be executed again when the mouse has left and then re-entered.
    public void mouseEntered (MouseEvent me) {
    // Will draw the "inside applet message"
    mouseEntered = true;
    repaint();
    }

    // When the Mouse leaves the applet.
    public void mouseExited (MouseEvent me) {
    // will draw the "outside applet message"
    mouseEntered = false;
    repaint();
    }

    }

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. First Java-game: Containers
    By eastviolence in forum New To Java
    Replies: 0
    Last Post: 04-04-2008, 06:09 PM
  3. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM
  4. Help with java text game
    By silvia in forum New To Java
    Replies: 6
    Last Post: 07-27-2007, 06:58 PM
  5. Help with my game in java
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 04:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •