Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Location
    California
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Game Programming Applet problem

    Im 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 havent been sucessful with anything yet. Everything I have tried except for EventListeners has failed. I know its a problem within my code but Im not sure what it is, even after checking 3 different Java books (one specializing in Applet Game Programming). I could use some serious help.

    Her is what I have right now.

    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?

    -Peter

  • #2
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cant' see anything wrong with the code, but wouldn't be easer just to use eventlisteners?

    mouseMove, mouseEnter etc are deprecated aswell....
    <JPM />

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is what i get after fixing it

    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 mouseMoved(MouseEvent e){
    int x_pos=e.getX();
    }
    
    public void mouseDragged(MouseEvent e){
    }
    
    public void paint(Graphics g){
    g.setColor(Color.blue);
    g.fillRect(x_pos-25,y_pos-5,50,10);
    }
    }
    I do no program applets but other than that from what i can tell it should work and besides it missing main which i am totally not sure if it needs it or not

  • #4
    New to the CF scene
    Join Date
    Mar 2005
    Location
    California
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Missing main

    Since applets run through web pages, you use HTML as the main. Just put a new tag in the HTML code.
    <APPLET
    code = "Catcher.class"
    width = "500"
    height = "300"
    >
    </APPLET>
    This is how you actualy run applets without a main.

    As for the code, something is still off. I replaced the 'y_pos-5' with '500' so it would be in a better place, but that didn't fix the problem I'm having now. The rectangle is stuck to the left of the screen and doesnt react to the mouse at all. I think its only getting the x position once then drawing it once. Will I need a thread to fix this?

    EDIT: I found out why it wasn't moving, it was easier than I thought. I changed one thing, I took out the 'int' in the ' int x_pos=e.getX();' and everything was fine. weird, isn't it. I'll be posting more questions, problems and info on applets soon, so keep an eye out for me.

    FINAL CODE:
    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(){
    y_pos=200;
    addMouseMotionListener(this);
    }
    
    public void mouseMoved(MouseEvent e){
    x_pos=e.getX();
    repaint();
    }
    
    public void mouseDragged(MouseEvent e){
    }
    
    public void paint(Graphics g){
    g.setColor(Color.blue);
    g.fillRect(x_pos-25,y_pos,50,10);
    }
    }
    -Peter
    Last edited by kcussemanliame; 04-18-2005 at 12:27 AM. Reason: Changed, tested, worked


  •  

    Posting Permissions

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