The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Extracting values of mouseclick to another class
Discuss Extracting values of mouseclick to another class in the Java Help forum on Dev Shed. Extracting values of mouseclick to another class Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 29th, 2012, 12:42 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 11 m 12 sec
Reputation 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.
|

October 29th, 2012, 01:05 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
|
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.
|

October 29th, 2012, 01:19 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 2
Time spent in forums: 11 m 12 sec
Reputation Power: 0
|
|
Quote: | 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?
|

October 29th, 2012, 01:41 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|