Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 29th, 2012, 12:42 PM
eaztridah eaztridah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 eaztridah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old October 29th, 2012, 01:05 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,814 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 7 h 54 m 5 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to 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.

Reply With Quote
  #3  
Old October 29th, 2012, 01:19 PM
eaztridah eaztridah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 eaztridah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old October 29th, 2012, 01:41 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,814 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 7 h 54 m 5 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Extracting values of mouseclick to another class

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap