Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Applet problem with MouseEvent

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Applet problem with MouseEvent

    Hi everyone,
    I have a lab assignment that I need to do, and currently I'm stuck at the step to make the "arrow" move every time the user clicks. Here is the description of my assignment:

    Start by drawing an arrow pointing at the upper-left corner of the screen (use the drawLine method)

    Now create two instance variables to represent the x and y screen coordinates where the arrow should be drawn. Then adjust your paint method to draw the arrow wherever the x and y coordinates dictate, rather than the upper-left corner. To test that this works, set your instance variables to different values in the init method, and see if your arrow goes there when you run the applet.

    Next, fill in the mousePressed method with code that updates the x and y coordinates of the arrow to be wherever the mouse was pressed down. To do this, you will need to use the getX() and getY() methods of the MouseEvent that is being passed in as a parameter (these methods return int values - see the MouseEvent Javadocs). You also need to make a call to repaint(); from mousePressed. This tells Java to make another call to your paint method as soon as it gets the chance. Note that repaint is a method inherited from the Applet class; we can call it because our class extends Applet.

    Now you should be able to click on your applet and the arrow will go wherever you click.
    So far here is my code :

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*; 
     
    public class Target extends Applet implements MouseListener, KeyListener {
        int x, y;
        public void init() { //init is called when the applet starts (much like a constructor)
            addMouseListener(this); //registers this applet to receive mouse events
            addKeyListener(this); //registers this applet to receive key events
        }
     
        public void paint(Graphics g) {
            g.drawLine(x, y, x+20, y+20);
            g.drawLine(x, y, x, y + 7);
            g.drawLine(x, y, x + 7, y);
        }
     
        public void mousePressed(MouseEvent e) {
            x = e.getX();
            y = e.getY();
            e.translatePoint(x, y);
            repaint();
        }
    }

    I know that the code inside mousePressed is wrong, but I can't figure out what's wrong with it. I try to click on the screen, but of course, the arrow does not move
    And I also want to ask about KeyPressed method too. The instructions were :

    Next fill in the keyPressed method. Make it so the arrow can move in any of the four directions given by the arrow keys on the keyboard. You can do this by using the getKeyCode method of KeyEvent and comparing the result with the static variables made available in the KeyEvent class. For example, to test if the left arrow key was pressed, use the following:

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {

    Not surprisingly, the constants you want are KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, and KeyEvent.VK_DOWN.

    Now you should be able to move the arrow by either clicking or using the arrow keys (you will have to click on the applet first to give the applet focus).
    I took a look at the javadoc for this class, but to be honest, I don't understand what the instructors want me to do ...
    Please take a look at my problem and suggest me what I should change for it to work. Thanks everyone in advance.
    Last edited by smashX; April 9th, 2011 at 11:05 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet problem with MouseEvent

    x = e.getX();
    y = e.getY();
    e.translatePoint(x, y);

    doesnt that mean your moving the cursor to the same position?

    If im getting this right...

    x = e.getX();
    y = e.getY();
    e.translatePoint(x, y + 10);

    Should move it to the right?

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Applet problem with MouseEvent

    @hirsty
    translatePoint(x,y) is relative to the current position, So translatePoint(x,y) doesn't move the mouse to x,y coordinates, but it adds x to x and y to y.
    So if mouse it currently at 10,10, and you say translatePoint(10,10), new position will be 20,20.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet problem with MouseEvent

    alrighty not used that yet was just an assumption

    just to get getting the focus the way in a program ive used this to automatically gain focus....

    setFocusable(true);
    requestFocus();

    EDIT: ive uses the keypressed events more often

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet problem with MouseEvent

    So does that mean I should not use translatePoint at all?

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Applet problem with MouseEvent

    had a lil mess around with your code in eclipse it does work, only if you click your mouse where you want the cursor to go, i think what your looking for is this: MouseMotionListener

Similar Threads

  1. Java applet programming problem
    By danielparry in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 4th, 2011, 10:31 AM
  2. the infamous null pointer applet problem
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2010, 07:09 PM
  3. Problem with my animation applet
    By cBlue in forum Java Applets
    Replies: 1
    Last Post: December 9th, 2009, 06:49 PM
  4. Problem veiwing textfields in applet
    By danwoods in forum AWT / Java Swing
    Replies: 0
    Last Post: October 21st, 2009, 02:34 AM
  5. applet and JPS problem.... please help me
    By rockster14 in forum Java Applets
    Replies: 0
    Last Post: August 7th, 2009, 03:59 PM