#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Extracting values of mouseclick to another class


    Hi I am new to java and am having problems understanding this concept. I have a drawing class which draws objects and I have a mouseclick controller class which gets the x, y values of the mouseclick.

    The concept I can't seem to understand is when I have something like this in the mouseclick class:
    public void mousePressed(MouseEvent me) {
    int x = me.getX();
    int y = me.getY();

    how do you actually extract those values to another class considering the mousePressed is a void method? You cannot return these values as it is a void method.

    I want to say something like g.drawline(x, y, x+100, y+100) in the drawing class .. but cannot pull in those values from the mouseclick class.

    I understand it would be easy to put the mouseclick in the drawing class, but that is not allowed.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    One way would be to have instance variables to store the x and y values in your drawing class. Then provide setter methods so that another class could set the value of those variables. Have an instance of your drawing class in the class that listens for mouse events and then call the setting method using the instance.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by bullet
    One way would be to have instance variables to store the x and y values in your drawing class. Then provide setter methods so that another class could set the value of those variables. Have an instance of your drawing class in the class that listens for mouse events and then call the setting method using the instance.
    Thank you for your response, but because I am so new to java I do not know what instance variables or setter methods mean? Could you show a small example of how the x, y values from mouseclick class go to -> setter class then go to -> drawing class?
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    What I mean is doing something like this.

    import java.awt.event.*;

    Code:
    public class Test1 implements MouseListener {
       Test2 test2;
    
       Test1() {
          test2 = new Test2();
       }
    
       public void mousePressed(MouseEvent me) {
          int x = me.getX();
          int y = me.getY();
    
          test2.setX(x);
       }
    
    
    }
    Code:
    public class Test2 {
       int x;
    
       Test2() {
          x = 0;
       }
    
       void setX(int x) {
          this.x = x;
       }
    
       public void print() {
          System.out.println(x);
       }
    
    }
    By creating an instance of Test2 in Test1, we can then access its methods. Remember that an instance method has access to all instance variables in a class definition.

IMN logo majestic logo threadwatch logo seochat tools logo